Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add QStrings containing quotes to write a PDF



  • 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!


  • Lifetime Qt Champion

    @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.



  • 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);
    

  • Lifetime Qt Champion

    @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.



  • 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.


  • Lifetime Qt Champion

    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.



  • 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.


  • Lifetime Qt Champion

    @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.



  • @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 :(


  • Lifetime Qt Champion

    @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.


Log in to reply