How to Build a Cordova PDF Viewer with PDF.js

24 Jul 2020

Dustin Riley

In this article (a six-minute read), you’ll learn how to quickly build a PDF viewer with Cordova and PDF.js, a popular open-source PDF viewer.

Here’s what we’re going to build.

The source code for this project is available in our Git repo.


Step 1 - Install the Cordova CLI

The Cordova CLI makes it a lot easier to scaffold a new project from the Node.js command line. To install it globally, we will open our Node.js terminal and type the following command:

npm install -g cordova

Step 2 - Create the Project

To create our project we will simply run in Node.js these commands:

cordova create pdfjs-viewer express.pdfjs.viewer pdfjsViewer
cd pdfjs-viewer

This command will create our project structure, but Cordova will need a target platform to build to. To see the list of available platforms, you can run this command:

cordova platform ls

For this tutorial, we will be using the browser platform, but you can add another platform or choose a different one. To add the browser platform to your project run the following command:

cordova platform add browser

To check to see if you meet the requirements to build the project for all the platforms you have added (the browser platform doesn’t have any requirements) you can do so with this command:

cordova requirements

Once you have downloaded all the necessary files to pass the requirements, we can build and run the project via the following commands:

cordova build
cordova run browser

Navigating to http://localhost:8000/index.html should show us the default Cordova project:

Step 3 - Integrating React

Next, we are going to integrate React into our Cordova app. Integrating React will allow us to run React natively on any platform you chose in the previous step, meaning you would be able to run your React web app as a native IOS/Android app.

First, we need to create our React app within the pdfjs-viewer directory. To do so, run the following command in the Node.js terminal within the pdfjs-viewer directory.

npx create-react-app pdfjs-viewer-react

This command will create a new folder pdfjs-viewer/pdfjs-viewer-react within our project, inside of whice is our React app.

