Results 1 to 8 of 8

Thread: Problem loading .pdf file from qrc

  1. #1
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem loading .pdf file from qrc

    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 :
    Qt Code:
    1. ........
    2. txt = new QWebView(this);
    3. QUrl url("qrc:/file/00101.htm"); //THE .HTM FILE LOADS FLAWLESSLY
    4. txt->load(url);
    5.  
    6. Poppler::Document *document = Poppler::Document::load("qrc:/file/money.pdf"); // DOESEN'T WORK IF I LOAD FROM RESOURCE
    7.  
    8. Poppler::Document *document = Poppler::Document::load("C:/Users/el33t/Documents/QT EXPERIMENTS/Pro1/file/money.pdf"); // WORKS FLAWLESSLY IF I LOAD IT EXTERNALLY.
    To copy to clipboard, switch view to plain text mode 

    Any help would be greatly appreciated.

    Regards,

  2. #2
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem loading .pdf file from qrc

    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.

  3. #3
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem loading .pdf file from qrc

    Quote Originally Posted by DanH View Post
    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.

  4. #4
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem loading .pdf file from qrc

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem loading .pdf file from qrc

    Quote Originally Posted by el33t View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem loading .pdf file from qrc

    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

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem loading .pdf file from qrc

    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.

  8. The following user says thank you to ChrisW67 for this useful post:

    el33t (28th August 2011)

  9. #8
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem loading .pdf file from qrc

    Quote Originally Posted by ChrisW67 View Post
    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:ocument::loadFromData().
    • Copy the file out of resources into a temporary file and then open that with Poppler:ocument::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 :

    Qt Code:
    1. f.setFileName(":/file/money.pdf");
    2. f.open(QIODevice::ReadOnly);
    3. QByteArray pdf=f.readAll();
    4.  
    5.  
    6. Poppler::Document *document = Poppler::Document::loadFromData(pdf);
    To copy to clipboard, switch view to plain text mode 


    Regards...

Similar Threads

  1. Loading a qmldir from a qrc file
    By bunjee in forum Qt Quick
    Replies: 7
    Last Post: 17th January 2017, 21:09
  2. Loading a TabLib .a File into project
    By Atais in forum Newbie
    Replies: 6
    Last Post: 9th February 2011, 22:22
  3. QML file not loading external JS file?
    By anothertest in forum Qt Programming
    Replies: 2
    Last Post: 14th April 2010, 09:11
  4. External Javascript file not loading initially
    By bkudrle in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2009, 17:05
  5. OpenGL file loading formats
    By TheKedge in forum Qt Programming
    Replies: 0
    Last Post: 6th August 2008, 15:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.