Results 1 to 7 of 7

Thread: QPrinter print QWebView content without any any page break.

  1. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default QPrinter print QWebView content without any any page break.

    I have a HTML document which consists of a succession of divs, some of which may contain images. I want to export the document as pdf. For this I am using QPrinter and QWebView combination. It prints fine, but but pages are 'broken` at regular intervals, mostly whenever page is about to exceed A4 size. In contrast, say if I use phantomjs and rasterize.js combo, it prints out the entire HTML in one long single page in the PDF document.

    How do I achieve the same in QPrinter as well? I tried adding media queries like page-break-inside: avoid, doesn't work.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter print QWebView content without any any page break.

    Are you currently using QWebView::print()?

    Have you tried just rendering instead? I.e. QWebFrame::render()?

    Cheers,
    _

  3. #3
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: QPrinter print QWebView content without any any page break.

    Not really. I have no use for QPainter. But nevertheless, I have fixed the problem. For anyone having the same problem, here is a solution. Define a script in your HEAD tags in the HTML content with getters for page height and width:

    Qt Code:
    1. <script LANGUAGE="JavaScript">
    2. function getHeight ()
    3. {
    4. return window.document.body.scrollHeight;
    5. }
    6. function getWidth ()
    7. {
    8. return window.document.body.scrollWidth;
    9. }
    10. </script>
    To copy to clipboard, switch view to plain text mode 

    Now in the code where you get the page content from the webview after loadFinished signal has been emitted, try this:

    Qt Code:
    1. QWebView* view = dynamic_cast<QWebView *>(sender());
    2. QVariant heightVariant = view->page()->mainFrame()->evaluateJavaScript("getHeight()");
    3. QVariant widthVariant = view->page()->mainFrame()->evaluateJavaScript("getWidth()");
    4. QPrinter *printer = new QPrinter();
    5. printer->setOutputFileName("<path to pdf>");
    6. printer->setOutputFormat(QPrinter::PdfFormat);
    7. printer->setOrientation(QPrinter::Portrait);
    8. printer->setPaperSize(QSize(widthVariant.toInt(),heightVariant.toInt()),QPrinter::Point);
    9. view->page()->mainFrame()->print(printer);
    To copy to clipboard, switch view to plain text mode 

    That solves the problem.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter print QWebView content without any any page break.

    Quote Originally Posted by Cupidvogel View Post
    Not really. I have no use for QPainter.
    Not sure what you mean, you don't use QPainter yourself, but simpy hande it to render().

    print() takes a QPrinter, so it knows that this is a paged device and will try to create a new page when the page size is reached.
    It also is limited to QPrinter so you can't use QPdfWriter.

    render() works on QPainter and doesn't care which paint device that works on, so it can't call "next page" and the paint device doesn't have to be a printer.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Cupidvogel (11th February 2016)

  6. #5
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: QPrinter print QWebView content without any any page break.

    Oh okay, I did not think along those lines. I just saw the parameters include a QPainter object, and here I simply pass a HTML file to the QPrinter, so though it will not suit my needs.

  7. #6
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: QPrinter print QWebView content without any any page break.

    While we are on this, is it possible to add table of contents in sidebar where each entry will point to a certain section in the pdf?

    And secondly, is it possible to use an image as cover image which displays as thumbnail when the file is rendered on the desktop/Finder, instead of a condensed view of the entire document showing up as thumbnail image? The problem is that my content is extremely long, and like I said, rendered only in a single page, so the thumbnail becomes kind of ugly, like this:

    Screen Shot 2016-02-12 at 3.14.59 AM.png

    Whereas a nice cover image thumbnail, irrespective of content, would look like this :

    Screen Shot 2016-02-12 at 3.17.30 AM.png
    Last edited by Cupidvogel; 11th February 2016 at 21:51.

  8. #7
    Join Date
    May 2018
    Posts
    1
    Qt products
    Platforms
    Windows

    Arrow Re: QPrinter print QWebView content without any any page break.

    Quote Originally Posted by Cupidvogel View Post
    (...) in the code where you get the page content from the webview after loadFinished signal has been emitted, try this:

    Qt Code:
    1. QWebView* view = dynamic_cast<QWebView *>(sender());
    2. QVariant heightVariant = view->page()->mainFrame()->evaluateJavaScript("getHeight()");
    3. QVariant widthVariant = view->page()->mainFrame()->evaluateJavaScript("getWidth()");
    4. QPrinter *printer = new QPrinter();
    5. printer->setOutputFileName("<path to pdf>");
    6. printer->setOutputFormat(QPrinter::PdfFormat);
    7. printer->setOrientation(QPrinter::Portrait);
    8. printer->setPaperSize(QSize(widthVariant.toInt(),heightVariant.toInt()),QPrinter::Point);
    9. view->page()->mainFrame()->print(printer);
    To copy to clipboard, switch view to plain text mode 

    That solves the problem.
    Hey! Could you help me to convert this code to Python? First three lines a bit confusing for me.. All the rest is fine, I know how to do it.. but first three

    UPDATE: well, no need in it, I've got it... But you wasn't fix it, yeah the rows and images are not breaks anymore, but the page is not breakes itself, so it is just a looong page in a height now....
    Last edited by mclawest; 21st May 2018 at 03:40.

Similar Threads

  1. How to print 2 QWebView on the same QPrinter
    By miraks in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2013, 08:21
  2. QWebView and css page break property
    By haldrik in forum Qt Programming
    Replies: 0
    Last Post: 31st October 2012, 02:57
  3. Replies: 1
    Last Post: 24th March 2011, 16:40
  4. Replies: 1
    Last Post: 7th July 2009, 16:46
  5. Replies: 1
    Last Post: 5th July 2009, 10:10

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.