Pdf Course: Javascript
High-resolution images inflate PDF file sizes drastically. Compress and downscale images to 72 or 150 DPI before embedding them into your documents.
const PDFDocument, rgb, StandardFonts = require('pdf-lib'); const fs = require('fs'); async function modifyAndMergePDFs() // Load existing PDF files into memory buffers const basePdfBuffer = fs.readFileSync('invoice.pdf'); // Create a brand new document container const pdfDoc = await PDFDocument.create(); // Load the existing document into our manipulation engine const sourceDoc = await PDFDocument.load(basePdfBuffer); // Copy all pages from our source invoice into the main document container const copiedPages = await pdfDoc.copyPages(sourceDoc, sourceDoc.getPageIndices()); copiedPages.forEach((page) => pdfDoc.addPage(page)); // Get access to the first page to stamp a watermark const pages = pdfDoc.getPages(); const firstPage = pages[0]; // Embed a standard built-in font const helveticaFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold); // Add a text overlay (Watermark) firstPage.drawText('PAID & PROCESSED', x: 150, y: 400, size: 36, font: helveticaFont, color: rgb(0.85, 0.18, 0.18), // Muted crimson red opacity: 0.25, // Transparent look rotate: angle: 30 // Angled diagonally across the page ); // Save the operations back to a file buffer const modifiedPdfBytes = await pdfDoc.save(); fs.writeFileSync('stamped_invoice.pdf', modifiedPdfBytes); console.log('Watermark stamped successfully.'); modifyAndMergePDFs(); Use code with caution. javascript pdf course
If you prefer defining document structures using JSON rather than calculating coordinates manually, pdfmake is an excellent choice. It features an automated layout engine that handles word wrapping, page breaks, and table alignments automatically. 2. PDF-LIB (Best for Modifying Existing PDFs) High-resolution images inflate PDF file sizes drastically
Every modern software ecosystem requires document generation. Building these features manually using HTML and CSS printer styling is unreliable. Client-side and server-side JavaScript libraries give you pixel-perfect control over your output. If you prefer defining document structures using JSON
