question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
uap-universecommented, Jun 27, 2018

@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.

1reaction
BoobalanKcommented, Oct 9, 2017

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Thread: Problem loading .pdf file from qrc - Qt Centre Forum
Hi, I have a qrc resource file which contains some image, htm and pdf file. I'm able to load the images and htm...
Read more >
The Qt Resource System - Qt for Python
By default, rcc embeds the resource files into executables in the form of C++ arrays. This can be problematic especially for large resources....
Read more >
Adobe Acrobat X Pro cannot be set as default progr...
Solved: Hi. Adobe Acrobat X Pro cannot be set as default program on my computer to open .pdf files. I am using Windows...
Read more >
open embedded pdf resource files in C++ using default ...
External programs can't access the PDF if it's embedded inside your .exe. You'll need to extract it to a temporary directory before passing...
Read more >
Qt Developer's Guide - BlackBerry QNX
the files containing the Qt source code to any location and examine their contents; however, you can't modify or rebuild the sample apps ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found