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. How to add QStrings containing quotes to write a PDF

How to add QStrings containing quotes to write a PDF

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 522 Views
  • 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.
  • M Offline
    M Offline
    Marcus Barnet
    wrote on last edited by
    #1

    Hi to all,

    I'm trying to generate a PDF by using a HTML template.
    Unfortunately, my HTML template is very complex and it contains strings with quotation marks (i.e. <tr style="height: 21px;"></tr>) which are considered as errors by Qt Creator.

    For example, I'm using:

     document.setHtml(html);
    
    
        QPrinter printer(QPrinter::PrinterResolution);
        QPrintDialog dialog(&printer, this);
        if(dialog.exec() == QDialog::Accepted){
            printer.setOutputFormat(QPrinter::PdfFormat);
            printer.setPaperSize(QPrinter::A4);
            printer.setOutputFileName("C:/Users/39328/prova/scheda.pdf");
            printer.setPageMargins(QMarginsF(15, 15, 15, 15));
            QPrinterInfo pinfo(printer);
            qDebug() << "Printer valid: " << printer.isValid();
            qDebug() << "Printer Name: " << printer.printerName();
            qDebug() << "Printer program: " << printer.printProgram();
            qDebug() << "Is printer null: " << pinfo.isNull();
            qDebug() << "Printer State: " << pinfo.state();
            qDebug() << "Is printer default: " << pinfo.isDefault();
            qDebug() << "Is printer remote: " << pinfo.isRemote();
            qDebug() << "file path: " << printer.outputFileName();
           document.print(&printer);
        }
    

    where

    QString html =
             "<table align= \"center \" border= \"0 \" style= \"border-collapse: collapse; width: 21cm; height: 29 cm; \">"
             "<tbody>"
             "<tr>"
             "<td style= \"width: 100%; \">"
             "<table border= \"0 \" style= \"height: 146px; width: 100%; border-collapse: collapse; margin-left: auto; margin-right: auto; \">"
             "<tbody>"
             "<tr style= \"height: 0px; \">"
             "<td style= \"width: 50%; height: 146px; text-align: center; \" rowspan= \"4 \"><img width= \"200 \" align= \"center \" src= \"http://www.robo-dyne.com/pics/pump_logo.png \" alt= \" \" /></td>"
             "<td style= \"width: 50%; height: 115px; font-size: 45px; vertical-align: middle; text-align: right; \" rowspan= \"2 \"><strong><em>La Tua Palestra a Maglie</em></strong></td>"
             "</tr>"
             "<tr style= \"height: 21px; \"></tr>"
             "<tr style= \"height: 21px; \">"
             "<td style= \"width: 50%; height: 21px; vertical-align: middle; font-size: 15px; padding-left: 40px; \"><strong>TABELLA DI ALLENAMENTO DI</strong></td>"
             "</tr>"
             "<tr style= \"height: 10px; \">"
             "<td style= \"width: 50%; height: 10px; padding-left: 40px; vertical-align: middle; font-size: 15px; border: 0.1px solid black; \"><strong>ROCCO GALATI (0001)</strong></td>"
             "</tr>"
    .. and so on ..
    

    I added the slash as escape character, but the slash / isn't recognized as HTML code and so my template isn't displayed correctly.

    What can I do in order to load my HTML code into a QString?

    Thank you!

    Christian EhrlicherC 1 Reply Last reply
    0
    • M Marcus Barnet

      Hi to all,

      I'm trying to generate a PDF by using a HTML template.
      Unfortunately, my HTML template is very complex and it contains strings with quotation marks (i.e. <tr style="height: 21px;"></tr>) which are considered as errors by Qt Creator.

      For example, I'm using:

       document.setHtml(html);
      
      
          QPrinter printer(QPrinter::PrinterResolution);
          QPrintDialog dialog(&printer, this);
          if(dialog.exec() == QDialog::Accepted){
              printer.setOutputFormat(QPrinter::PdfFormat);
              printer.setPaperSize(QPrinter::A4);
              printer.setOutputFileName("C:/Users/39328/prova/scheda.pdf");
              printer.setPageMargins(QMarginsF(15, 15, 15, 15));
              QPrinterInfo pinfo(printer);
              qDebug() << "Printer valid: " << printer.isValid();
              qDebug() << "Printer Name: " << printer.printerName();
              qDebug() << "Printer program: " << printer.printProgram();
              qDebug() << "Is printer null: " << pinfo.isNull();
              qDebug() << "Printer State: " << pinfo.state();
              qDebug() << "Is printer default: " << pinfo.isDefault();
              qDebug() << "Is printer remote: " << pinfo.isRemote();
              qDebug() << "file path: " << printer.outputFileName();
             document.print(&printer);
          }
      

      where

      QString html =
               "<table align= \"center \" border= \"0 \" style= \"border-collapse: collapse; width: 21cm; height: 29 cm; \">"
               "<tbody>"
               "<tr>"
               "<td style= \"width: 100%; \">"
               "<table border= \"0 \" style= \"height: 146px; width: 100%; border-collapse: collapse; margin-left: auto; margin-right: auto; \">"
               "<tbody>"
               "<tr style= \"height: 0px; \">"
               "<td style= \"width: 50%; height: 146px; text-align: center; \" rowspan= \"4 \"><img width= \"200 \" align= \"center \" src= \"http://www.robo-dyne.com/pics/pump_logo.png \" alt= \" \" /></td>"
               "<td style= \"width: 50%; height: 115px; font-size: 45px; vertical-align: middle; text-align: right; \" rowspan= \"2 \"><strong><em>La Tua Palestra a Maglie</em></strong></td>"
               "</tr>"
               "<tr style= \"height: 21px; \"></tr>"
               "<tr style= \"height: 21px; \">"
               "<td style= \"width: 50%; height: 21px; vertical-align: middle; font-size: 15px; padding-left: 40px; \"><strong>TABELLA DI ALLENAMENTO DI</strong></td>"
               "</tr>"
               "<tr style= \"height: 10px; \">"
               "<td style= \"width: 50%; height: 10px; padding-left: 40px; vertical-align: middle; font-size: 15px; border: 0.1px solid black; \"><strong>ROCCO GALATI (0001)</strong></td>"
               "</tr>"
      .. and so on ..
      

      I added the slash as escape character, but the slash / isn't recognized as HTML code and so my template isn't displayed correctly.

      What can I do in order to load my HTML code into a QString?

      Thank you!

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Marcus-Barnet said in How to add QStrings containing quotes to write a PDF:

      I added the slash as escape character

      slash is no escape character, backslash is.
      You can also take a look at raw string literals - will help for sure here. Or read the string from a file instead trying to properly escape everything.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • M Offline
        M Offline
        Marcus Barnet
        wrote on last edited by Marcus Barnet
        #3

        Thank you for your suggestion.
        I'm still having problems and I already spent lots of hours trying to solve it with no success, unfortunately.
        I tried to read the file and load it into a QString:

         QString html;
            QFile file("C:/Users/39328/Documents/build-untitled1-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug/debug/scheda.txt");
                if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
                 qDebug() << "Problema! \n";
                    return;
                }
                QTextStream in(&file);
                html = in.readAll();
                file.close();
                qDebug() << html;
        
            document.setHtml(html);
        

        But even if the file contains only HTML, the QString outputs the strings with the backslashes.

        For example, the file contains:

        <table align="center" border="0" style="border-collapse: collapse; width: 21cm; height: 29 cm;"> <tbody> <tr> <td style="width: 100%;">
        

        The QString html contains:

        <table align=\"center\" border=\"0\" style=\"border-collapse: collapse; width: 21cm; height: 29 cm;\"> <tbody> <tr> <td style=\"width: 100%;\">
        

        this results in a non-correct HTML file.

        Why does this happen? It seems like the QString adds the backslashes automatically.

        I need to use the QString since I print it with:

         document.print(&printer);
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Marcus-Barnet said in How to add QStrings containing quotes to write a PDF:

          Why does this happen? It seems like the QString adds the backslashes automatically.

          QString does not add anything. You only see the debug output.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • M Offline
            M Offline
            Marcus Barnet
            wrote on last edited by Marcus Barnet
            #5

            Thank you for your reply and your support.

            You are definitely right, since I tried to read all the HTML content from the text file and then to save it as it is in another file and the content is the same without any backslash!

            However, the generated PDF is wrong formatted and I can't understand why!

            This is my current whole code:

                QString html;
                QFile file("C:/Users/39328/Documents/build-untitled1-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug/debug/scheda.txt");
                    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
                     qDebug() << "Problema! \n";
                        return;
                    }
                    QTextStream in(&file);
                    html = in.readAll();
                    file.close();
                    html.remove(QChar('\\'), Qt::CaseInsensitive);
                    qDebug() << html;
            
                    QString filename = "C:/Qt/MyFile.txt";
                    QFile file_out(filename);
                       // Trying to open in WriteOnly and Text mode
                       if (!file_out.open(QIODevice::ReadWrite | QIODevice::Text))
                       {
                           qDebug() << " Could not open file for writing: " << filename << "\n";
                           return;
                       }
            
                       QTextStream out(&file_out);
                       out << html;
                       file_out.flush();
                       file_out.close();
            
            
            
                    document.setHtml(html);
            
                QPrinter printer(QPrinter::PrinterResolution);
                QPrintDialog dialog(&printer, this);
                if(dialog.exec() == QDialog::Accepted){
                    printer.setOutputFormat(QPrinter::PdfFormat);
                    printer.setPaperSize(QPrinter::A4);
                    printer.setOutputFileName("C:/Users/39328/prova/scheda.pdf");
                    printer.setPageMargins(QMarginsF(15, 15, 15, 15));
                    QPrinterInfo pinfo(printer);
                    qDebug() << "Printer valid: " << printer.isValid();
                    qDebug() << "Printer Name: " << printer.printerName();
                    qDebug() << "Printer program: " << printer.printProgram();
                    qDebug() << "Is printer null: " << pinfo.isNull();
                    qDebug() << "Printer State: " << pinfo.state();
                    qDebug() << "Is printer default: " << pinfo.isDefault();
                    qDebug() << "Is printer remote: " << pinfo.isRemote();
                    qDebug() << "file path: " << printer.outputFileName();
                   document.print(&printer);
                }
            
            

            This is the HTML file generated with the content of QString html:

            scheda.PNG

            while this one if the generated PDF by using QString html:

            scheda_error.PNG

            What am I doing wrong?

            P.S. the HTML is formatted to fit in a standard A4 paper sheet.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              The first image you have is that rendered via browser?
              I think maybe the issue is that the HTML is using elements that Qt cannot handle.
              https://doc.qt.io/qt-5/richtext-html-subset.html
              Qt does not support full HTML spec in QTextDocument.
              Its only rich text.

              1 Reply Last reply
              1
              • M Offline
                M Offline
                Marcus Barnet
                wrote on last edited by
                #7

                Yes, the first picture comes from the Browser.
                I've read the linked documentation and it seems that some HTML elements I used to build the page are not supported.
                This is really sad since the main purpose for the QT application I'm developing is to print a HTML page by including some user data produced by the user in real time.

                mrjjM 1 Reply Last reply
                0
                • M Marcus Barnet

                  Yes, the first picture comes from the Browser.
                  I've read the linked documentation and it seems that some HTML elements I used to build the page are not supported.
                  This is really sad since the main purpose for the QT application I'm developing is to print a HTML page by including some user data produced by the user in real time.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Marcus-Barnet
                  Yeah, its doesnt seems overly fancy so would have been great if it was supported.
                  I know others have used webengine to print the pdf since its a full blown engine and can handle all html.
                  https://doc.qt.io/qt-5/qtwebengine-webenginewidgets-html2pdf-example.html

                  Note: QWebengine is not included with mingw compiler as it not supported by its HTML library,
                  So to go down this route, you will need to use Qt VS + visual studio compiler.

                  M 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @Marcus-Barnet
                    Yeah, its doesnt seems overly fancy so would have been great if it was supported.
                    I know others have used webengine to print the pdf since its a full blown engine and can handle all html.
                    https://doc.qt.io/qt-5/qtwebengine-webenginewidgets-html2pdf-example.html

                    Note: QWebengine is not included with mingw compiler as it not supported by its HTML library,
                    So to go down this route, you will need to use Qt VS + visual studio compiler.

                    M Offline
                    M Offline
                    Marcus Barnet
                    wrote on last edited by
                    #9

                    @mrjj said in How to add QStrings containing quotes to write a PDF:

                    @Marcus-Barnet
                    Yeah, its doesnt seems overly fancy so would have been great if it was supported.
                    I know others have used webengine to print the pdf since its a full blown engine and can handle all html.
                    https://doc.qt.io/qt-5/qtwebengine-webenginewidgets-html2pdf-example.html

                    Note: QWebengine is not included with mingw compiler as it not supported by its HTML library,
                    So to go down this route, you will need to use Qt VS + visual studio compiler.

                    Thank you for your suggestion, but I think it's too complicated to integrate webengine into my application :(

                    mrjjM 1 Reply Last reply
                    0
                    • M Marcus Barnet

                      @mrjj said in How to add QStrings containing quotes to write a PDF:

                      @Marcus-Barnet
                      Yeah, its doesnt seems overly fancy so would have been great if it was supported.
                      I know others have used webengine to print the pdf since its a full blown engine and can handle all html.
                      https://doc.qt.io/qt-5/qtwebengine-webenginewidgets-html2pdf-example.html

                      Note: QWebengine is not included with mingw compiler as it not supported by its HTML library,
                      So to go down this route, you will need to use Qt VS + visual studio compiler.

                      Thank you for your suggestion, but I think it's too complicated to integrate webengine into my application :(

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Marcus-Barnet
                      Well thats understandable.
                      I used SVG files for such cases. ( rich reports / pages )
                      but i have a feeling that you must use HTML and in that case, you must stick to the HTML supported.

                      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