Results 1 to 10 of 10

Thread: QPrinter prints different font size and region on different computers (printing PDF)

  1. #1
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPrinter prints different font size and region on different computers (printing PDF)

    Hey everyone,

    I'm trying and searching all the forums I know for days now, but I just can't find the problem with this one:

    Qt Code:
    1. QWebView *view = new QWebView();
    2. QString htmlPage = "<html> ..... </html>";
    3. view->setHtml(htmlPage);
    4.  
    5. QPrinter printer;
    6. printer.setOutputFormat(QPrinter::PdfFormat);
    7. printer.setOutputFileName(fileName);
    8. printer.setFullPage(true);
    9. printer.setOrientation(QPrinter::Portrait);
    10. printer.setPaperSize(QPrinter::A4);
    11. view->print(&printer);
    12. QDesktopServices::openUrl(QUrl("file:///" + fileName, QUrl::TolerantMode));
    To copy to clipboard, switch view to plain text mode 

    I'm simply trying to print an HTML-File to PDF that I previously parsed in a QWebView. The HTML should fill the whole page, which it does on the machine I'm developing on.
    But on some computers I tried my application, in the PDF either fonts appear printed a lot larger or the HTML is printed in an area much smaller than the actual page.
    My client is starting to get mad, cause on his computer the content only fills the uppler left quarter of the page and is too small to read...

    Am I doing something wrong here? Is there some kind of relative size setting that I missed? (setResolution doesn't show an effect) How can i specify the area in which the WebView should print to?

    Thank you so much for your advice, MisterIKS

  2. #2
    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: QPrinter prints different font size and region on different computers (printing P

    QTBUG-5363 might explain a small offset on Windows but not the scaling issue unless the user's default margins on the page are really broken.

    Have you tried explicitly setting QPrinter's margins to 0?

  3. #3
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter prints different font size and region on different computers (printing P

    Thanks for your reply, I would be fine with a small offset, but not with the content filling only half the page...
    Actually I just found out that the windows setting "Control Panel -> Appearance and Personalization -> Display -> Make text an other items larger of smaller" causes this problem when set to something different from 100%.

    There must be a way to force QPrinter not to care about windows settings and print the size I want?

  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: QPrinter prints different font size and region on different computers (printing P

    Does it change anything if you pass QPrinter::HighResolution to the constructor of the printer object?

    What about setting an explicit resolution with QPrinter::setResolution()?
    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.


  5. #5
    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: QPrinter prints different font size and region on different computers (printing P

    Changing the Windows display scaling to 125% or 150% does not affect printing the Google front page for me. The test code I used is below.

    What is the HTML you are loading? Is it well formed? Does it use styles with fixed widths? External CSS? Javascript?

    Qt Code:
    1. #include <QApplication>
    2. #include <QWebView>
    3. #include <QPrinter>
    4. #include <QDebug>
    5.  
    6. class MyWebView: public QWebView
    7. {
    8. Q_OBJECT
    9. public:
    10. MyWebView(QWidget *p = 0): QWebView(p) {
    11. connect(this, SIGNAL(loadFinished(bool)), SLOT(doPrint(bool)));
    12. }
    13. public slots:
    14. void doPrint(bool ok) {
    15. qDebug() << "doPrint" << ok;
    16. QPrinter printer;
    17. printer.setOutputFormat(QPrinter::PdfFormat);
    18. printer.setOutputFileName("zz.pdf");
    19. printer.setFullPage(true);
    20. printer.setOrientation(QPrinter::Portrait);
    21. printer.setPaperSize(QPrinter::A4);
    22. print(&printer);
    23. qDebug() << "doPrint done";
    24. }
    25. };
    26.  
    27.  
    28. int main(int argc, char **argv)
    29. {
    30. QApplication app(argc, argv);
    31. MyWebView web;
    32. web.setUrl(QUrl("http://www.google.com.au"));
    33. web.show();
    34. return app.exec();
    35. }
    36. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter prints different font size and region on different computers (printing P

    Thanks for your replies!

    Explicitly setting a resolution via constructor or setResolution() unfortunately doesn't change anything for me.

    I actually use fixed widths to 800px in my document for the body-tag. I did this as the printed size of my document otherwise depended on the actual content I'm adding dynamically to the HTML. I probably got something wrong here concerning the way Qt transforms the WebView into printed Document, but without fixed width and with the text getting longer in my document the printed fontsize got smaller to have the text fit in one line instead of doing line breaks, which I could only prevent by setting the width in CSS.

    However at some other point in my application I'm also printing PDFs directly with QPrinter and QPainter and without QWebView, which also results in larger fontsize when set to 125% (the layout stays intact here because I'm using fixed pixel values for positioning the text) ... does anybody experience this too? Thats why I still thing that this has more something to do with QPrinter but the HTML.

    Let me know if you need more information. I really hope to get this working until christmas

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPrinter prints different font size and region on different computers (printing P

    This may or may not be related, but on some of my printers web pages are printed very small also. The problem seems to be related to a printer setting, something like "scale to fit page". The default setting was to print without scaling, and that caused the microscopic output. Once the driver was set to "scale to fit", things were fine.

    I don't know if you can do that programmatically from Qt, but your client might be willing to change a few printer settings.

  8. #8
    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: QPrinter prints different font size and region on different computers (printing P

    Are you able to post a sample HTML file, and supporting images etc., that displays the problem?


    Further to d_stranz suggestion you may also like to check if the affected user has a default printer set at all, and if selecting a different printer as the default changes the behaviour.
    Last edited by ChrisW67; 21st December 2012 at 03:39.

  9. #9
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter prints different font size and region on different computers (printing P

    I thought that printing to PDF would be a Qt internal thing, does changing default printer and it's settings really change something? Anyway I'm currently testing on my PC an notebook which both run on Windows 7, both have the same one and only printer installed, the only difference is, that my notebook is set to 125% in windows scaling options because of the small screen.

    I now tried the following code on both notebook and pc which resulted in these two files:
    notebook_result.pdfpc_result.pdf

    Qt Code:
    1. <html><head><style type=\"text/css\">body{margin-top:0px; margin-left:20px; padding:0px 80px; font-family:Arial; font-size:16px;}</style></head><body>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</body></html>
    To copy to clipboard, switch view to plain text mode 

    I feel like it can't be a problem in my html, any other ideas?

  10. #10
    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: QPrinter prints different font size and region on different computers (printing P

    Quote Originally Posted by MisterIKS View Post
    I thought that printing to PDF would be a Qt internal thing, does changing default printer and it's settings really change something?
    It is, but when you create a default QPrinter it configures itself for the system default printer and then you change it to PDF and fiddle with the margins etc. I reasoned that if there is no default printer perhaps you get odd results.

    Your example HTML is missing <p> tags but browsers are tolerant of these things.

    I can reproduce the issue here using a longer form of the lorem ipsum, your style, 32-bit Win 7 and the MS document writer as the only printer. The text gets smaller as the display scaling value increases. Changing to font size specified as 16pt did not help.

    Try rendering some 16px Arial text on the QPrinter using a QPainter directly. If that also scales with the display scaling then the problem is outside QWebView... might help searching for (or reporting) a Qt bug.

Similar Threads

  1. Replies: 1
    Last Post: 30th March 2012, 16:46
  2. Printing QwtPlot on QPrinter
    By gkarthick5 in forum Qwt
    Replies: 5
    Last Post: 28th June 2011, 12:33
  3. Replies: 1
    Last Post: 12th August 2010, 15:19
  4. Replies: 3
    Last Post: 10th March 2010, 16:07
  5. QTextDocument printing - specify font size?
    By Scorp1us in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2007, 15:25

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.