Now in the new React folder find the pdfjs-viewer-react/src/index.js file and replace it contents with the following:

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import "./index.css";
const startApp = () => {
  ReactDOM.render(<App />, document.getElementById("root"));

if (!window.cordova) {
} else {
  document.addEventListener("deviceready", startApp, false);

In the pdfjs-viewer-react/package.json file, above the dependencies add the following line:

"homepage": "../www",

In the same file within the scripts: {} object replace the line that has build as it's key with:

"build": "react-scripts build && robocopy .\\build ..\\www /MIR",

replacing robocopy with cp-r if you are working with a Mac/Linux enviroment.

To run the project run the following set of commands

cd pdfjs-viewer-react
npm run build
cd ../
cordova build
cordova run browser

This will build the React app copy that build into the pdfjs-viewer/www folder, then we build the Cordova project and run it with the browser platform. If the builds worked correctly, when you navigate to http://localhost:8000/ you should see:

Step 4 - Implementing PDF.js

We will now integrate the open-source PDF.js library into our project to render a PDF inside our app. We will start by downloading the latest stable release from GitHub and then extracting the contents into a new pdfjs-viewer/pdfjs-viewer-react/public/lib folder. Make sure this is the public folder within the pdfjs-viewer-react directory not pdfjs-viewer.

We will also need a PDF file to view, which we will place in the web folder. You can use your own or download one from here.

The new file structure in our project folder will look like the following:

├── public
│    ├── lib
│    │   ├── ui
│    │   │   ├── ...
│    │   ├── core
│    │   │   ├── ...
│    │   └── webviewer.min.js
│    ├── favicon.ico
│    ├── index.html
│    ├── logo192.png
│    ├── logo512.png
│    ├── manifest.json
│    ├── robots.text
│    └── ...
└── ...

Step 5 - Creating the viewer

Next we will create a PDF.js viewer component within our React project pdfjs-viewer-react. To start, we will create a new folder pdfjs-viewer-react/src/components in this new folder we will create a new file called PdfjsViewer.js. Within this file, we will add the following code:

import React from 'react'

const PdfJSViewer = ({ height, width, url }) => {
    const path = 'lib/web/viewer.html'
    let pdfPath = url ? path + '?file=' + url : path;
    return (
        <div style={{ width, height }}>
export default PdfJSViewer

Also within the existing file pdfjs-viewer-react\src\App.js we will replace all the existing code with:

import React from 'react';
import PdfJsViewer from './components/PdfJsViewer'
import './App.css';
function App() {
  return (
    <div  className="App">
      <PdfJsViewer height='100vh' />
export default App;

Now if we run our set of build commands again:

cd pdfjs-viewer-react
npm run build
cd ../
cordova build
cordova run browser

We should see the default PDF.js viewer running at http://localhost:8000/index.html.

Step 6 - Customizing the PDF.js Toolbar

As a final step, we will reorganize the toolbar by moving elements around, removing buttons, and changing the icons.

Let’s open public/lib/web/viewer.html and add the following to the <head> section:

<script src="customToolbar.js"></script>

Next, we’ll create customToolbar.js inside the public/lib/web folder and add the following code:

let sheet = (function() {
  let style = document.createElement(&quot;style&quot;);
  return style.sheet;
 function editToolBar(){
  //when the page is resized, the viewer hides and move some buttons around.
  //this function forcibly show all buttons so none of them disappear or re-appear on page resize

  /* Reorganizing the UI */
  // the &#39;addElemFromSecondaryToPrimary&#39; function moves items from the secondary nav into the primary nav
  there are 3 primary nav regions (toolbarViewerLeft, toolbarViewerMiddle, toolbarViewerRight)

  //adding elements to left part of toolbar
  addElemFromSecondaryToPrimary(&#39;pageRotateCcw&#39;, &#39;toolbarViewerLeft&#39;)
  addElemFromSecondaryToPrimary(&#39;pageRotateCw&#39;, &#39;toolbarViewerLeft&#39;)
  addElemFromSecondaryToPrimary(&#39;zoomIn&#39;, &#39;toolbarViewerLeft&#39;)
  addElemFromSecondaryToPrimary(&#39;zoomOut&#39;, &#39;toolbarViewerLeft&#39;)
  //adding elements to middle part of toolbar
  addElemFromSecondaryToPrimary(&#39;previous&#39;, &#39;toolbarViewerMiddle&#39;)
  addElemFromSecondaryToPrimary(&#39;pageNumber&#39;, &#39;toolbarViewerMiddle&#39;)
  addElemFromSecondaryToPrimary(&#39;numPages&#39;, &#39;toolbarViewerMiddle&#39;)
  addElemFromSecondaryToPrimary(&#39;next&#39;, &#39;toolbarViewerMiddle&#39;)
  //adding elements to right part of toolbar
  addElemFromSecondaryToPrimary(&#39;secondaryOpenFile&#39;, &#39;toolbarViewerRight&#39;)
  /* Changing icons */
  changeIcon(&#39;previous&#39;, &#39;icons/baseline-navigate_before-24px.svg&#39;)
  changeIcon(&#39;next&#39;, &#39;icons/baseline-navigate_next-24px.svg&#39;)
  changeIcon(&#39;pageRotateCcw&#39;, &#39;icons/baseline-rotate_left-24px.svg&#39;)
  changeIcon(&#39;pageRotateCw&#39;, &#39;icons/baseline-rotate_right-24px.svg&#39;)
  changeIcon(&#39;viewFind&#39;, &#39;icons/baseline-search-24px.svg&#39;);
  changeIcon(&#39;zoomOut&#39;, &#39;icons/baseline-zoom_out-24px.svg&#39;)
  changeIcon(&#39;zoomIn&#39;, &#39;icons/baseline-zoom_in-24px.svg&#39;)
  changeIcon(&#39;sidebarToggle&#39;, &#39;icons/baseline-toc-24px.svg&#39;)
  changeIcon(&#39;secondaryOpenFile&#39;, &#39;./icons/baseline-open_in_browser-24px.svg&#39;)

  /* Hiding elements */
 function changeIcon(elemID, iconUrl){
  let element = document.getElementById(elemID);
  let classNames = element.className;
  classNames = elemID.includes(&#39;Toggle&#39;)? &#39;toolbarButton#&#39;+elemID :
 classNames.split(&#39; &#39;).join(&#39;.&#39;);
  classNames = elemID.includes(&#39;view&#39;)? &#39;#&#39;+elemID+&#39;.toolbarButton&#39; : &#39;.&#39;+classNames
  classNames+= &quot;::before&quot;;
  addCSSRule(sheet, classNames, `content: url(${iconUrl}) !important`, 0)
 function addElemFromSecondaryToPrimary(elemID, parentID){
  let element = document.getElementById(elemID);
  let parent = document.getElementById(parentID); = &quot;0px&quot;;
  element.innerHTML =&#39;&#39;
 function removeElement(elemID){
  let element = document.getElementById(elemID);
 function removeGrowRules(){
  addCSSRule(sheet, &#39;.hiddenSmallView *&#39;, &#39;display:block !important&#39;);
  addCSSRule(sheet, &#39;.hiddenMediumView&#39;, &#39;display:block !important&#39;);
  addCSSRule(sheet, &#39;.hiddenLargeView&#39;, &#39;display:block !important&#39;);
  addCSSRule(sheet, &#39;.visibleSmallView&#39;, &#39;display:block !important&#39;);
  addCSSRule(sheet, &#39;.visibleMediumView&#39;, &#39;display:block !important&#39;);
  addCSSRule(sheet, &#39;.visibleLargeView&#39;, &#39;display:block !important&#39;);
 function addCSSRule(sheet, selector, rules, index) {
  if(&quot;insertRule&quot; in sheet) {
  sheet.insertRule(selector + &quot;{&quot; + rules + &quot;}&quot;, index);
  else if(&quot;addRule&quot; in sheet) {
  sheet.addRule(selector, rules, index);
 window.onload = editToolBar

The PDF.js primary toolbar is broken down into 3 regions:

Screenshot: PDF.js Viewer Primary Toolbar Regions

The secondary toolbar is accessed via the chevron icon in the right region:

Screenshot: PDF.js Viewer Secondary Toolbar

We can move elements from the secondary toolbar into the left, middle, or right regions of the primary toolbar with the addElemFromSecondaryToPrimary function in customToolbar.js. For example, uncommenting this line will move the counter-clockwise rotation tool to the left region of the primary toolbar:

  addElemFromSecondaryToPrimary(&#39;pageRotateCcw&#39;, &#39;toolbarViewerLeft&#39;)

If you wanted to move pageRotateCcw to the middle region instead, you’d replace toolbarViewerLeft with toolbarViewerMiddle, or toolbarViewerRight for the right region. To move a different tool, replace the pageRotateCcw ID with the element ID you want to move. (See below for a full list of element IDs.)

We can also hide elements like this:


To hide different elements, replace print or download with the element ID.

NOTE: Hiding the download and print buttons is not a bulletproof way to protect our PDF, because it’s still possible to look at the source code to find the file. It just makes it a bit harder.

We can also customize the icons for various tools by swapping out the SVG file, like this:

changeIcon(&#39;previous&#39;, &#39;icons/baseline-navigate_before-24px.svg&#39;)

In the above example, previous is the element ID, while icons/baseline-navigate_before-24px.svg is the path to the tool icon.

And that’s it!

Element ID Reference for PDF.js User Interface Customization

Here’s handy reference with the IDs of the various toolbar icons:

Toolbar IconID
PDF Viewer UI Icon: sidebarToggle
PDF Viewer UI Icon: viewFind
PDF Viewer UI Icon: pageNumber
PDF Viewer UI Icon: numPages
PDF Viewer UI Icon: zoomOut
PDF Viewer UI Icon: zoomIn
PDF Viewer UI Icon: next page
PDF Viewer UI Icon: previous page
PDF Viewer UI Icon: presentationMode
PDF Viewer UI Icon: openFile
PDF Viewer UI Icon: print
PDF Viewer UI Icon: download
PDF Viewer UI Icon: viewBookmark
PDF Viewer UI Icon: secondaryToolbarToggle
PDF Viewer UI Icon: scaleSelectContainer
PDF Viewer UI Icon: secondaryPresentationMode
PDF Viewer UI Icon: secondaryOpenFile
PDF Viewer UI Icon: secondaryPrint
PDF Viewer UI Icon: secondaryDownload
PDF Viewer UI Icon: secondaryViewBookmark
PDF Viewer UI Icon: firstPage
PDF Viewer UI Icon: lastPage
PDF Viewer UI Icon: pageRotateCw
PDF Viewer UI Icon: pageRotateCcw
PDF Viewer UI Icon: cursorSelectTool
PDF Viewer UI Icon: cursorHandTool
PDF Viewer UI Icon: scrollVertical
PDF Viewer UI Icon: scrollHorizontal
PDF Viewer UI Icon: scrollWrapped
PDF Viewer UI Icon: spreadNone
PDF Viewer UI Icon: spreadOdd
PDF Viewer UI Icon: spreadEven
PDF Viewer UI Icon: documentProperties


As you can see, rendering a PDF with Cordova isn't difficult using open-source libraries.

Building a PDF Viewer with Cordova is relatively straightforward, but once you want to start annotating, signing, or filling forms, you would have to implement these things yourself. See our PDF.js Build vs Buy and Guide to Evaluating PDF.js to learn more.

That’s where PDF.js Express comes in. It’s a commercial PDF.js viewer that wraps a React-based UI around the open-source PDF.js rendering engine and offers out-of-the-box features like annotations, form filling and e-signatures. It’s fully compatible with the Cordova framework -- check out the demo, and let us know what you think!

If you need high-fidelity rendering, increased reliability, and faster performance, you could consider PDFTron WebViewer. It’s a JavaScript PDF library that integrates with the Cordova framework and offers hundreds of features, like redaction, editing, page manipulation, real-time document collaboration, digital signatures, and much more. Check out the WebViewer demo.

If you have any questions about implementing PDF.js Express in your project, please contact us and, we will be happy to help!