QT利用QPdfWriter實現繪制PDF(支持表單輸出)
之前打印PDF都是通過html形式來實現的,但是這次要做的東西,需要打印界面控件,所以需要使用QPdfWriter.
通過QPdfWriter來獲取QPainter對象,就能實現在PDF上來畫畫啦.
代碼生成的PDF如下所示:
代碼效果如下所示:
代碼如下所示:
//通過dialog來保存PDF路徑 QString mainWindow::pdfSaveAs(QString fileName) { QString file=""; file = QFileDialog::getSaveFileName ( this, //父組件 "另存為", //標題 QString("%1/%2").arg(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).arg(""+fileName+".pdf"), //設置路徑, .表示當前路徑,./表示更目錄 "PDF文件", //過濾器,保存的類型 Q_NULLPTR, QFileDialog::ShowDirsOnly); return file; } //y:表單的Y坐標 //horzBorder:水平外邊距,寫0表示沒有邊距 //row,column:行數和列數 //unitHeight:單元高度 //font:字體大小 //list:要寫入的單元數據 void mainWindow::pdfDrawForm(QPainter* paint,int y,int horzBorder,int row,int column,int unitHeight,QFont &font,QStringList& list) { paint->setFont(font); paint->setPen(QPen(QBrush(QColor(0,0,0)),2)); int Width = paint->viewport().width()-horzBorder*2; int unitWidth = Width/column; for(int i=0;i<row;i++) { int x= horzBorder; for(int j=0;j<column;j++) { paint->drawText(QRect(x,y, unitWidth, unitHeight), Qt::AlignCenter,list[i*column+j]); paint->drawRect(QRect(x,y, unitWidth, unitHeight)); x+=unitWidth; } y += unitHeight; } } void mainWindow::pdfCreate(checkInfo &info) { // //設置pdf保存的路徑 // QString file_path = pdfSaveAs(QString("表單%1").arg(QDateTime::currentDateTime().toString("MMdd-hh-mm"))); // if(file_path.isEmpty()) // return; QString file_path =QApplication::applicationDirPath()+"/test.pdf"; QFile pdfFile(file_path); pdfFile.open(QIODevice::WriteOnly); QPdfWriter* pWriter = new QPdfWriter(&pdfFile); //Init Page pWriter->setPageSize(QPagedPaintDevice::A4); pWriter->setResolution(300); //設置dpi 每個平方英寸像素為300 pWriter->setPageMargins(QMarginsF(30, 30, 30, 30)); QPainter* pPainter = new QPainter(pWriter); //Init Font QFont font[5]={QFont("宋體",26,60),QFont("宋體",26,61),QFont("宋體",26,QFont::Normal),QFont("宋體",26,QFont::Normal),QFont("宋體",26,QFont::Normal)}; font[0].setPixelSize(86); font[1].setPixelSize(61); font[2].setPixelSize(61); font[3].setPixelSize(42); font[4].setPixelSize(54); //Painter PDF qDebug()<<pPainter->viewport(); int nPDFWidth = pPainter->viewport().width(); int nPDFHeight = pPainter->viewport().height(); //在10%的頭部居中顯示 int y=10; pPainter->setFont(font[0]); pPainter->drawText(QRect(0,y, nPDFWidth, 100), Qt::AlignCenter, m_arg.printTitle); y+=140; pPainter->setPen(QPen(QBrush(QColor(0,0,0)),5)); pPainter->drawLine(0,y,nPDFWidth,y); pPainter->drawLine(0,y+18,nPDFWidth,y+18); y+=60; pPainter->setFont(font[2]); pPainter->drawText(QRect(100,y, nPDFWidth/2, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("器件型號: %1").arg(info.dev.name)); pPainter->setFont(font[2]); pPainter->drawText(QRect(nPDFWidth/2+100,y, nPDFWidth/2-100, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("器件編號: %1").arg(info.devNum)); y+=90; pPainter->setFont(font[2]); pPainter->drawText(QRect(100,y, nPDFWidth/2, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("裝車車號: %1").arg(info.devCar)); pPainter->setFont(font[2]); pPainter->drawText(QRect(nPDFWidth/2+100,y, nPDFWidth/2-100, 70), Qt::AlignVCenter | Qt::AlignLeft, QString("試驗日期: %1").arg(info.checkDate)); y+=110; pPainter->setFont(font[1]); pPainter->drawText(QRect(0,y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft, "減 振 器 標 準 參 數"); y+=120; QStringList list; list<<"速度"<<"壓力"<<"拉力"<<"阻尼系數"<<"不對稱率"<<"正允差"<<"負允差"; list<<info.dev.speed+"m/s"<<info.dev.yaLi+"kN"<<info.dev.laLi+"kN"<<\ info.dev.zhuNi+"kNs/m"<<info.dev.buDuiC+"%"<<info.dev.zYunCha+"%"<<info.dev.fYunCha+"%"; pdfDrawForm(pPainter,y,0,2,7,100,font[3],list); y+=260; pPainter->setFont(font[1]); pPainter->drawText(QRect(0,y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft, "示 功 圖"); y+=100; //獲取界面圖片 int imageBorder=150; //設置圖片水平邊距為150 QPixmap pixmap = QPixmap::grabWidget(m_checkChart, m_checkChart->rect()); float x = (float)(nPDFWidth-imageBorder*2)/(float)pixmap.width(); pixmap= pixmap.scaled(nPDFWidth-imageBorder*2, x*pixmap.height(),Qt::IgnoreAspectRatio); //根據大小比例,來放大縮小圖片 pPainter->drawPixmap(imageBorder, y, pixmap); y+=pixmap.height()+90; pPainter->setFont(font[1]); pPainter->drawText(QRect(0,y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft, "減 振 器 測 試 結 果"); y+=120; list.clear(); list<<"拉伸行程:"<<QString::asprintf("%.2fmm",info.laSheng)<<"壓縮行程:"<<QString::asprintf("%.2fmm",info.yaSuo)<<"最大速度:"<<QString::asprintf("%.3fm/s",info.speed)<<"阻尼系數:"<<QString::asprintf("%.2fkNs/m",info.speed) <<"最大壓力:"<<QString::asprintf("%.2fkN",info.max)<<"壓力偏差:"<<QString::asprintf("%.2f%",info.maxBaiFengb)<<"最大拉力:"<<QString::asprintf("%.2fkN",info.min)<<"拉力偏差:"<<QString::asprintf("%.2f%",info.minBaiFengb) <<"不對稱率:"<<QString::asprintf("%.2f%",info.buduic)<<"運行時間:"<<QString::asprintf("%dS",info.runDate)<<"試驗結果:"<<info.result<<"曲線結論:"<<info.ChartResult; pdfDrawForm(pPainter,y,0,3,8,100,font[3],list); y+=400; pPainter->setFont(font[4]); pPainter->drawText(QRect(50,y, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft, QString( "試驗員: %1").arg(m_userLoginInfo->name)); pPainter->drawText(QRect(nPDFWidth/3+50,y, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft, QString( "檢查員簽字: ")); pPainter->drawText(QRect(nPDFWidth/3*2,y, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft, QString( "簽字日期: ")); // pWriter->newPage(); //寫下一頁 //繪制完畢 delete pPainter; delete pWriter; pdfFile.close(); //通過其它PDF閱讀器來打開PDF QDesktopServices::openUrl(QUrl::fromLocalFile(file_path)); }
到此這篇關於QT利用QPdfWriter實現繪制PDF(支持表單輸出)的文章就介紹到這瞭,更多相關QT QPdfWriter繪制PDF內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!