parallax / jsPDF

Client-side JavaScript PDF generation for everyone.
https://parall.ax/products/jspdf
MIT License
29k stars 4.64k forks source link

png and ttf not correct from output() #3749

Open jmbluethner opened 1 month ago

jmbluethner commented 1 month ago

Hey there 👋

I'm currently working on my first jsPDF project, and it's going great, accept for one issue ...

On the clientside, I'm generating a PDF which then gets sent to a local REST API. Saving the PDF locally, using the save() method works fine and I get my beautiful PDF. However, after sending it to the API using the output() method, the Barcode TTF is lost and the png on the top right corner looks super weird as well.

PDF from save()

image

PDF from output()

image

Might the problem be, that neither the custom ttf nor the png are being embedded to the PDF? If so, how would I change that? Or is it something different?

Any advice would be highly appreciated!

Thanks in advance and best regards 😊

jmbluethner commented 1 month ago

Just for reference I'm adding the part from the API endpoint which is responsible for encoding the output() data back into a PDF file. req.body.doc is the data from output()

// [...]
const writer = fs.createWriteStream(uploadDirectory + '' + fileName);
writer.write(req.body.doc)
writer.end();
// [...]
jmbluethner commented 1 month ago

Found the solution. I had to add the correct encoding in fs.createWriteStream()

const writer = fs.createWriteStream(uploadDirectory + '' + fileName, {encoding: 'binary'});