PDA

View Full Version : [SOLVED] QTextDocument setHtml and HTML Tags/Properties "align=center" problems



jjones11
20th February 2014, 03:00
Hello all. My first post here. I am having a devil of a time with something that ought to be simple.

I am trying to generate a simple text Report. It is one page only. I build and format the Report using HTML from the "Supported HTML Subset". I offer a "Save Report to PDF" Button, and a "Print Report" Button. I have played with font families, font sizes, styles, colors, etc. All the HTML codes that I have tried are working perfectly except for horizontal alignment in the Print-out. The PDF file is properly aligned, as verified by Acrobat Reader.

I have pulled my hair out for a week trying various things, eating up a few hundred sheets of paper, trying to get the print-out to align. I humbly challenge someone to please provide the trick that makes the Print-out align properly.

As shown in the overly-simplified, yet complete and compilable code below, I assign the HTML codes to a QString as demonstrated in many different examples throughout the web. The posted code demonstrates the problem. The PDF file is correct, the Print output is not.

Since all other HTML Tags and Properities are working (I haven't included all of them in the simplified code), it appears to me that the "align=center" issue must have a trick to it, but if so, I think I would have found reference on the web to it. I have tried many other methods, all to no avail, which is why I would like to challenge others to see if they get the same result. Please indicate what I'm doing wrong, or leaving out a step.

I am using Qt 5.2.0, Qt Creator 3.0.0, Windows 7 Pro 64bit, and the MSVC2010 32-bit Debug Kit.

Thank You for your help.

The following is main.cpp:



#include <QApplication>

#include <QPrinter>
#include <QPrintDialog>
#include <QFileDialog>
#include <QTextDocument>

/*---------------------------------------------------------------------------*/
void printReportStr(QString str)
{

QPrinter printer;
printer.setOrientation(QPrinter::Portrait);
printer.setOutputFormat(QPrinter::NativeFormat);
printer.setPaperSize(QPrinter::Letter);
printer.setOutputFileName(NULL);

QPrintDialog prnDlg(&printer, NULL);

if (prnDlg.exec() != QDialog::Accepted)
return;


QTextDocument doc;
doc.setHtml(str);
doc.print(&printer);
}

/*---------------------------------------------------------------------------*/
void saveAsPDF(QString fileName, QString str)
{

QPrinter printer;
printer.setOrientation(QPrinter::Portrait);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::Letter);

QString path = QFileDialog::getSaveFileName(NULL,
"Save as PDF...", fileName,
"PDF Files (*.pdf)");
if (path.isEmpty())
return;

printer.setOutputFileName(path);


QTextDocument doc;
doc.setHtml(str);
doc.print(&printer);
}

/*---------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QString str;

str = "<html>" "<head>" "</head>"
"<body>"
"<h3 align=center>Centered h3 Header</h3>"
"</body>"
"</html>";



// The following saves as a PDF-file. Using Acrobat Reader,
// it is formatted and displays correctly, and centered
//
saveAsPDF("myPDFfile", str);



// The following dumps the same thing to a printer, and it prints
// correctly EXCEPT for the centering. In fact, I have done all
// manner of HTML code-tags and styles... changing fonts, colors,
// sizes, etc, and everything is correct except for the horizontal
// centering. It is always left-aligned.
//
printReportStr(str);


exit(0);

return a.exec();
}


And here is the .pro file:


#-------------------------------------------------
#
# Project created by QtCreator 2014-02-19T19:00:37
#
#-------------------------------------------------

QT += core gui printsupport widgets

TARGET = HTMLPrintDemo
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

ChrisW67
20th February 2014, 03:59
I can confirm that this code does the right thing under Linux with Qt 5.2.0.

With MSVC 2010 Qt 5.1.1 the program does the right thing with the "Microsoft XPS Writer" virtual printer.
Ditto for MingW and Qt5.1.1.
I do not have Qt 5.2.0 on this machine or a "real" printer attached. I used A4 paper size (otherwise exactly your code).

Have you tried using "Microsoft XPS Writer" as your printer to eliminate a printer driver curiosity?
Have you tried an earlier Qt version to eliminate a Qt 5.2-ism?

BTW: Thank you for taking the time to ask a smart question.

jjones11
20th February 2014, 04:47
Thank you ChrisW for your informative response. As per your recommendation, I tried printing to Microsoft XPS Writer and got the same left-aligned header instead of being centered. I also tried printing to a second/different printer and got the same result.

It is interesting that it works fine on your Linux platform. Hopefully, someone with a MSVC 2010/Qt5.2.0 development environment will respond.

I will try the MingW Kit (that's a good idea) and if that doesn't reveal anything, then I will take your recommendation and bite off an earlier Qt 5.1.1. version.

Thanks again.
jjones

jjones11
20th February 2014, 16:14
Thank you ChrisW for the ideas. I tried the MingW Kit and got the same results.

I upgraded to Qt 5.2.1 (couldn't find 5.1.1) and now it works correctly... and I didn't have to change a thing!

This problem is a big fat SOLVED! I am greatly relieved.

Thanks again.
jjones

P.S. I don't see a way to add "SOLVED" to the Title because apparently I can't edit the original post. If some Admin would please...