Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/utils/pdf/__tests__/create-document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,16 @@ describe('createDocument', () => {
).rejects.toThrow('boom');
expect(onError).toHaveBeenCalled();
});

it('calls onError and rejects when the browser download fails', async () => {
const { pdfMake } = createFakePdfMake();
const onError = jest.fn();
global.URL.createObjectURL = jest.fn(() => {
throw new Error('no blob url');
});
jest.spyOn(console, 'error').mockImplementation(() => {});
const api = await createDocument({ pdfMake, template: () => ({ content: [] }), data: {}, onError });
await expect(api.download('receipt.pdf')).rejects.toThrow('no blob url');
expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'no blob url' }));
});
});
8 changes: 8 additions & 0 deletions src/utils/pdf/__tests__/download-blob.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ describe('downloadBlob', () => {
downloadBlob(new Blob(['%PDF']), 'file.pdf');
expect(document.body.contains(anchor)).toBe(true);
});

it('rethrows when the browser cannot start the download', () => {
global.URL.createObjectURL = jest.fn(() => {
throw new Error('no blob url');
});
jest.spyOn(console, 'error').mockImplementation(() => {});
expect(() => downloadBlob(new Blob(['%PDF']), 'file.pdf')).toThrow('no blob url');
});
});
8 changes: 8 additions & 0 deletions src/utils/pdf/__tests__/resolve-font.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ describe('resolveFont', () => {
expect(pdfMake.addFontContainer).toHaveBeenCalledWith({ vfs: font.vfs, fonts: font.fonts });
});

it('falls back to Helvetica when a pre-baked container does not define its family', async () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
const pdfMake = fakePdfMake();
const font = { family: 'Brand', vfs: { 'BrandSans-normal.ttf': 'QUFB' }, fonts: { BrandSans: { normal: 'BrandSans-normal.ttf' } } };
await expect(resolveFont(pdfMake, font)).resolves.toBe('Helvetica');
expect(pdfMake.addFontContainer).not.toHaveBeenCalled();
});

it('fetches + base64s a URL font and registers a container', async () => {
global.fetch = jest.fn(() => Promise.resolve({ ok: true, arrayBuffer: () => Promise.resolve(new Uint8Array([65, 66, 67]).buffer) }));
const pdfMake = fakePdfMake();
Expand Down
1 change: 1 addition & 0 deletions src/utils/pdf/download-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const downloadBlob = (blob, filename) => {
} catch (err) {
// eslint-disable-next-line no-console
console.error('[pdf] download failed', err);
throw err;
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/utils/pdf/resolve-font.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export const resolveFont = async (pdfMake, font) => {

// Imported / pre-baked container — no fetch.
if (font.family && font.vfs && font.fonts) {
if (!font.fonts[font.family]) {
// eslint-disable-next-line no-console
console.warn('[pdf] font container lacks family, using Helvetica:', font.family);
return HELVETICA;
}
pdfMake.addFontContainer({ vfs: font.vfs, fonts: font.fonts });
return font.family;
}
Expand Down
Loading