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:
Qt Code:
  1. #include <QApplication>
  2.  
  3. #include <QPrinter>
  4. #include <QPrintDialog>
  5. #include <QFileDialog>
  6. #include <QTextDocument>
  7.  
  8. /*---------------------------------------------------------------------------*/
  9. void printReportStr(QString str)
  10. {
  11.  
  12. QPrinter printer;
  13. printer.setOrientation(QPrinter::Portrait);
  14. printer.setOutputFormat(QPrinter::NativeFormat);
  15. printer.setPaperSize(QPrinter::Letter);
  16. printer.setOutputFileName(NULL);
  17.  
  18. QPrintDialog prnDlg(&printer, NULL);
  19.  
  20. if (prnDlg.exec() != QDialog::Accepted)
  21. return;
  22.  
  23.  
  24. doc.setHtml(str);
  25. doc.print(&printer);
  26. }
  27.  
  28. /*---------------------------------------------------------------------------*/
  29. void saveAsPDF(QString fileName, QString str)
  30. {
  31.  
  32. QPrinter printer;
  33. printer.setOrientation(QPrinter::Portrait);
  34. printer.setOutputFormat(QPrinter::PdfFormat);
  35. printer.setPaperSize(QPrinter::Letter);
  36.  
  37. QString path = QFileDialog::getSaveFileName(NULL,
  38. "Save as PDF...", fileName,
  39. "PDF Files (*.pdf)");
  40. if (path.isEmpty())
  41. return;
  42.  
  43. printer.setOutputFileName(path);
  44.  
  45.  
  46. doc.setHtml(str);
  47. doc.print(&printer);
  48. }
  49.  
  50. /*---------------------------------------------------------------------------*/
  51. int main(int argc, char *argv[])
  52. {
  53. QApplication a(argc, argv);
  54.  
  55. QString str;
  56.  
  57. str = "<html>" "<head>" "</head>"
  58. "<body>"
  59. "<h3 align=center>Centered h3 Header</h3>"
  60. "</body>"
  61. "</html>";
  62.  
  63.  
  64.  
  65. // The following saves as a PDF-file. Using Acrobat Reader,
  66. // it is formatted and displays correctly, and centered
  67. //
  68. saveAsPDF("myPDFfile", str);
  69.  
  70.  
  71.  
  72. // The following dumps the same thing to a printer, and it prints
  73. // correctly EXCEPT for the centering. In fact, I have done all
  74. // manner of HTML code-tags and styles... changing fonts, colors,
  75. // sizes, etc, and everything is correct except for the horizontal
  76. // centering. It is always left-aligned.
  77. //
  78. printReportStr(str);
  79.  
  80.  
  81. exit(0);
  82.  
  83. return a.exec();
  84. }
To copy to clipboard, switch view to plain text mode 

And here is the .pro file:
Qt Code:
  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2014-02-19T19:00:37
  4. #
  5. #-------------------------------------------------
  6.  
  7. QT += core gui printsupport widgets
  8.  
  9. TARGET = HTMLPrintDemo
  10. CONFIG += console
  11. CONFIG -= app_bundle
  12.  
  13. TEMPLATE = app
  14.  
  15.  
  16. SOURCES += main.cpp
To copy to clipboard, switch view to plain text mode