When writing business web applications you often need to show PDF files in your application. But this requires that the user has an installation of a PDF viewer (e.g. Adobe Reader) and the browser plugin is activated. With PDF.js there is a chance to provide reliable and consistent PDF viewing capabilities in your application. This post provides some background information and shows how to integrate the PDF.js viewer into your own application.
About PDF.js
PDF.js, mainly developed by Mozilla, provides a JavaScript library that makes it possible to render PDF files in a browser without using a browser plugin. This library does the rendering but isn’t responsible for providing any other functionality to the user like navigation, zoom levels or printing.
Additionally, there’s a complete viewer (implemented using html, CSS and JavaScript) that does the things mentioned above. A demonstration of this viewer is hosted on the project’s web page.
As PDF.js uses many technologies provided by modern browsers only, it doesn’t work with old browsers like Internet Explorer 8. For details on that you can refer to the project’s compatibility list.
License
PDF.js is licensed under the “Apache License, Version 2.0” that makes it possible to use it in your own application.
In accordance to end users, if you’re finding Unsuccessful to load PDF doc information in Chrome, the issue could possibly be your content material options. By default, Chrome is set to open PDF information internally, but you could possibly be in a position to circumvent this issue only by choosing to open PDF information in a devoted PDF viewer. I tried Firefox, Chrome, and Safari and the PDF opened in each. Are you looking at the page while logged in and is the template debugger on? I’m wondering if the debugging output is somehow adding stuff to the pdf. Or some other output is being added to the pdf? ← Failed to Load PDF Document. (5) The document does not conform to the requested standard. The file format (header, trailer, objects, xref, streams) is corrupted. The document contains device-specific color spaces. The document contains transparency. The document's meta data is either missing or inconsistent or corrupt.
Be aware that the PDF.js viewer also includes files of the Adobe CMap project. These files are available under the terms of a different license.
Limitations
PDF.js is implemented in JavaScript and runs in a browser. This results in some limitations, so don’t expect every feature of PDF files to be supported or working correctly. But in my experience PDF files containing text and images only work very well.
Getting the PDF.js viewer
The README file of PDF.js describes how to build a version on your own.
You can also take a pre-built version from the “gh-pages” branch of the GitHub project (the branch can be downloaded as zip file). But be aware that this version isn’t minified and therefore not as efficient as possible. If using the version from the “gh-pages” branch, you’ll need to put the folders “web” (excluding the example PDF file) and “build” into your own web application. These are only static files that need to be downloaded by the browser, so no special server-side technology is needed.
Necessary adjustments
On the top of the file web/viewer.js
you can adjust several settings. One setting is the default PDF file that is shown if no other file is specified. This is set by default to a value that is useful for the demo hosted on the project’s web page. You should change this to a reasonable value or about:blank
to show nothing by default.
If you want the pdf.js
and pdf.worker.js
files to be in a different location than the build
folder, you have to adjust two things:
The pdf.js
file is included in the web/viewer.html
file:
The pdf.worker.js
is included in the web/viewer.js
file:
Showing PDF files using the PDF.js viewer
This is my favorite part as it is almost trivial… Simply open the web/viewer.html
with a parameter file
set to the URL of your PDF file. An example URL could look like this:
As you simply show an html file, you are free to embed the PDF.js viewer as IFrame, popup or opening it as new window/browser tab.
Some more thoughts
As current versions of Google Chrome and Mozilla Firefox already natively support showing PDF files, it could be unnecessary to force the usage of the self-hosted PDF.js viewer as this results in additional files to be downloaded. As the PDF viewer included in Firefox is based on the PDF.js viewer, the user potentially won’t even see the difference. So a valid option to provide best and efficient support for the users is to use the built-in PDF viewer of the browser if you are sure one is available and only use PDF.js for uncertain cases (e.g. users with Internet Explorer).
But it could also be desirable to use PDF.js on all browsers to ensure a consistent look and behaviour for all users.
If you need to support Internet Explorer 8, you should provide a fallback for those users. For example you could use an IFrame directly pointing at the PDF’s URL. If the user has a PDF viewer, it will be used. If not the file can be downloaded by the user as last fallback.
Microsoft Edge is not just the default internet browser, but it is also the default PDF viewer in Windows 10. By default, all PDF files are opened with Microsoft Edge.
Very often Windows 10 users complain about Microsoft Edge not opening PDF files. If you are also using Edge as the default PDF reader but unable to open PDF files using Edge, following solutions might help you fix the issue.
We are confident that one of these solutions will fix your Edge not opening PDF files issue in Windows 10.
Failed To Load Pdf File
Restart the Edge browser
If you have not tried this yet, please reboot your PC once before trying out other solutions mentioned below as restarting the browser fixes issues at times.
Try to open another PDF file
Microsoft Edge might be failing to open the PDF file because the PDF file is corrupted or damaged. Try opening another PDF file before proceeding to solutions mentioned below.
If you don’t have another PDF file, you can also try opening the same PDF file in another web browser such as Chrome or Firefox to make sure that the PDF file is not corrupt.
Check if you are running Windows 10 N or KN editions
By default, Windows 10 N and Windows 10 KN editions don’t support opening PDF files in Edge as these editions don’t come with Windows Media features. Without Windows Media features installed, Edge browser cannot open PDF files, according to this page of Microsoft.
To check the edition, right-click on This PC icon in File Explorer navigation pane and then click Properties.
So, if you are on N or KN edition of Windows 10, please download and install Windows 10 Media Features Pack from Microsoft.
Reset Edge as default PDF viewer
At times, setting another program/app as the default PDF viewer and then setting Edge as default PDF reader again helps to fix the issue. Here is how to do that:
Step 1:Open Settings. Navigate to Apps > Default apps.
Step 2: Click Choose default apps by file type link.
Failed To Load Pdf Document Chrome File
Step 3: Here, locate the “.PDF” entry. Click on the Edge entry next to “.PDF”, and select any other program or app as the default PDF reader.
Step 4: Finally, click on the current default app or program, and then select Edge again. That’s it! Try opening the PDF file in Edge now.
Failed To Load Pdf Document In Chrome
Repair the Edge browser
If the PDF file that you are attempting to open is in perfect shape and you are not running N/KN editions, or you are running N/KN edition by installing Windows Media Features Pack, you can try repairing the Edge browser to fix the issue.
Repairing Edge browser in Windows 10 is easy. You can refer to our how to repair the Edge browser in Windows 10 guide for step-by-step directions with images.
Reset Microsoft Edge to fix Edge not opening PDF files
If Repairing the Edge browser didn’t help, you can try resetting the Edge browser. Repairing the Edge browser is also very easy. Follow the easy instructions in how to reset the Edge browser in Windows 10 guide to reset the Edge quickly.
Download and install Microsoft Reader app
There is an official PDF reader app available in the Windows Store. You can download and install Reader app from Microsoft to open PDF files. If you are looking for alternatives, please refer to our 4 PDF readers for Windows 10 guide.
To make the new program/app as the default reader, follow our how to change the default PDF reader in Windows 10 guide.
Hope you find this guide helpful!