JavaScript PDF: Generate, Edit, and Render with JavaScript
Learn how to work with PDFs in JavaScript using libraries like pdf-lib and pdf.js. Generate, modify, render, and extract text in Node.js and browsers with practical, runnable examples.

According to JavaScripting, JavaScript PDF workflows cover generating, editing, rendering, and text extraction using libraries such as pdf-lib and pdf.js. The approach works in both Node.js and browser contexts, enabling programmatic PDF creation, form filling, and content extraction. This quick overview pairs with practical examples, prerequisites, and common pitfalls for real-world projects.
What is JavaScript PDF and when to use it
JavaScript PDF workflows enable generation, modification, rendering, and text extraction directly from JS. They run in Node.js and in browsers, making it possible to automate report creation, fill forms, or extract content from existing documents. Popular libraries such as pdf-lib and pdf.js provide robust APIs, but they differ in intent: pdf-lib focuses on creation and editing, while pdf.js focuses on rendering. According to JavaScripting, choosing the right tool depends on whether you need to write or render, or both.
// Node.js example using CommonJS
const { PDFDocument, StandardFonts, rgb } = require('pdf-lib');
(async () => {
const doc = await PDFDocument.create();
const page = doc.addPage([612, 792]);
const font = await doc.embedFont(StandardFonts.Helvetica);
page.drawText('Hello from JavaScript PDF', { x: 50, y: 700, size: 24, font, color: rgb(0,0,0) });
const bytes = await doc.save();
// write bytes to disk in your environment
})();// ES modules example (modern Node or browser bundler)
import { PDFDocument, StandardFonts, rgb } from 'pdf-lib';
async function build() {
const d = await PDFDocument.create();
const p = d.addPage([612, 792]);
const f = await d.embedFont(StandardFonts.Helvetica);
p.drawText('JavaScript PDF', { x: 60, y: 730, size: 26, font: f, color: rgb(0.1,0.1,0.1) });
const b = await d.save();
return b;
}
export default build;When to use this approach: You need controlled PDF generation, dynamic content, or form filling. If you only need to display PDFs, you should consider a rendering approach with pdf.js instead of writing new content.
Steps
Estimated time: 60-90 minutes
- 1
Initialize project
Create a new Node.js project and install pdf-lib. This establishes a clean workspace for PDF generation tasks.
Tip: Use npm init -y to speed setup. - 2
Implement a generator
Write a module that creates a PDF with sample text and fonts. Use pdf-lib's PDFDocument API to build structure.
Tip: Embed a standard font for portability. - 3
Add file output
Write the generated PDF to disk or a blob. Decide between Buffer/Uint8Array depending on environment.
Tip: In browsers, use blob URLs for download. - 4
Parse or render
Choose between parsing (pdf-parse) or rendering (pdf.js). Add a small demo for the chosen path.
Tip: Keep dependencies small for faster installs. - 5
Run and verify
Execute scripts and verify PDF content via text extraction or visual render. Iterate with fixes.
Tip: Check fonts and layout in multiple viewers.
Prerequisites
Required
- Required
- npm or pnpmRequired
- Required
- Basic knowledge of promises and async/awaitRequired
- Familiarity with PDF basics (views, fonts, forms)Required
Optional
- Sample PDF file for parsing (optional)Optional
Keyboard Shortcuts
| Action | Shortcut |
|---|---|
| Copy selectionCode editor | Ctrl+C |
| Paste into editorCode editor | Ctrl+V |
| Save fileEditor | Ctrl+S |
| Open terminalVS Code | Ctrl+` |
| Run scriptIn VS Code to run build task | Ctrl+⇧+B |
| Format documentEditor | ⇧+Alt+F |
Questions & Answers
What is the difference between pdf-lib and pdf.js?
pdf-lib is aimed at creating, modifying, and filling PDFs, while pdf.js focuses on rendering PDFs in the browser. They can be used together but serve distinct purposes. If you need to generate content, choose pdf-lib; if you only need to display a PDF, pdf.js is typically sufficient.
Pdf-lib helps you build or edit PDFs, while pdf.js renders them in the browser. Use the right tool for the task—generation vs viewing.
Can I modify an existing PDF's form fields with JavaScript?
Yes. Many libraries provide form APIs. You can access fields, set values, and save the updated document. Ensure the PDF's permissions allow modification and that the fonts render consistently.
You can fill or alter form fields in a PDF using JavaScript libraries, then save the result.
Is it possible to render PDFs in non-Chromium browsers?
Rendering depends on the browser and library. pdf.js supports multiple browsers, but rendering quality and features may vary. Test in all target environments.
Rendering works in many browsers, but test across targets to avoid surprises.
How do I extract text from a PDF in Node.js?
Use a parsing library like pdf-parse. Load the PDF into a buffer, pass it to the parser, and access the extracted text. Handle encoding and layout nuances.
Use a PDF parser library to pull out text content from your document.
What about licensing and security for these libraries?
Review the license of libraries like pdf-lib (MIT) or pdf.js. Consider security implications when loading external PDFs and applying patches promptly.
Check licenses and stay aware of security considerations when handling PDFs.
Do these libraries support encryption or digital signatures?
Some libraries offer basic support, but full-fledged encryption or digital signatures may require specialized tooling or server-side processing. Verify capabilities in the library docs.
Encryption and signatures may require extra tools; verify capabilities in docs.
What to Remember
- Choose the right library for your task: pdf-lib for creation/editing, pdf.js for rendering.
- Always embed fonts to ensure consistent rendering across platforms.
- Test in Node.js and browser environments to catch environment-specific issues.
- Prefer structured error handling when parsing or generating PDFs.
- Validate PDFs for security and licensing before distribution.