Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where is the promise? #2407

Closed
pjebs opened this issue Mar 13, 2022 · 8 comments
Closed

Where is the promise? #2407

pjebs opened this issue Mar 13, 2022 · 8 comments

Comments

@pjebs
Copy link

pjebs commented Mar 13, 2022

I have a link to a font that does not exist because the internet is down.

let fonts = {
	'Courier Prime': {
		normal: 'https://fonts.cdnfonts.com/s/16424/Courier Prime.woffx' // doesn't exist
	}
};
docDefinition = ...
const pdfDocGenerator = pdfMake.createPdf(docDefinition, null, fonts);
	pdfDocGenerator.getBlob((blob) => {
		// 
	});

Since the font doesn't exist, I see an error on the client side console:

URLBrowserResolver.js:47          GET https://fonts.cdnfonts.com/s/16424/Courier%20Prime.woffx net::ERR_FAILED 404
(anonymous) @ URLBrowserResolver.js:47
fetchUrl @ URLBrowserResolver.js:10
(anonymous) @ URLBrowserResolver.js:61
URLBrowserResolver.resolve @ URLBrowserResolver.js:59
Document._createDoc @ pdfMake.js:61
Document.getBuffer @ pdfMake.js:257
Document.getBlob @ pdfMake.js:244
generatePDF @ pdf.ts:83
savePDF @ index.svelte? [sm]:68
(anonymous) @ index.mjs:996
bubble @ index.mjs:996
click_handler @ SideNavMenuItem.svelte? [sm]:18
URLBrowserResolver.js:23 Uncaught (in promise) TypeError: Failed to fetch (url: "https://fonts.cdnfonts.com/s/16424/Courier Primex.woff")
    at URLBrowserResolver.js:23:1

How can I catch this uncaught promise and act appropriately?

@liborm85
Copy link
Collaborator

Versions 0.1.x and 0.2.x not support promises.
Promises are added in version 0.3, which is in beta state, see #2375.

@pjebs
Copy link
Author

pjebs commented Mar 14, 2022

I'm using 0.2.x. why did the error message say promise not caught. See bottom

@liborm85
Copy link
Collaborator

Because fetching fonts uses promises to download file.

@pjebs
Copy link
Author

pjebs commented Mar 14, 2022

How can I handle that if the internet goes down?

@liborm85
Copy link
Collaborator

Is is not possible. Is solved in version 0.3.

@pjebs
Copy link
Author

pjebs commented Mar 14, 2022

Can v 0.2 be updated? Is v0.3 nearly prod ready?

@liborm85
Copy link
Collaborator

Version 0.2 support only callbacks, cannot be changed due to backward compatibility. Version 0.3 is in beta state, date of final release is unknown.

@pjebs pjebs closed this as completed Mar 15, 2022
@alxvallejo
Copy link

You can wrap it in a promise as such:

const generatePdf = (docInfo) => {
	return new Promise((resolve, reject) => {
		let doc = new PdfPrinter(FONTS).createPdfKitDocument(docInfo);
		let chunks = [];
		let result;

		doc.on('data', function (chunk) {
			chunks.push(chunk);
		});
		doc.on('end', function () {
			result = Buffer.concat(chunks);
			resolve('data:application/pdf;base64,' + result.toString('base64'));
		});
		doc.on('error', (error) => reject(error));
		doc.end();
	});
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants