Progress will discontinue Telerik Platform on May 10th, 2018. Learn more
  • How to Open PDF Files Inside Your App Using PDF.js

How to Open PDF Files Inside Your App Using PDF.js

Environment

Service Code (AppBuilder), Third-party product
Mobile development type Hybrid (Cordova app)

Description

Opening a PDF in a hybrid app is possible out of the box only on iOS devices. For Android and Windows Phone, the device needs to have an external PDF viewer. An alternative approach is to use a JavaScript library, like PDF.js, to parse and render the PDF file inside the WebView. This article provides basic steps and a sample for opening a PDF file inside your app using PDF.js.

Solution

  1. Go to the PDF.js page and click Download.
  2. On the download page, click Stable.
  3. Extract the downloaded archive and copy the pdf.js and pdf.worker.js files from the build folder into a scripts/pdf.js/ folder in your project.
  4. Reference the pdf.js and pdf.worker.js files in your index.html.

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8" src="scripts/pdf.js/pdf.js"></script>
    <script type="text/javascript" charset="utf-8" src="scripts/pdf.js/pdf.worker.js"></script>
    
  5. Enable the File core plugin in your app.
  6. (Windows Phone only) Explicitly set the path to the pdf.worker.js file in your code:

        PDFJS.workerSrc = "scripts/pdf.js/pdf.worker.js";
    
  7. Use the File plugin to read the PDF file as a Uint8Array and pass it to the PDFJS.getDocument() function.
  8. Render the parsed PDF content into a canvas element on the page.

A working example is available here: PDF Create/Open sample app.

Notes

Steps 7 and 8 describe logic that includes code snippets, which are too long to present in a concise way in this article. You can refer to the scripts\open.js file in the PDF Create/Open sample app.

This example relies heavily on a third-party library. At the time of testing, it worked on all three operating systems supported by Telerik Platform. However, there is no guarantee that this code will continue to work seamlessly with future OS versions. It is recommended to test this functionality in your app with each release of a new OS version and each update of the PDF.js library that you decide to use.

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.