Results 1 to 6 of 6

Thread: QWebView problem with pdf

  1. #1
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default QWebView problem with pdf

    Hi,

    The following code results in an empty pdf file whereas when I show() the component, the html code displays correctly.
    Any idea?

    Here is the source:

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3. #include <QtWebKit>
    4.  
    5.  
    6. int main(int argc, char *argv[]) {
    7. QApplication app(argc, argv, 1);
    8.  
    9. QPrinter *qPrinter = new QPrinter(QPrinter::HighResolution);
    10. qPrinter->setOutputFormat(QPrinter::PdfFormat);
    11. qPrinter->setOutputFileName("out.pdf");
    12.  
    13. QFile file("in.txt");
    14. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    15. return 1;
    16.  
    17. QString text;
    18. while (!file.atEnd()) {
    19. text += file.readLine();
    20. }
    21.  
    22. QWebView *qWebView = new QWebView();
    23. qWebView->setMinimumSize(1024,768);
    24. qWebView->show();
    25. qWebView->setHtml(text);
    26. qWebView->show();
    27. qWebView->print(qPrinter);
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

    The file in.txt contains only:
    Qt Code:
    1. <html>
    2. <body
    3. helloWorld
    4. </body>
    5. </html>
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help.
    Best regards,
    Oscar

  2. #2
    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: QWebView problem with pdf

    Try setting the viewport size of the associated web page.

  3. #3
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default Re: QWebView problem with pdf

    Thank you wysota for your reply.

    I tried:
    qWebView->page()->setViewportSize(QSize(2000,2000));
    before
    qWebView->print(qPrinter);

    But the result is the same...Grrrr

    Maybe I've got something wrong in my config.
    Do you get a valid pdf with this example? (I'm testing it under Vista)

    Best regards,
    Oscar

  4. #4
    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: QWebView problem with pdf

    I didn't try. But you don't need a web view widget. All you need is QWebPage with its mainFrame where you can set the content using setHtml().

  5. #5
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default Re: QWebView problem with pdf

    The problem is the same with the use of a QWebPage class...

    Here is the code:

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3. #include <QtWebKit>
    4.  
    5.  
    6. int main(int argc, char *argv[]) {
    7. QApplication app(argc, argv, 1);
    8.  
    9. QPrinter *qPrinter = new QPrinter(QPrinter::HighResolution);
    10. qPrinter->setOutputFormat(QPrinter::PdfFormat);
    11. qPrinter->setOutputFileName("out.pdf");
    12.  
    13. QString text = "<html> <body> coucou <br>coucou br>coucou <br>coucou <br>coucou <br>coucou <br>coucou </body> </html>";
    14.  
    15. QWebPage *qWebPage = new QWebPage();
    16. qWebPage->mainFrame()->setHtml(text);
    17. qWebPage->setViewportSize(QSize(2000,2000));
    18. qWebPage->mainFrame()->print(qPrinter);
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

    Any idea of what I'm missing?

    Best regards,
    Oscar

  6. #6
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default Re: QWebView problem with pdf

    Well, I've found a html->pdf converter that's working.

    I was missing the following line:

    Qt Code:
    1. connect(qWebView, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
    To copy to clipboard, switch view to plain text mode 

    Now, everything is ok.

    Best regards,
    Oscar
    Last edited by oscar; 23rd August 2008 at 17:12.

  7. The following user says thank you to oscar for this useful post:

    Cupidvogel (11th February 2016)

Similar Threads

  1. Problem With QWebView
    By ivi2501 in forum Qt Programming
    Replies: 8
    Last Post: 2nd August 2009, 20:37
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 10:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 09:47
  4. QWebView unknown problem
    By morfei in forum Qt Programming
    Replies: 1
    Last Post: 5th August 2008, 01:12
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

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.