Solved PDF output from QPrinter cuts last letter of words if the html characters are generated with cyrillic support
-
I am creating a software for Windows 10 with QT 5.7. So, basically I generate a QString named htmltable that i fill with html. I use the function for PDF printing:
QPrinter printer; printer.setResolution(QPrinter::HighResolution); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(fileName); // printer.setFullPage(true); printer.setOrientation(QPrinter::Portrait); printer.setPaperSize(QPrinter::A4); QSizeF size = printer.paperSize(QPrinter::Point); QTextDocument doc; doc.setHtml(htmltable); doc.setPageSize(size); doc.print(&printer);`
The pages print fine if the characters in the words are latin. But if they are written with cyrillic support, the last character of some of the words is cut-off as seen on the picture bellow. This only happens if the font-size attribute is smaller for example 4pt. As you can see when the font is bigger there are no cut offs even on cyrillic characters.
Any suggestions?
-
How does it look on screen using a QTextDocument + QTextEdit ?
-
@mrjj Do you mean like this?
QTextEdit *edit = new QTextEdit(this); edit->setText(doc.toHtml()); edit->resize(1500,1748); edit->show(); edit->raise();
In the QTextEdit the letters are fine in cyrillic. So the problem is in the printer ? :((
-
Hi
Not sure yet. It could be the font when converted to "printer".
Could you now print it via QTextEdit ?
http://doc.qt.io/qt-5/qtextedit.html#print
Just to see if it then blows or what happening. -
@mrjj i try to print it like this:
QSizeF size = printer.paperSize(QPrinter::Point); QTextDocument doc; doc.setHtml(htmltable); doc.setPageSize(size); QTextEdit *edit = new QTextEdit(this); edit->setText(doc.toHtml()); edit->resize(size.toSize()); edit->show(); edit->raise(); // doc.print(&printer); edit->print(&printer);
but i get like 200 blank pdf pages and i know i should only get one. I think i am not setting the size correctly for the qtextedit
-
Ok, That's surprising as
QSizeF size = printer.paperSize(QPrinter::Point);
edit->resize(size);I thought it would get really huge ?
Did u check the value for size?Maybe try
QSizeF size = printer.paperSize(QPrinter::DevicePixel);
edit->resize(size);
And see what happens as it sounds like it TExtEdit got really small :) -
@mrjj I tried your code i got the same result. I remember experiencing this behaviour when i was using qtextdocument without setting the page size().
-
@mardzo
Ah, that sounds like thats the issue and it then will "page break" in other places as
expected.
Im wondering if the HTML code contains anything
"fixed size"-ish that dont scale so well between screen and printer DPI. -
@mrjj well the html table has a fixed width=440 and style = "margin-top:50px; margin-right:20px; " Im a bit new at QT and i dont understand how the latin letters are displayed correctly and cyrillic at the end of a word are the only one that are cut off
-
@mardzo
Yes that is a bit odd. But cyrillic is another font, i assume?
So it could react differently being upscaled/created for the printer context.
A printer has many more DPI than the screen so some scaling is involved as else
it would just be ultra tiny. ( when printed)
Could also be a bug in the font handling so the actual size of the cyrillic is reported incorrectly.Is it possivble to get the actual HTML that you print?
( so I can try it) -
@mrjj I create it dynamically so i cant send it all. I tried so save it all in a txt file, but the cyrilic characters are replaced with ??????. here is a small snippet with cyrilic characters in it. Im sorry for the bad formatting
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="qrichtext" content="1"> <title>QTextEdit Example</title> <style type="text/css">p, li { white-space: pre-wrap; } </style> </head> <body style=" font-family:Tahoma; font-size:9pt; font-weight:300; font-style:normal;"> <table align=center style = "margin-top:50px; margin-right:20px; "> <tr><th><h2>Извештај за настани од контрола на пристап</h2></th> </tr> </table> <table align=center style='margin-top:10px; margin-right:20px; '> <tr> <td>Филтер:</td> <td>Сите логови</td> <td>Од:</td> <td>28.11.2016</td> <td>До:</td> <td>28.11.2016</td> </tr> </table>" <table style = 'text-align: left; padding: 1px; background-color: #fff; font-size:6pt;' border=2 align=center cellspacing=1 width=440 > <tr> <th>Бр.</th> <th>Уред</th> <th>Лог</th> <th>Врата</th> <th>Корисник</th> <th>Датум и Време</th> </tr> </table> </body> </html>
-
Hi
Super.
I get this
Is that ok test case?
-
@mrjj yes, this is okay but what happens when you try to set style="font-size : 6pt;" on the table and export it as pdf? Does it cut the last letter of the cyrillic words? I edited the previously posted html
-
Hi
Super.
style="font-size : 6pt;"
As set stylesheet on it or?I will retry with the new html.
-
@mrjj yes, try with the exact new html i posted
-
@mardzo
Hmm seems to be 1-1 one
Can we try reverse and see if u get same?
https://www.dropbox.com/s/kxu7drheoulah3p/myTextEdit.zip?dl=0 -
@mrjj yes your project works fine i have tried it and got a good pdf :). But did u notice that when u input the html in the qtextedit in the ui it alters the html adding spans. paragrarphs etc.? Do u suggest for me to change my html table based on the changes the QT Designer made to it?
-
Bloody hell! I figured it out. The only thing i needed to do was to put <span></span> inside the table cells. Thanks @mrjj for your help.
-
@mardzo
Super. Good work. Well i was wondering what the difference was between screen
and pdf and you found it :)