PDF basics
Tooling and libraries
Creating PDFs from scratch
Converting HTML to PDF
Editing and merging PDFs
Advanced features
Testing and deployment
Project: Invoice generator
Resources
Filling a PDF form using pdf-lib (Universal JavaScript):
import PDFDocument from 'pdf-lib'; import fs from 'fs';async function fillTaxForm() // 1. Load the existing PDF template const templateBytes = fs.readFileSync('w9_template.pdf'); const pdfDoc = await PDFDocument.load(templateBytes);
// 2. Get the form const form = pdfDoc.getForm(); // 3. Fill fields form.getTextField('Name').setText('Jane Developer'); form.getTextField('SSN').setText('123-45-6789'); form.getCheckBox('IsUSCitizen').check(); // 4. Flatten to prevent editing form.flatten(); // 5. Save const pdfBytes = await pdfDoc.save(); fs.writeFileSync('filled_w9.pdf', pdfBytes);
Before installing heavy libraries, remember that the browser is a rendering engine. You can use window.print() to turn any webpage into a PDF.
The Trick: Create a "print-only" stylesheet.
@media print
body
margin: 0;
padding: 0;
.no-print
display: none; /* Hide buttons, nav bars */
.print-header
position: fixed;
top: 0;
a[href]::after
content: " (" attr(href) ") "; /* Show URLs */
JavaScript:
document.getElementById('generatePrint').onclick = () =>
window.print();
;
Best for: Receipts, simple reports, and legal documents where WYSIWYG matters.
If you are building a React/SaaS app, here is the industry standard pattern for PDF generation:
import useRef from 'react'; import useReactToPrint from 'react-to-print'; import jsPDF from 'jspdf'; import html2canvas from 'html2canvas';const InvoiceGenerator = () => const componentRef = useRef(); javascript pdf course
// Option A: Print stylesheet method (Fastest) const handlePrint = useReactToPrint( content: () => componentRef.current, );
// Option B: Exact pixel download (Best for saving files) const handleDownload = async () => const canvas = await html2canvas(componentRef.current, scale: 2 ); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF( orientation: 'portrait', unit: 'px', format: [canvas.width, canvas.height] ); pdf.addImage(imgData, 'PNG', 0, 0, canvas.width, canvas.height); pdf.save('invoice.pdf'); ;
return ( <div> <div ref=componentRef className="invoice-container"> /* Your complex React Invoice HTML/CSS here / <h1>Invoice #123</h1> / ... */ </div> <button onClick=handlePrint>Print / Save as PDF</button> <button onClick=handleDownload>Download Exact PDF</button> </div> ); ;
| Task | Browser (Client) | Node.js (Server) |
| :--- | :--- | :--- |
| Create from scratch | jspdf | PDFKit |
| Edit/Fill forms | pdf-lib | pdf-lib |
| Extract text | pdfjs-dist (Mozilla) | pdf-parse |
| Merge/Split | pdf-lib | pdf-lib / hummusjs |
| HTML to PDF | html2pdf.js | puppeteer (Headless Chrome) |
Without structured learning, you will hit these walls: PDF basics
Goals: Understand what a PDF actually is (not just a picture).
console.log to inspect bytes of a sample PDF.Generating a PDF is only half the battle. You must display it inside your web app without forcing a download.