Qt printing using html and QTextDocument
-
I fight with printing pdf from QTextDocument object with html format. I generate QString formatted code of html page and it works fine. To test it i make using QFile a html file which have to be printed and it look greats.
But pdf file looks completly different and i don't know why.Code:
QString html = getHtmlPrintString(FVToPrint, currentCustomer); QTextDocument document; document.setHtml(html); QFile qHtmlFile("D:\\Qt\\projects\\FVGeneratorV2\\fv\\"+QString::fromStdString(FVToPrint.getFVId()) +".html"); if(qHtmlFile.open(QIODevice::WriteOnly)) { qHtmlFile.write(html.toUtf8()); qHtmlFile.close(); } QPrinter printer(QPrinter::PrinterResolution); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPageSize(QPageSize::A4); QString fvname = "D:\\Qt\\projects\\FVGeneratorV2\\fv\\" + QString::fromStdString(FVToPrint.getFVId()) + ".pdf"; printer.setOutputFileName(fvname); printer.setPageMargins(QMarginsF(15, 15, 15, 15)); document.print(&printer);
Html file code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<!DOCTYPE html>
<html> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> <style type='text/css'> table, tr, td { border: 1px solid black; border-collapse: collapse; } td { padding: 1%; } html, body { th: 100%; height: 100%; margin: 0; padding: 0; } </style> </head> <body> <table style='width:100%; height:100%; border: 0px; margin:0; padding: 0;'> <tr> <td> <table style='float:left; border: 2px solid black; width:42%; margin-right:2%; margin-left:2%; padding:2%; height:5%;'> <tr> <td> <h2><b>FV_24.01.2022_666_1_73</b></h2> </td> </tr> </table> <table style='float:left; padding:2%; margin-right:2%; border: 2px solid black; width:42%; height:5%;'> <tr> <td>24.01.2022</td> </tr> </table> <table style='float:none; border-style:none;'></table> <table style='float:left; border: 2px solid black; width:42%; margin-right:2%; margin-top:1%; margin-left:2%; padding:2%; height:15%;'> <tr> <td>kunito<br> Nip: 8712314<br> Phone: 9123412<br> Email: mail@com<br>miejska 12<br>47821 krakow, Polska<br></td> </tr> </table> <table style='float:left; margin-top:1%; padding:2%; margin-right:2%; border: 2px solid black; width:42%; height:15%;'> <tr> <td>Customer: <br> Name: test <br> Nip: 12398401<br>miejska 27<br>81293 Krakow, Polska<br></td> </tr> </table> <table style='float:none; border-style:none;'></table> <table style=' margin: 1% 2% 0 2%; padding:2%; border: 2px solid black; width:90.5%; height:10%;'> <tr> <td>Payment data: <br>online transfer<br> Account number: 8912384<br> Title: FV_24.01.2022_666_1_73</td> </tr> </table> <table style='border-style:none; margin: 1% 2% 0 2%; padding:2%; width:90.5%; height:30%;'> <tr style='border-style:none;'> <td style='border-style:none;'> <table style='width:100%;'> <tr style='background-color:darkgray;'> <td>Nb.</td> <td>Name</td> <td>Um</td> <td>Quantity</td> <td>Price netto</td> <td>Vat</td> <td>Vat value</td> <td>Price brutto</td> </tr> <tr> <td>1</td> <td>Bulka</td> <td>pcs</td> <td>2</td> <td>2.024</td> <td>8</td> <td>0.176</td> <td>2.2</td> </tr> <tr style='background-color:darkgray;'> <td colspan='5'></td> <td>Netto sum</td> <td>Vat sum</td> <td>Sum</td> </tr> <tr> <td colspan='5'></td> <td>2.024</td> <td>0.176</td> <td>2.2</td> </tr> </table> </td> </tr> </table> <table style='border-style:none; margin: 1% 2% 0 2%; padding:2%; width:90.5%; height:5%;'> <tr> <td>To pay: 2.2 zl <br> Payed: 2.2 zl</td> </tr> </table> <table style='border-style:none; margin: 1% 2% 0 2%; padding:2%; width:90.5%; height:5%;'> <tr style='border-style:none;'> <td style='border-style:none;'> <table style='border-style:none; float:left; width: 20%; height:100%; '> <tr style='border-style:none;'> <td style='border-style:none;'> <p align='center'>............................<br>podpis pracownika</p> </td> </tr> </table> <table style='border-style:none; float:left; width: 20%; height:100%; margin-left:50%; '> <tr style='border-style:none;'> <td style='border-style:none;'> <p align='center'>............................<br>podpis klienta</p> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </body> </html>
[Pdf printed file][1]
[1]: https://i.stack.imgur.com/hEkT0.png
-
Hi and welcome to devnet,
Do you mean that if you have that document printed on paper it works correctly but not as a PDF ?
In any case, don't forget that QTextDocument does not support the full range of HTML capabilities.
-
@SGaist I mean i save file with QString html formatted text and when i open it in chrome it look as i want. Now i change div's to table because only table have border and float property in CSS Rich Text, but i still don't look as a html in web browser.
-
This remark I wrote is really important:
@SGaist said in Qt printing using html and QTextDocument:
In any case, don't forget that QTextDocument does not support the full range of HTML capabilities.
-
@SGaist i know it and i think that now i have everything that QTextDocument support, but it still look like width property didn't work correctly. Check it how it looks now because i edit the post. Only clear property is not supporting and i don't know what i should use instead of them. I tried float:none but it didn't work as i want. I edit post check how it looks now
-
IIRC, there should be no px after border.