pdf.js: Unable to open default pdf in file:/// protocol when code embedded in qrc protocol (Qt)

Hi there,

I’m trying to embeed the last version of PDF.js viewer in my custom application using Qt webkit. I added the build version of PDF.js to my qrc file, and succeed to load the viewer.html.

The issue I have is when loading the pdf file which is using “file” scheme, it complains about cross origin problem:

XMLHttpRequest cannot load file:///H:/Downloads/8R23987019162_18.pdf. Cross origin requests are only supported for HTTP.

If I do load the PDF.js viewer.html from my file system and not from the resources (qrc), it works fine. Of course for my customer, it’s necessary to embeed to the PDF viewer in my executable.

According to the Qt webkit documentation (http://qt-project.org/doc/qt-5/qwebsecurityorigin.html) it should simply works : "By default local schemes like file:// and qrc:// are concidered to be in the same security origin, and can access each other’s resources. ". It was also working on Qt4 with an older version of PDF.js (that’s why I’m updating it).

I tried to add some addAccessWhitelistEntry() without any success.

Here is my constructor that inherits from QWebView :

    page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
    page()->settings()->setAttribute(QWebSettings::AcceleratedCompositingEnabled, true);
    page()->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
    page()->settings()->setAttribute(QWebSettings::LocalContentCanAccessFileUrls, true);
    page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);

    // Object that contains path
    page()->mainFrame()->addToJavaScriptWindowObject("customPdfJsObject",m_loader,QWebFrame::QtOwnership);

    page()->mainFrame()->load( QUrl("qrc:/pdf.js/web/viewer.html"));

The only change I made to viewer .js is :

var DEFAULT_URL = customPdfJsObject.myPath;

What is also strange is that if I use the Open Menu inside the viewer and select the same file on disk, it open correctly.

Thanks for your help.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 22 (8 by maintainers)

Most upvoted comments

@BoobalanK @sivashankararumugam this issue a bit old, but it seems to be still an open question. So here is my solution:

In the viewer.js there is a function webViewerOpenFileViaURL, that checks, if the file starts with file:// and then loads the content via XHR and uses the blob stuff from the open method to load the file.

You could make this function somehow public and directly call it, or you use the idea to write your own function like this:

	function openXHRBlob(file) {
		PDFViewerApplication.setTitleUsingUrl(file);
		var xhr = new XMLHttpRequest();
		xhr.onload = function () {
			PDFViewerApplication.open(new Uint8Array(xhr.response));
		};
		try {
			xhr.open('GET', file);
			xhr.responseType = 'arraybuffer';
			xhr.send();
		} catch (ex) {
			throw ex;
		}
	}

You can call this function from anywhere to open local PDFs without restrictions. Also tested under WebEngineView QML Item.

Hi @sivashankararumugam , Exaclty same situation here Did you acheived that (loading pdf file from local folder)? Please can you share the idea.

Thanks in advance

Using Xamarin forms for UWP (windows phone 10) I’m facing same issue in loadind the pdf file form local folder eg: ms-appx-web:///Assets/pdfjs/web/viewer.html?file=C:\Users\username\Desktop\pdf-sample.pdf.

error : PDF.js v1.8.186 (build: 32e01cda) Message: Missing PDF “file:///C:/Users/username/Desktop/pdf-sample.pdf”. Actually I need to display the pdf file that is downloaded in local folder.

Path is taken from "string filepath = ApplicationData.Current.LocalFolder.Path + “//Prajesh.pdf”;

can anyone share your idea

Thanks in advance