Embed PDF viewer in web applications
✓Works with OpenClaudeYou are a web developer integrating a PDF viewer into a web application. The user wants to embed a functional PDF viewer that displays PDF files in the browser with standard controls (zoom, navigation, search).
What to check first
- Verify you have a modern browser with PDF.js support or plan to use a library like
pdfjs-dist,react-pdf, orpdf-lib - Check that your PDF files are accessible via URL or can be loaded as binary data
- Confirm your project uses a module bundler (Webpack, Vite) or supports ES modules
Steps
- Install
pdfjs-distvia npm:npm install pdfjs-dist - Set the worker file path in your initialization code — PDF.js requires a worker script to parse PDFs asynchronously
- Create a canvas element in your HTML where the PDF will render
- Load the PDF document using
getDocument()with either a URL string or binary data - Fetch individual pages using
getPage(pageNumber)on the PDF document object - Create a render context with the canvas 2D context and specify viewport scaling
- Call
render()on the page with the rendering task to draw it to the canvas - Implement navigation controls to increment/decrement the current page number and re-render
Code
import * as pdfjsLib from 'pdfjs-dist';
// Set the worker source
pdfjsLib.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjsLib.version}/pdf.worker.min.js`;
class PDFViewer {
constructor(containerSelector, pdfUrl) {
this.container = document.querySelector(containerSelector);
this.pdfUrl = pdfUrl;
this.pdfDoc = null;
this.pageNum = 1;
this.pageRendering = false;
this.pageNumPending = null;
this.scale = 1.5;
this.setupUI();
}
setupUI() {
this.container.innerHTML = `
<div class="pdf-viewer">
<div class="pdf-controls">
<button id="prev-btn">← Previous</button>
<span id="page-info"></span>
<button id="next-btn">Next →</button>
<input type="range" id="zoom-slider" min="0.5" max="3" step="0.1" value="1.5">
</div>
<canvas id="pdf-canvas"></canvas>
</div>
`;
this.canvas = document.getElementById('pdf-canvas');
this.pageInfo = document.getElementById('page-info');
document.getElementById('prev-btn').addEventListener('click', () => this.prevPage());
document.getElementById('next-btn').addEventListener('click', () => this.nextPage());
document.
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related PDF Generation Skills
Other Claude Code skills in the same category — free to download.
PDF Generator
Generate PDFs from HTML or React components
Invoice PDF
Generate professional invoice PDFs with line items
Report PDF
Generate data report PDFs with charts and tables
PDF Form Filler
Programmatically fill PDF forms and templates
PDF Form Filler
Programmatically fill PDF form fields from data
PDF Watermarker
Add watermarks to PDFs (text, image, or stamp)
PDF Text Extraction
Extract text from PDFs (including scanned ones via OCR) for indexing or analysis
Want a PDF Generation skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.