Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* ==================== 1. Read in Fonts and Images ========================= */
// This step is platform dependent. Since this is a Node script, we can just
// read the assets in from the file system. But this approach wouldn't work
// in a browser. Instead, you might need to make HTTP requests for the assets.
const assets = {
ubuntuFontBytes: fs.readFileSync('./assets/ubuntu-fonts/Ubuntu-R.ttf'),
unicornJpgBytes: fs.readFileSync('./assets/cat_riding_unicorn.jpg'),
marioPngBytes: fs.readFileSync('./assets/running_mario.png'),
};
/* ================ 2. Create and Setup the PDF Document ==================== */
// This step is platform independent. The same code can be used in any
// JavaScript runtime (e.g. Node, the browser, or React Native).
// Here we create a new PDF document.
const pdfDoc = PDFDocumentFactory.create();
// Let's define some constants that we can use to reference the fonts and
// images later in the script.
const HELVETIVA_FONT = 'Helvetica';
const UBUNTU_FONT = 'Ubuntu';
const UNICORN_JPG = 'UnicornJpg';
const MARIO_PNG = 'MarioPng';
// Now we embed a standard font (Helvetiva), and the custom TrueType font we
// read in (Ubuntu-R).
const [helveticaFontRef, helveticaFont] = pdfDoc.embedStandardFont(
HELVETIVA_FONT,
);
const [ubuntuFontRef] = pdfDoc.embedFont(assets.ubuntuFontBytes);
// Next, we embed the JPG and PNG images we read in.