Brian Brown
Brian Brown

Reputation: 4311

Create a PDF document for printing in Qt from a template

I write an application, when a 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.

The PDF file should have a defined layout, something like this:

enter image description here

I tried to do this with QPdfWriter, but I had problems aligning text in the 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();
}

The date printed to the 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)

I also tried the QTextDocument approach, but it's hard to write any document, with almost no 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);
}

Upvotes: 33

Views: 29603

Answers (1)

Meefte
Meefte

Reputation: 6735

There are several ways to create a PDF document in Qt. You already mentioned two of them. I propose some improvement for the 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 supports a limited subset of HTML 4 markup.

Upvotes: 28

Related Questions