How to use the pdf-lib.PDFDocumentFactory.load function in pdf-lib

To help you get started, we’ve selected a few pdf-lib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Hopding / pdf-lib / examples / document_modification / node / index.js View on Github external
/* ========================= 1. Read in Assets ============================== */
// 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'),
  marioPngBytes: fs.readFileSync('./assets/running_mario.png'),
  taxVoucherPdfBytes: fs.readFileSync('./assets/income_tax_voucher.pdf'),
};

/* ================== 2. Load 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 load the tax voucher PDF file into a PDFDocument object.
const pdfDoc = PDFDocumentFactory.load(assets.taxVoucherPdfBytes);

// Let's define some constants that we can use to reference the fonts and
// images later in the script.
const COURIER_FONT = 'Courier';
const UBUNTU_FONT = 'Ubuntu';
const MARIO_PNG = 'MarioPng';

// Now we embed a standard font (Courier), and the custom TrueType font we
// read in (Ubuntu-R).
const [courierFontRef] = pdfDoc.embedStandardFont('Courier');
const [ubuntuFontRef] = pdfDoc.embedFont(assets.ubuntuFontBytes);

// Next, we embed the PNG image we read in.
const [marioPngRef, marioPngDims] = pdfDoc.embedPNG(assets.marioPngBytes);

/* ====================== 3. Modify Existing Page =========================== */