#include <QtGui>
int main(int argc, char **argv)
{
content.append("here my html");
td.setHtml(content);
//p.setPageMargins(20, 20, 5, 20, QPrinter::Millimeter);
bool pdf = true; //use pdf as output?
if (pdf) {
"PDF files (*.pdf)");
if (outFile.isEmpty())
exit(1);
if (!outFile.contains(".pdf"))
outFile.append(".pdf");
p.setOutputFileName(outFile);
} else {
pd.exec();
}
td.setPageSize(p.pageRect().size());
QRect innerRect
= p.
pageRect();
innerRect.setTop(innerRect.top() + 20);
innerRect.setBottom(innerRect.bottom() - 30);
int count = 0;
painter.save();
painter.translate(0, 30);
while (currentRect.intersects(contentRect)) {
td.drawContents(&painter, currentRect);
count++;
currentRect.translate(0, currentRect.height());
painter.restore();
painter.
drawText(10,
10,
QString("Header %1").
arg(count
));
painter.
drawText(10, p.
pageRect().
bottom() - 10,
QString("Footer %1").
arg(count
));
painter.save();
painter.translate(0, -currentRect.height() * count + 30);
if (currentRect.intersects(contentRect))
p.newPage();
}
painter.restore();
painter.end();
return 0;
}
#include <QtGui>
int main(int argc, char **argv)
{
QApplication a(argc, argv);
QString content;
content.append("here my html");
QTextDocument td;
td.setHtml(content);
QPrinter p;
//p.setPageMargins(20, 20, 5, 20, QPrinter::Millimeter);
bool pdf = true; //use pdf as output?
if (pdf) {
QString outFile = QFileDialog::getSaveFileName(0, "Save PDF file",
QDir::homePath(),
"PDF files (*.pdf)");
if (outFile.isEmpty())
exit(1);
if (!outFile.contains(".pdf"))
outFile.append(".pdf");
p.setOutputFileName(outFile);
} else {
QPrintDialog pd(&p, 0);
pd.exec();
}
td.setPageSize(p.pageRect().size());
QRect innerRect = p.pageRect();
innerRect.setTop(innerRect.top() + 20);
innerRect.setBottom(innerRect.bottom() - 30);
QRect contentRect = QRect(QPoint(0,0), td.size().toSize());
QRect currentRect = QRect(QPoint(0,0), innerRect.size());
QPainter painter(&p);
int count = 0;
painter.save();
painter.translate(0, 30);
while (currentRect.intersects(contentRect)) {
td.drawContents(&painter, currentRect);
count++;
currentRect.translate(0, currentRect.height());
painter.restore();
painter.drawText(10, 10, QString("Header %1").arg(count));
painter.drawText(10, p.pageRect().bottom() - 10, QString("Footer %1").arg(count));
painter.save();
painter.translate(0, -currentRect.height() * count + 30);
if (currentRect.intersects(contentRect))
p.newPage();
}
painter.restore();
painter.end();
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks