Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PDF creation issue
Forum Updated to NodeBB v4.3 + New Features

PDF creation issue

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 751 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • HoMaH Offline
    HoMaH Offline
    HoMa
    wrote on last edited by
    #1

    Hi there,
    every now and then someone complains about PDF Creation - now I am one of them (maybe not the first time).
    This time my issue is twofold:

    • PDF output is so much different then the html it was created with and
    • it differes from PC to PC ...
      If someone wants to take a look: https://github.com/Schachigel/QtPdfHtmlTest
      But I will have most of the code also here: How I create a pdf and an html file from a given html and a css file:
        const QString html =fileToString ("../in.html");
        const QString css =fileToString( "../in.css");
    
        QPrinter printer;
        // set A4
        QPageLayout pl =printer.pageLayout ();
        pl.setPageSize (QPageSize(QPageSize::A4));
        printer.setPageLayout (pl);
    
        QTextDocument doc;
        // adjust to printer pagesizse
        doc.setPageSize (pl.pageSize ().size (QPageSize::Unit::Point));
    
        doc.setDefaultStyleSheet (css);
        doc.setHtml (html);
    
        printer.setOutputFileName ("../out.pdf");
        doc.print(&printer);
    
        stringToFile (doc.toHtml (), "../out.html");
    

    My sample css and html:

    body { 
        width:210mm; 
        height:297mm;
        font-size: 9pt;
        font-family: Verdana, Geneva, Tahoma, sans-serif;
    }
    .C1 { font-size: 7pt; }
    .C2 { font-size: 10pt; }
    .C3 { font-size: 14pt; }
    .C4 { font-size: 28pt; }
    
    <head>
        <link rel="stylesheet" href="in.css">
    </head>
    <body>
    Normal Text <small>Small text</small> <br>
    <div class="C1">Seven Point is pretty small <br> </div>
    <div class="C2">Ten Point is close to normal size <br> </div>
    <div class="C3">Fourteen Point - like headline <br> </div>
    <div class="C4">HUGE Point size<br></div>
    </body>
    

    How the original and the created html in a browser look:
    Win11.in_vs_out.png

    How the pdf looks:
    pdf_output.png

    Font sizes are quite off - and even more the spacing. As if there was a paragraph everywhere!

    Thank you for having a look!
    PS: my qt version is 5.15.2 on Windows. I thried this on 2 computers. The outcome differs - which is a problem on its own - but is bad on both. Same symptoms.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      QTextDocument accepts a limited subset of HTML, probably only a well-formed example, converts it to an equivalent internal structure and is not guaranteed to render this precisely the same way as any browser.

      The HTML 4 specification says of DIV elements that, "Visual user agents generally place a line break before and after DIV elements, ..." Your markup is specifically inserting a line break, and the DIV (block) element may be triggering another. If you inspect the text document structure you might find extra, empty blocks in the result after import.

      The font may vary from machine to machine because of what fonts are available and whether any are subject to aliasing/substitution. Your markup allows the rendering engine to choose an available font matching one of four options and, as on the web, the result may differ by consumer. On a standard Windows 8, 10, or 11 box you should find Verdana or Tahoma, but the fonts themselves have changed between these Windows versions.

      HTML is not a page description language. If you want better control then use QTextDocument directly to construct the document. If you want a more browser-like result then use a browser component and render it from there.

      1 Reply Last reply
      1
      • HoMaH Offline
        HoMaH Offline
        HoMa
        wrote on last edited by
        #3

        Thx Chris - that is a great answer and it will take some time to work me through ;)

        Your last advice ...
        HTML is not a page description language. If you want better control then use QTextDocument directly to construct the document. If you want a more browser-like result then use a browser component and render it from there.
        is so true! However - do you know any alternatives? My aim is to create letters in my qt app. They are made from some html templates wihch are "enriched" by some database data (using Mustache). With html templates I can give (limited) freedom to the user, to change the layout. If I do it on the QTextDocument everything is hard coded (or very complicated to implement).
        best regards

        1 Reply Last reply
        0
        • HoMaH Offline
          HoMaH Offline
          HoMa
          wrote on last edited by
          #4

          PS: using <span instead of <div solved the new line issue! That is great! Tank you again!
          Now I am "only" fighting the actual font sizes in pdf.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            There is Qt WebEngine, specifically QWebEnginePage::printToPdf().
            This example may help: WebEngine Widgets Html2Pdf Example

            JonBJ 1 Reply Last reply
            0
            • C ChrisW67

              There is Qt WebEngine, specifically QWebEnginePage::printToPdf().
              This example may help: WebEngine Widgets Html2Pdf Example

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @ChrisW67
              I know @HoMa is on Windows. He needs to be aware that Qt WebEngine stuff is only available under Windows if Qt/his app is compiled with MSVC, not MinGW, in case that is relevant.

              HoMaH 1 Reply Last reply
              0
              • JonBJ JonB

                @ChrisW67
                I know @HoMa is on Windows. He needs to be aware that Qt WebEngine stuff is only available under Windows if Qt/his app is compiled with MSVC, not MinGW, in case that is relevant.

                HoMaH Offline
                HoMaH Offline
                HoMa
                wrote on last edited by
                #7

                @JonB good hint - thank you. Fortunately I use MSVC. I guess the WebEnginge is available on Linux and Mac? I will definitly give it a shot - even so I am afraight, that this will be a big junk of additional software to install and memory to use - just for the printing.
                But if it solves the other issues. ... hard disks and memory have become cheap ;)

                HoMaH 1 Reply Last reply
                0
                • HoMaH HoMa

                  @JonB good hint - thank you. Fortunately I use MSVC. I guess the WebEnginge is available on Linux and Mac? I will definitly give it a shot - even so I am afraight, that this will be a big junk of additional software to install and memory to use - just for the printing.
                  But if it solves the other issues. ... hard disks and memory have become cheap ;)

                  HoMaH Offline
                  HoMaH Offline
                  HoMa
                  wrote on last edited by
                  #8

                  the example app of WebEngine to create PDFs from HTML has > 230 mb of dependencies (created with WinDeployqt) - hat is heavy! Maybe this is too much to be added to my 40 or so mb application :(
                  But the results seem to look better then with QTextDocument ...

                  JonBJ 1 Reply Last reply
                  0
                  • HoMaH HoMa

                    the example app of WebEngine to create PDFs from HTML has > 230 mb of dependencies (created with WinDeployqt) - hat is heavy! Maybe this is too much to be added to my 40 or so mb application :(
                    But the results seem to look better then with QTextDocument ...

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #9

                    @HoMa
                    Indeed, that is a payload you must consider. WebEngine is a full (chromium) web browser, and is large, but gives the best PDF. QTextDocument is much lighter, but is more limited in the layout/PDF it can produce. Your choice....

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved