Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create PDF document for printing in Qt from template

Tags:

c++

pdf

qt

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:

enter image description here

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:

  1. I have trouble with further text - how to "move" painter to different locations, to print also the sender name, document title, and document content inside the page (is the translate method of the painter enough, or can it be done simpler?)
  2. I don't know how to handle page breaks, in case the document content will be very long (will spread on 2 or more pages)

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); } 
like image 789
Brian Brown Avatar asked Nov 11 '15 15:11

Brian Brown


People also ask

How do you create a PDF in Qt?

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.


1 Answers

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.

like image 83
Meefte Avatar answered Sep 18 '22 06:09

Meefte