从模板创建用于在Qt中打印的PDF文档

时间:2015-11-11 15:35:30

标签: c++ qt pdf

我写一个应用程序,当用户在对话框窗口中插入数据(文档标题,发件人姓名和地址等),然后我的应用程序应该从这个用户数据生成一个pdf文件。

PDF文件应该有定义的布局,如下所示:

enter image description here

我尝试使用QPdfWriter执行此操作但在pdf中对齐文本时遇到问题。这是我的代码:

#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();
}

打印到pdf的日期在左侧,但是:

  1. 我在进一步的文字方面遇到了麻烦 - 如何“移动”画家 不同的位置,打印发件人姓名,文件标题, 和页面内的文档内容(画家的translate方法是否足够,还是可以更简单?)
  2. 我不知道如何处理分页符,以防文档内容很长(会在2页或更多页上传播)
  3. 由于

    修改

    我也尝试了QTextDocument方法,但很难编写任何文档,几乎可以在网上找到任何示例。我只想到这个:

    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);
    }
    

1 个答案:

答案 0 :(得分:20)

有几种方法可以在Qt中创建PDF文档。你已经提到了其中两个。我建议对QTextDocument方法进行一些改进。您可以使用HTML格式的文本创建它,而不是手动编写QTextDocument

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);

警告:QTextDocument支持{4}的HTML 4标记。