#include <QApplication>
#include <QPrinter>
#include <QPrintDialog>
#include <QFileDialog>
#include <QTextDocument>
/*---------------------------------------------------------------------------*/
{
printer.
setOrientation(QPrinter::Portrait);
printer.
setOutputFormat(QPrinter::NativeFormat);
printer.setOutputFileName(NULL);
if (prnDlg.
exec() != QDialog::Accepted) return;
doc.setHtml(str);
doc.print(&printer);
}
/*---------------------------------------------------------------------------*/
{
printer.
setOrientation(QPrinter::Portrait);
printer.
setOutputFormat(QPrinter::PdfFormat);
"Save as PDF...", fileName,
"PDF Files (*.pdf)");
if (path.isEmpty())
return;
printer.setOutputFileName(path);
doc.setHtml(str);
doc.print(&printer);
}
/*---------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
#-------------------------------------------------
#
# 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
#-------------------------------------------------
#
# 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
To copy to clipboard, switch view to plain text mode
Bookmarks