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