Peer reviewed by Jani Hartikainen, Florian Rappl, Jezen Thomas, and Jeff Smith. Thanks to SitePoint's peer reviewers for their contributions!
Most modern browsers natively support PDF viewing, but this functionality is beyond a developer's direct control. Imagine needing to customize a web app's PDF display – disabling the print button, or restricting page access based on user subscriptions. While the <embed></embed>
tag utilizes the browser's native renderer, it lacks the programmatic control for such customizations.
Enter PDF.js, a powerful library from Mozilla Labs. It renders PDFs within the browser, giving developers complete control over the rendering process.
Key Features of PDF.js:
- Full Control: Bypass browser limitations and customize PDF rendering.
- HTML5-Based: Works in modern browsers without plugins.
- Rendering Options: Supports Canvas and SVG for flexible implementation.
- Asynchronous Loading: Uses promises for efficient handling of large files.
- Advanced Features: Enables text-layer rendering, custom zoom, and more.
Understanding PDF.js
PDF.js, built on HTML5, eliminates the need for third-party plugins. Its use extends to various online file-sharing services (Dropbox, CloudUp, Jumpshare, etc.) for seamless online PDF viewing. While incredibly useful, integrating PDF.js can be challenging due to limited documentation on advanced features like text layers, annotations, and password-protected files.
This article explores PDF.js integration, covering:
- Basic Integration
- SVG Rendering
- Text-Layer Rendering
- Zooming
Basic Integration
1. Downloading Necessary Files:
PDF.js is a JavaScript library. You'll need pdf.js
and pdf.worker.js
. While Node.js and Gulp are options for downloading, a simpler approach is using these direct URLs (always providing the latest version):
https://mozilla.github.io/pdf.js/build/pdf.js
https://mozilla.github.io/pdf.js/build/pdf.worker.js
2. Web Workers and PDF.js:
PDF parsing and rendering are computationally intensive. PDF.js leverages HTML5 Web Workers to offload these tasks to a separate thread, preventing browser lockups. This is the default behavior, but can be disabled if needed.
3. Promises in PDF.js:
The PDF.js API utilizes promises for clean asynchronous operation handling.
4. A Simple Example:
Let's render a simple "Hello World!" PDF (available at http://mozilla.github.io/pdf.js/examples/learning/helloworld.pdf
). Ensure your files are served via a local web server (e.g., http://localhost/pdfjs_learning/index.html
).
Include pdf.js
in your index.html
:
<🎜>
You can optionally specify the pdf.worker.js
path if it's not in the same directory:
PDFJS.workerSrc = "/path/to/pdf.worker.js";
Now, add the following JavaScript to render the PDF:
var url = "http://mozilla.github.io/pdf.js/examples/learning/helloworld.pdf"; PDFJS.getDocument(url) .then(function(pdf) { return pdf.getPage(1); }) .then(function(page) { var scale = 1.5; var viewport = page.getViewport(scale); var canvas = document.getElementById('the-canvas'); var context = canvas.getContext('2d'); canvas.height = viewport.height; canvas.width = viewport.width; var renderContext = { canvasContext: context, viewport: viewport }; page.render(renderContext); });
And add a <canvas></canvas>
element to your index.html
:
<canvas id="the-canvas"></canvas>
This code fetches, parses, and renders the PDF onto the canvas. PDFJS.getDocument()
initiates the asynchronous download; pdf.getPage()
retrieves a specific page; page.render()
performs the rendering.
Rendering Using SVG
PDF.js also supports SVG rendering. Modify the page.render()
section to use SVG:
page.getOperatorList() .then(function(opList) { var svgGfx = new PDFJS.SVGGraphics(page.commonObjs, page.objs); return svgGfx.getSVG(opList, viewport); }) .then(function(svg) { container.appendChild(svg); });
Replace the <canvas></canvas>
with a <div id="the-svg"></div>
in your HTML.
Rendering Text Layers
To enable text selection, download text_layer_builder.js<code>text_layer_builder.js
and text_layer_builder.css<code>text_layer_builder.css
and include them in your HTML. Use a more complex PDF (e.g., http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf<code>http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf
). The following code renders multiple pages and adds text layers:
<🎜>
Zooming
Adjust the scale<code>scale
variable to control zoom level.
Conclusion
PDF.js provides a powerful and flexible way to integrate custom PDF rendering into web applications. Its clean API and asynchronous handling make it a valuable tool for developers. Experiment with the code and explore its advanced features!
Frequently Asked Questions (FAQs) (Concisely summarized)
- Purpose of Custom Rendering: Provides a seamless user experience within the web application, eliminating the need for external PDF viewers.
- How PDF.js Works: Parses and renders PDFs using HTML5 and web standards.
- Implementation: Include the library, use the API to load and render on a canvas.
- Customization: Manipulate the canvas and use CSS to style.
- Limitations: May struggle with complex PDFs, performance depends on file size and device.
- Performance Improvement: Optimize PDF files, use lazy loading.
- Compatibility: Works well with other JavaScript libraries.
- Licensing: Open-source (Apache License 2.0).
- Contribution: Welcome to contribute to the project.
- Support: Consult the official documentation and GitHub community.
The above is the detailed content of Custom PDF Rendering in JavaScript with Mozilla's PDF.Js. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools
