PDA

View Full Version : Problem loading .pdf file from qrc



el33t
27th August 2011, 01:48
Hi,

I have a qrc resource file which contains some image, htm and pdf file. I'm able to load the images and htm files perfectly without any problem, however whenever I try to load the PDF files(using poppler), I get the following error :

Couldn't open file 'qrc:/file/money.pdf' : Invalid argument "

HOWEVER,

When I open the PDF file from outside the resource, it works flawlessly.

Below is a code snippet of what I'm trying to do :

........
txt = new QWebView(this);
QUrl url("qrc:/file/00101.htm"); //THE .HTM FILE LOADS FLAWLESSLY
txt->load(url);

Poppler::Document *document = Poppler::Document::load("qrc:/file/money.pdf"); // DOESEN'T WORK IF I LOAD FROM RESOURCE

Poppler::Document *document = Poppler::Document::load("C:/Users/el33t/Documents/QT EXPERIMENTS/Pro1/file/money.pdf"); // WORKS FLAWLESSLY IF I LOAD IT EXTERNALLY.

Any help would be greatly appreciated.

Regards,

DanH
27th August 2011, 03:12
If the load operation is being done by the PDF viewer then likely it doesn't know how to access something from a QRC.

And putting large files in a QRC is a bad idea anyway -- it's really only intended for small files. Putting large files in a QRC increases the amount of main store occupied by your program, makes it load slower, and generally hoses things up.

el33t
27th August 2011, 07:54
And putting large files in a QRC is a bad idea anyway -- it's really only intended for small files. Putting large files in a QRC increases the amount of main store occupied by your program, makes it load slower, and generally hoses things up.

But then using resources is the only way(as far as I know) of 'hiding' my files so that people don't just leech them off when I release my program.

DanH
27th August 2011, 14:18
They can be placed in the private directory of the app. This isn't perfectly secure, but they're inaccessible to any on-phone tools.

wysota
27th August 2011, 14:25
But then using resources is the only way(as far as I know) of 'hiding' my files so that people don't just leech them off when I release my program.

Security through obscurity is never a good idea. And extracting this pdf file from your binary (or directly from memory) is pretty simple so if one really wants to do it, you won't stop him.

el33t
27th August 2011, 23:40
ummm... I already had a discussion regarding this...so let's just stick to the topic's purpose.

What's wrong with my program? Why isin't the pdf loading from the .qrc ?

Regards

ChrisW67
28th August 2011, 01:45
You have already been told why Poppler, which knows nothing about the Qt mechanism for embedding resources or the pseudo-filesystem used to access them, cannot open the embedded file using a pseudo file name.

If you really must have the PDF embedded then you could:

Use the Poppler API call that loads from data in-memory and use that after reading the resource into a QByteArray: Poppler::Document::loadFromData().
Copy the file out of resources into a temporary file and then open that with Poppler::Document::load()


Incidentally, you refer to a "file" in the resources as ":/path/to/file"... the "qrc:" scheme is for use in URLs.

el33t
28th August 2011, 05:44
You have already been told why Poppler, which knows nothing about the Qt mechanism for embedding resources or the pseudo-filesystem used to access them, cannot open the embedded file using a pseudo file name.

If you really must have the PDF embedded then you could:

Use the Poppler API call that loads from data in-memory and use that after reading the resource into a QByteArray: Poppler::Document::loadFromData().
Copy the file out of resources into a temporary file and then open that with Poppler::Document::load()


Incidentally, you refer to a "file" in the resources as ":/path/to/file"... the "qrc:" scheme is for use in URLs.


WHOA!!! I followed your advice of reading the pdf into a array and voila! it works!! Thanks A LOT!

Also, I'd like to thanks the rest others who participated in this thread.

Anyways, if anyone else facing a similar problem want to see what code modification/addition I did to resolve the issue, here is the snippet :


QFile f;
f.setFileName(":/file/money.pdf");
f.open(QIODevice::ReadOnly);
QByteArray pdf=f.readAll();


Poppler::Document *document = Poppler::Document::loadFromData(pdf);


Regards...