QT5 生成pdf的一段代码:
QPdfWriter +QPainter 原贴地址: 已经改编过
QString fileName="d:/aaa.pdf"; QFile pdfFile(fileName); pdfFile.open(QIODevice::WriteOnly); // 打开要写入的pdf文件 QPdfWriter* pPdfWriter = new QPdfWriter(&pdfFile); // 创建pdf写入器 pPdfWriter->setPageSize(QPagedPaintDevice::A4); // 设置纸张为A4 pPdfWriter->setResolution(300); // 设置纸张的分辨率为300,因此其像素为3508X2479 int iMargin = 60; // 页边距 pPdfWriter->setPageMargins(QMarginsF(iMargin, iMargin, iMargin, iMargin)); QPainter* pPdfPainter = new QPainter(pPdfWriter); // qt绘制工具 // 标题,居中 QTextOption option(Qt::AlignHCenter | Qt::AlignVCenter); option.setWrapMode(QTextOption::WordWrap); // 标题上边留白 int iTop = 100; // 文本宽度2100 int iContentWidth = 2100; // 标题,22号字 QFont font; font.setFamily("simhei.ttf"); //font.setFamily("黑体"); int fontSize = 22; font.setPointSize(fontSize); pPdfPainter->setFont(font); // 为绘制工具设置字体 //pPdfPainter->drawText(QRect(0, iTop, iContentWidth, 90), // QString::fromLocal8Bit("我的标题我骄傲"), option); pPdfPainter->drawText(QRect(0, iTop, iContentWidth, 90),"我的标题我骄傲", option); // 内容,16号字,左对齐 fontSize = 16; font.setPointSize(fontSize); pPdfPainter->setFont(font); option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter); iTop += 60; pPdfPainter->drawText(QRect(0, iTop, iContentWidth, 60),"1、目录一"); iTop += 60; // 左侧缩进2字符 int iLeft = 120; pPdfPainter->drawText(QRect(iLeft, iTop, iContentWidth - iLeft, 60),"我的目录一的内容。", option); iTop += 60; pPdfPainter->drawText(QRect(0, iTop, iContentWidth, 60), "2、目录二"); iTop += 60; pPdfPainter->drawText(QRect(iLeft, iTop, iContentWidth - iLeft, 60),"我的目录2的内容", option); delete pPdfPainter; delete pPdfWriter; pdfFile.close();
另一段代码 原帖 https://blog.csdn.net/toby54king/article/details/79109545
/***************************.h*************************************/ #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPrinter> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); /// 设置PDF文件名字 void setPdfName(QString fileName); /// 写入文字到pdf上 void writeTextToPdf(QString text); /// 插入图片到PDF上 void insertPictureToPdf(QString picFile); /// 结束绘制 void endPainter(); /// html转化为PDF QString saveHtmlToPDF(); private: Ui::Widget *ui; QPrinter *m_printer; QPainter *m_painter; int m_x; int m_y; }; #endif // WIDGET_H /***************************.cpp*************************************/ #include "widget.h" #include "ui_widget.h" #include <QPainter> #include <QPixmap> #include <QTextDocument> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); m_printer = new QPrinter; m_painter = new QPainter; m_x = 0; m_y = 0; QString pdfname = "E:/testhtmlpdf.pdf"; QPrinter printer; printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(pdfname); QTextDocument document; QString html = saveHtmlToPDF(); document.setHtml(html); document.print(&printer); document.end(); } Widget::~Widget() { delete ui; } //************************************ // 函数名称:setPdfName // 函数说明:设置PDF文件名字并启动绘制 // 作 者:ISmileLi //************************************ void Widget::setPdfName(QString fileName) { m_printer->setPageSize(QPrinter::A4); m_printer->setOutputFormat(QPrinter::PdfFormat); m_printer->setOutputFileName(fileName); m_painter->begin(m_printer); } //************************************ // 函数名称:writeTextToPdf // 函数说明:写入文字到PDF // 作 者:ISmileLi //************************************ void Widget::writeTextToPdf(QString text) { if(m_printer == NULL) return; int height = 10; int width = 300; m_painter->drawText(m_x,m_y,width,height,0,text); m_y += height; } //************************************ // 函数名称:insertPictureToPdf // 函数说明:插入图片到PDF // 作 者:ISmileLi //************************************ void Widget::insertPictureToPdf(QString picFile) { if(m_printer == NULL) return; QPixmap *pixmap = new QPixmap(picFile); int width = pixmap->width(); int height = pixmap->height(); m_painter->drawPixmap(m_x,m_y,width,height,*pixmap); m_y += height; } //************************************ // 函数名称:insertPictureToPdf // 函数说明:结束PDF绘制操作 // 作 者:ISmileLi //************************************ void Widget::endPainter() { m_painter->end(); } //************************************ // 函数名称:saveHtmlToPDF // 函数说明:html方法生成PDF // 作 者:ISmileLi //************************************ QString Widget::saveHtmlToPDF() { QString html = ""; html += "<html>"; html += "<head>"; html += "<title>qt实现生成PDF文档</title>"; html += "<head>"; html += "<body bgcolor=\"#ccccff\">"; html += "<h1 style=\"background-color:red\">测试qt实现生成PDF文档</h1>"; html += "<hr>"; html += "<p>PDF插入图片<img src=\"E:/work/girl.jpg\" alt=\"picture\" width=\"100\" height=\"100\"></p>"; html += "<hr>"; html += "<table width=\"100%\" border=\"1\" cellspacing=\"1\" cellpadding=\"4\" align=\"center\">"; html += "<tr>"; html += "<td align=\"center\" height=\"60\">编程语言统计</td>"; html += "</tr>"; html += "<tr>"; html += "<td align=\"center\" height=\"25\">2017-01-18---2018-01-18</td>"; html += "</tr>"; html += "</table>"; html += "<table width=\"100%\" border=\"1\" cellspacing=\"1\" cellpadding=\"4\" bgcolor=\"#cccccc\" align=\"center\">"; html += "<tr>"; html += "<th>C/C++</th>"; html += "<th>python</th>"; html += "<th>java</th>"; html += "<th>html</th>"; html += "<th>Qt</th>"; html += "</tr>"; html += "<tr>"; html += "<th>上升</th>"; html += "<th>上升</th>"; html += "<th>下降</th>"; html += "<th>下降</th>"; html += "<th>上升</th>"; html += "</tr>"; html += "</table>"; html += "</body>"; html += "</html>"; return html; } /***************************main.cpp*************************************/ #include "widget.h" #include <QApplication> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.setPdfName("E:/testPDF.pdf"); QStringList testList = (QStringList()<<"测试PDF文字写入" << "测试PDF写入hello word"); foreach(QString text,testList) { w.writeTextToPdf(text); qDebug() << "text:" <<text; } w.insertPictureToPdf("E:/Monitor.png"); w.insertPictureToPdf("E:/map.png"); w.insertPictureToPdf("E:/girl.jpg"); w.endPainter(); w.show(); return a.exec(); }
发表评论