Unable to open default pdf in file:/// protocol when code embedded in qrc protocol (Qt)
See original GitHub issueHi 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:
- Created 9 years ago
- Comments:22 (8 by maintainers)
Top GitHub 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 withfile://
and then loads the content via XHR and uses the blob stuff from theopen
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:
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