一、使用QPdfWriter繪制PDF
1 void MainWindow::exportPdf() 2 { 3 //一、選擇保存pdf文件路徑 4 QString sPath = QFileDialog::getSaveFileName(this, tr("另存為"), "/", tr("Text Files (*.pdf)")); 5 if(sPath.isEmpty()) 6 { 7 return; 8 } 9 qDebug() << sPath; 10 11 //二、創建pdf文件 12 QFile pdfFile(sPath); 13 pdfFile.open(QIODevice::WriteOnly); 14 15 //三、創建生成pdf類,作為繪圖設備 16 QPdfWriter *pPdfWriter = new QPdfWriter(&pdfFile); 17 pPdfWriter->setResolution(300); 18 pPdfWriter->setPageSize(QPagedPaintDevice::A4); 19 pPdfWriter->setPageMargins(QMarginsF(30, 30, 30, 30)); 20 21 //四、開始繪制PDF 22 paintPdf(pPdfWriter); 23 24 delete pPdfWriter; 25 pdfFile.close(); 26 27 //通過其它PDF閱讀器來打開剛剛繪制的PDF 28 QDesktopServices::openUrl(QUrl::fromLocalFile(sPath)); 29 }
二、使用QPrintPreviewDialog預覽繪制的PDF然后輸出
在生成的打印預覽界面點擊打印按鈕,就會彈出導出PDF窗口,選擇要PDF文件要保存的路徑,然后保存即可。
如下圖所示:
(1)創建QPrintPreviewDialog對象
1 QPrintPreviewDialog *m_pPrintPreviewDlg = new QPrintPreviewDialog();
(2)將paintRequested()信號連接到槽
1 connect(m_pPrintPreviewDlg, &QPrintPreviewDialog::paintRequested, this, &MainWindow::slot_previewPdf);
(3)設置默認打印機的一些參數,最重要的是設置打印機輸出格式為PDF
1 m_pPrintPreviewDlg->printer()->setResolution(300); 2 m_pPrintPreviewDlg->printer()->setOrientation(QPrinter::Portrait); 3 //設置打印機輸出格式為PDF格式 4 m_pPrintPreviewDlg->printer()->setOutputFormat(QPrinter::PdfFormat); 5 m_pPrintPreviewDlg->printer()->setPaperSize(QPagedPaintDevice::A4); 6 m_pPrintPreviewDlg->printer()->setPageMargins(QMarginsF(30, 30, 30, 30));
(4)槽函數
1 void MainWindow::slot_previewPdf(QPrinter *printer) 2 { 3 paintPdf(printer); 4 }
(5)繪制PDF函數
1 void MainWindow::paintPdf(QPagedPaintDevice *device) 2 { 3 QPainter *pPainter = new QPainter(device); 4 qDebug() << pPainter->viewport(); 5 int nPdfWidth = pPainter->viewport().width(); 6 int nPdfHeight = pPainter->viewport().height(); 7 8 //繪制標題 9 int y = 10; 10 pPainter->setFont(QFont("宋體", 18, 36)); 11 pPainter->drawText(QRect(0, y, nPdfWidth, 100), Qt::AlignCenter, tr("實驗操作記錄報告")); 12 13 //繪制報告相關信息 14 y += 200; 15 pPainter->setFont(QFont("宋體", 12, 20)); 16 int nLineSpace = 100; 17 int nLineHeight = 60; 18 pPainter->drawText(QRect(100, y, nPdfWidth, nLineHeight), Qt::AlignLeft, QString("實驗編號:%1").arg("19990926001")); 19 y += nLineSpace; 20 pPainter->drawText(QRect(100, y, nPdfWidth, nLineHeight), Qt::AlignLeft, QString("操作儀器:%1").arg("TMachine9568")); 21 y += nLineSpace; 22 pPainter->drawText(QRect(100, y, nPdfWidth, nLineHeight), Qt::AlignLeft, QString("操作用戶:%1").arg("Dr.Miss")); 23 y += nLineSpace; 24 pPainter->drawText(QRect(100, y, nPdfWidth, nLineHeight), Qt::AlignLeft, QString("開始時間:%1").arg("1999-09-26 17:50")); 25 y += nLineSpace; 26 pPainter->drawText(QRect(100, y, nPdfWidth, nLineHeight), Qt::AlignLeft, QString("結束時間:%1").arg("1999-09-26 22:30")); 27 28 //繪制每個操作記錄的各項標題 (標題寬的比例為 序號:操作時間:操作說明:試管和試劑信息 = 2:5:5:10) 29 y += 150; 30 pPainter->setFont(QFont("宋體", 14, 28)); 31 int nUnitW = nPdfWidth / (2 + 5 + 5+ 10); 32 33 int nNoWidth = nUnitW * 2; 34 35 int nTimeX = nNoWidth; 36 int nTimeWidth = nUnitW * 5; 37 38 int nExplainX = nTimeX + nTimeWidth; 39 int nExplainWidth = nUnitW * 5; 40 41 int nRgInfoX = nExplainX + nExplainWidth; 42 int nRgInfoW = nUnitW * 10; 43 44 nLineHeight = 80; 45 pPainter->drawText(QRect(0, y, nNoWidth, nLineHeight), Qt::AlignCenter, "序號"); 46 pPainter->drawText(QRect(nTimeX, y, nTimeWidth, nLineHeight), Qt::AlignCenter, "操作時間"); 47 pPainter->drawText(QRect(nExplainX, y, nExplainWidth, nLineHeight), Qt::AlignCenter, "操作說明"); 48 pPainter->drawText(QRect(nRgInfoX, y, nRgInfoW, nLineHeight), Qt::AlignCenter, "試管和試劑信息"); 49 50 //繪制一條分隔線 51 y += 100; 52 pPainter->setPen(QPen(QBrush(QColor(144, 166,188)), 8)); 53 pPainter->drawLine(QLineF(0, y, nPdfWidth, y)); 54 55 //繪制染色操作記錄,假設此刻有25條記錄需要繪制 56 for(int i = 0; i < 25; ++i) 57 { 58 //判斷是否應該另起一頁(320:根據計算,繪制每條記錄大概需要320點) 59 if(y + 320 >= nPdfHeight) 60 { 61 device->newPage(); 62 y = 10; 63 } 64 65 y += 50; 66 pPainter->setFont(QFont("宋體", 12, 20)); 67 pPainter->setPen(QPen(QBrush(QColor(0, 0, 0)), 1)); 68 69 nLineSpace = 80; 70 nLineHeight = 50; 71 72 pPainter->drawText(QRect(0, y, nNoWidth, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, QString("%1").arg(i + 1)); 73 pPainter->drawText(QRect(nTimeX, y, nTimeWidth, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, "1999-09-26 18:27"); 74 pPainter->drawText(QRect(nExplainX, y, nExplainWidth, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, "需加試劑DC9混合"); 75 76 //試管和試劑信息的每行需列出2項信息 寬度比例為7:3 77 nUnitW = nRgInfoW / (7 + 3); 78 79 int nLeftX = nRgInfoX; 80 int nLeftW = nUnitW * 6; 81 82 int nRightX = nLeftX + nLeftW; 83 int nRightW = nUnitW * 4; 84 85 pPainter->drawText(QRect(nLeftX, y, nLeftW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, 86 QString("試管ID: %1").arg("15bb00226fa")); 87 pPainter->drawText(QRect(nRightX, y, nRightW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, 88 QString("試管位置號: %1").arg(18)); 89 90 y += nLineSpace; 91 pPainter->drawText(QRect(nLeftX, y, nLeftW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, 92 QString("添加試劑1: %1").arg("AC856")); 93 pPainter->drawText(QRect(nRightX, y, nRightW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, 94 QString("添加量:%1 μL").arg(150)); 95 96 y += nLineSpace; 97 pPainter->drawText(QRect(nLeftX, y, nLeftW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, 98 QString("添加試劑2: %1").arg("SFH51")); 99 pPainter->drawText(QRect(nRightX, y, nRightW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, 100 QString("添加量:%1 μL").arg(80)); 101 102 y += 70; 103 pPainter->setPen(QPen(QBrush(QColor(166, 188, 222)), 3)); 104 pPainter->drawLine(QLineF(0, y, nPdfWidth, y)); 105 } 106 107 delete pPainter; 108 }