I write an application, when user inserts data in a dialog window (document title, sender name and address, etc) and then my application should generate a pdf file from this user data.
PDF file should have defined layout, something like this:
I tried to do this with QPdfWriter
but have problems aligning text in pdf. Here's my code:
#include <QApplication> #include <QtCore> #include <QPrinter> #include <QPdfWriter> #include <QPainter> QString currDate() { QDate date = QDate::currentDate(); return date.toString("dd.MM.yyyy"); } void pdf(QString filename) { QPdfWriter writer(filename); writer.setPageSize(QPagedPaintDevice::A4); writer.setPageMargins(QMargins(30, 30, 30, 30)); QPainter painter(&writer); painter.setPen(Qt::black); painter.setFont(QFont("Times", 10)); QRect r = painter.viewport(); QString citydate = "City, "; citydate += currDate(); painter.drawText(r, Qt::AlignRight, citydate); QString sender = "COMPANY XYZ\n"; sender += "Random Street 12/314A\n"; sender += "123-1232 City\n"; painter.drawText(r, Qt::AlignLeft, sender); painter.end(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); pdf("example1.pdf"); return a.exec(); }
Date printed to pdf is on the left, but:
translate
method of the painter enough, or can it be done simpler?)Thanks
EDIT
I also tried the QTextDocument
approach, but its hard to write any document, with almost any example available on the web. I came up only with this:
void pdf(QString filename) { QPrinter printer(QPrinter::PrinterResolution); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); printer.setOutputFileName(filename); printer.setPageMargins(QMarginsF(30, 30, 30, 30)); QFont headerFont("Times New Roman", 8); QFont titleFont("Times New Roman", 14, QFont::Bold); QTextCharFormat txtformat = QTextCharFormat(); QTextDocument doc; doc.setPageSize(printer.pageRect().size()); QTextCursor* cursor = new QTextCursor(&doc); txtformat.setFont(headerFont); cursor->insertText("Company XYZ", txtformat); cursor->movePosition(QTextCursor::Right && QTextCursor::EndOfLine, QTextCursor::KeepAnchor, 1000); cursor->insertText(currDate(), txtformat); doc.print(&printer); }
For creating PDF documents from scratch, you can use Qt's built-in print support which also allows "printing" to PDF files. To do so you can set up a QPrinter instance like this: QPrinter printer(QPrinter::HighResolution); printer. setOutputFormat(QPrinter::PdfFormat); printer. setOutputFileName("path/to/file.
There is several ways to create a PDF document in Qt. You already mentioned two of them. I propose some improvement for QTextDocument
approach. Rather than manually writing a QTextDocument
, you can create it from HTML-formatted text.
QString html = "<div align=right>" "City, 11/11/2015" "</div>" "<div align=left>" "Sender Name<br>" "street 34/56A<br>" "121-43 city" "</div>" "<h1 align=center>DOCUMENT TITLE</h1>" "<p align=justify>" "document content document content document content document content document content document content document content document content document content document content " "document content document content document content document content document content document content document content document content document content document content " "</p>" "<div align=right>sincerly</div>"; QTextDocument document; document.setHtml(html); QPrinter printer(QPrinter::PrinterResolution); printer.setOutputFormat(QPrinter::PdfFormat); printer.setPaperSize(QPrinter::A4); printer.setOutputFileName("/tmp/test.pdf"); printer.setPageMargins(QMarginsF(15, 15, 15, 15)); document.print(&printer);
Warning: QTextDocument
support a limited subset of HTML 4 markup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With