How to Build a PDFium Image Viewer with PSPDFKit
In this post, we provide you with a step-by-step guide outlining how to deploy PSPDFKit’s PDFium-based JavaScript image viewer.
PDFium is an open source PDF rendering library that was released by Google in 2014. Currently, PDFium is used to open billions of PDFs directly in the Chrome browser. At PSPDFKit, we leverage PDFium to render PDFs in our commercial PDF viewers.
In addition to our JavaScript integration, you can also deploy our PDFium-based viewer on other platforms, including iOS, Android, Windows, and Mac Catalyst.
What Is a JavaScript Image Viewer?
A JavaScript image viewer uses JavaScript to render and view images in a web browser without the need to download it to your hard drive or use an external application like an image reader.
PSPDFKit PDFium Image Viewer
Our PDFium-based image viewer can easily be integrated into your web application. It comes with 30+ features that let you view, annotate, edit, and sign documents directly in your browser. Out of the box, it has a polished and flexible UI that you can extend or simplify based on your unique use case.
- A prebuilt and polished UI for an improved user experience
- 15+ prebuilt annotation tools to enable document collaboration
- Support for more file types with client-side PDF, MS Office, and image viewing
- Dedicated support from engineers to speed up integration
Example of Our JavaScript Image Viewer
To see our image viewer in action, upload a JPG, PNG, or TIFF file by selecting Choose Example > Open Document (if you don’t see this, switch to the Standalone option). Once your image is displayed in the viewer, you can try drawing freehand, adding a note, or applying a crop or an e-signature.
Requirements
To get started, you’ll need:
- The latest stable version of Node.js.
- A package manager compatible with npm. This guide contains usage examples for the npm client (installed with Node.js by default).
Adding PSPDFKit to Your Project
-
Now, you can install the
pspdfkit
package fromnpm
. If you prefer, you can also download PSPDFKit for Web manually:
npm install pspdfkit
-
For PSPDFKit for Web to work, you have to copy the directory containing all the required library files (artifacts) to the
assets
folder. You can use the following command to do this:
cp -R ./node_modules/pspdfkit/dist/ ./assets/
Make sure your assets
directory contains the pspdfkit.js
file and a pspdfkit-lib
directory with the library assets.
Integrating into Your Project
-
Add the image you want to display to your project’s directory. You can use our demo image as an example.
-
Add an empty
<div>
element with a defined height to where PSPDFKit will be mounted:
<div id="pspdfkit" style="height: 100vh;"></div>
-
Include
pspdfkit.js
in your HTML page:
<script src="assets/pspdfkit.js"></script>
-
Initialize PSPDFKit for Web in JavaScript by calling
PSPDFKit.load()
:
<script> PSPDFKit.load({ container: "#pspdfkit", document: "image.jpg" // Add the path to your image here. }) .then(function(instance) { console.log("PSPDFKit loaded", instance); }) .catch(function(error) { console.error(error.message); }); </script>
You can see the full index.html
file below:
<!DOCTYPE html> <html> <head> <title>My App</title> <!-- Provide proper viewport information so that the layout works on mobile devices. --> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" /> </head> <body> <!-- Element where PSPDFKit will be mounted. --> <div id="pspdfkit" style="height: 100vh;"></div> <script src="assets/pspdfkit.js"></script> <script> PSPDFKit.load({ container: '#pspdfkit', document: 'image.jpg', // Add the path to your image here. }) .then(function (instance) { console.log('PSPDFKit loaded', instance); }) .catch(function (error) { console.error(error.message); }); </script> </body> </html>
Serving Your Website
You’ll use the npm serve
package to serve your project.
-
Install the
serve
package:
npm install --global serve
-
Serve the contents of the current directory:
serve -l 8080 .
-
Navigate to
http://localhost:8080
to view the website.
Adding Even More Capabilities
Once you’ve deployed your viewer, you can start customizing it to meet your specific requirements or easily add more capabilities. To help you get started, here are some of our most popular JavaScript guides:
- Adding annotations
- Editing documents
- Filling PDF forms
- Adding signatures to documents
- Real-time collaboration
- Redaction
- UI customization
Conclusion
You should now have our PDFium-based image viewer up and running in your web application. If you hit any snags, don’t hesitate to reach out to our Support team for help.
You can also integrate our JavaScript image viewer using web frameworks like Angular, Vue.js, and React.js. To see a list of all web frameworks, start your free trial. Or, launch our demo to see our viewer in action.
FAQ
What is a JavaScript image viewer?
A JavaScript image viewer uses JavaScript to render and view images directly in a web browser, eliminating the need to download the image to your hard drive or use an external application. This provides a seamless and efficient way to view images online.
How can you integrate PSPDFKit’s PDFium-based image viewer into a web application?
To integrate PSPDFKit’s PDFium-based image viewer into a web application, you need to install the pspdfkit package from npm, copy the necessary library files to the assets folder, and initialize PSPDFKit for Web by calling PSPDFKit.load() with the appropriate configurations.
What features does the PSPDFKit PDFium image viewer offer?
The PSPDFKit PDFium image viewer offers over 30 features, including viewing, annotating, editing, and signing documents directly in the browser. It supports various file types, provides a polished and customizable UI, and facilitates collaboration with prebuilt annotation tools.
What are the requirements for building a PDFium image viewer with PSPDFKit?
To build a PDFium image viewer with PSPDFKit, you need the latest stable version of Node.js and a package manager compatible with npm. Additionally, you need to include the PSPDFKit JavaScript file in your HTML and initialize it with the desired image document.
How can you serve your web application using PSPDFKit’s image viewer?
You can serve your web application using PSPDFKit’s image viewer by installing the serve package via npm and serving the contents of the current directory. Navigate to the specified local server URL (e.g., http://localhost:8080) to view the website and interact with the image viewer.