pdfmake
pdfmake copied to clipboard
Pdf files look identical, but base64 representation differs on node 16 and node 20
HI!
First of all, thank you for this library.
What is the problem?
Pdf files that were generated using node 16 and node 20 are not equal.
Prerequisites:
- pdfmake: ^0.2.7
- Use type: "module" in package.json
- nvm: 0.39.7
- Download this font - https://fonts.google.com/specimen/Roboto
- Create folder with
index.jsfile and installed Roboto font folder in the root, so you have following structure:
- folder
- index.js
- package.json
- Roboto
- Roboto-Bold.ttf
Steps to reproduce
- Paste following code in
index.mjsfile
import { writeFile } from 'fs/promises';
import PdfPrinter from 'pdfmake';
const pdfPrinter = new PdfPrinter({
Roboto: {
bold: 'Roboto/Roboto-Bold.ttf',
bolditalics: 'Roboto/Roboto-BoldItalic.ttf',
normal: 'Roboto/Roboto-Light.ttf',
italics: 'Roboto/Roboto-Light.ttf',
},
});
const doc = pdfPrinter.createPdfKitDocument({
content: 'Hello world!',
info: {
creationDate: new Date('2024-01-24'),
},
});
const pdf = await pdfStreamToBuffer(doc);
const base64Pdf = pdf.toString('base64');
await writeFile(process.argv[2], Buffer.from(base64Pdf, 'base64'));
async function pdfStreamToBuffer(stream) {
return new Promise((resolve, reject) => {
const buffer = [];
stream.on('data', (chunk) => buffer.push(chunk));
stream.on('end', () => resolve(Buffer.concat(buffer)));
/* istanbul ignore next */
stream.on('error', (err) => reject(`error converting stream - ${err}`));
stream.end();
});
}
- Install & use node 16
$ nvm install 16
$ nvm use 16
- Generate pdf file use node 16 and
index.mjsscript:
node index.mjs example-node-16.pdf
- Install & use node 20
$ nvm install 20
$ nvm use 20
- Generate pdf file use node 20 and
index.mjsscript:
node index.mjs example-node-20.pdf
- Create
compare.mjsfile:
import { readFile } from 'fs/promises';
const node16Pdf = await readFile(`./${process.argv[2]}`, 'base64');
const node20Pdf = await readFile(`./${process.argv[3]}`, 'base64');
console.log(node16Pdf === node20Pdf);
- Compare both files as base64 strings:
$ node index.mjs example-node-16.pdf example-node-20.pdf
This should output true to the console, but for some reason it outputs false.
Thanks for the help!
+1, seeing something similar in my application
Tested with versions 20.9.0 and 16.20.2. And both files are identical.