天天看點

Qt 建立生成PDF報告Qt 建立生成PDF報告

Qt 建立生成PDF報告

使用QPdfwriter 和QPainter建立pdf報告,關鍵是對頁面的布局排版。本示例,進行了頁眉,頁腳,中間内容的排版。頁眉左上角為圖檔logo,頁眉右邊為頁數提示;頁腳有生成報告的時間和公司logo布局。

圖像的縮放不采用scale,直接采用Rect來縮放,如縮小原圖的一半,隻需 image.width/2。

#include "QReport.h"

#include <QFile>
#include <QTextOption>
#include <QDateTime>
#include <QDebug>

QReport::QReport()
{

}

/** 建立生成PDF*/
void QReport::MakePDFReport(std::string filePath)
{
    std::string fileName = filePath + "\\Report.pdf";//PDF檔案名
    QString fileNameString = QString::fromStdString(fileName);
    QFile pdfFile(fileNameString);
    pdfFile.open(QIODevice::WriteOnly);
    QPdfWriter *pdfWriter = new QPdfWriter(&pdfFile);
    pdfWriter->setPageSize(QPagedPaintDevice::A4);//設定pdf大小
    
    //@像素3508*2479,分辨率決定了像素大小,便于後續的排版布局
    //@ 這裡操作的所有x,y坐标,都是像素,即在3508*2479的基礎上進行操作
    pdfWriter->setResolution(300);
    pdfWriter->setTitle("Report");

    int pageMargin = 100;
    pdfWriter->setPageMargins(QMarginsF(pageMargin, pageMargin, pageMargin, pageMargin));//設定pdf四周的空白

    QDateTime currentTime = QDateTime::currentDateTime();
    QString timeString = currentTime.toString("yyyy-MM-dd hh:mm:ss ddd");//擷取時間


    QPainter *pdfPainter = new QPainter(pdfWriter);//建立painter,用于繪制圖檔,文字,線條等



    QTextOption option(Qt::AlignHCenter | Qt::AlignVCenter);//字型排版方式,左右,上下居中
    option.setWrapMode(QTextOption::WordWrap);
    int yCurrentP = 0;
    int xCurrentP = 0;
    int contentWidth = 2479 - pageMargin;

    QFont font;
    font.family();
//    font.setFamily("simsun.ttc");
    font.setFamily("Times New Roman");
    int fontSize =9;
    int textHeight = 90;
    font.setPointSize(fontSize);
    pdfPainter->setFont(font);

	//設定頁眉左上角的logo
    std::string titleImageFileName = filePath + "\\Imager.png";//圖檔名稱,可更換為自己的圖檔路徑
    QPixmap titleImage;
    titleImage.load(QString::fromStdString(titleImageFileName));//加載圖像
    //    pdfPainter->drawPixmap(xCurrentP, yCurrentP, titleImage.width(), titleImage.height(), titleImage);
    //    pdfPainter->scale(0.5, 0.5);
    //@ 繪制圖像,xCurrentP,yCurrentP決定了圖像放置的X,Y坐标起始位置,
    //@ titleImage.width()/2 和titleImage.height()/2,寬和高均除以2,就是圖像等比例縮小一倍
   	//@  最後圖像在PDF呈現的就是原圖進行了縮放,縮小了一倍,通過此方式來縮小圖檔,進行一行放置多張圖檔,進行排版
   	//@ 不采用scale的方式進行圖像縮放
    pdfPainter->drawPixmap(xCurrentP, yCurrentP, titleImage.width()/2, titleImage.height()/2, titleImage);
    //    pdfPainter->scale(1.5, 1.5);//不采用

	//設定頁眉右上角頁數
    option.setAlignment(Qt::AlignRight | Qt::AlignBottom);
    int pageIndex = 1;
    int totalPageIndex = 2;
    QString pageIndexString = QString::number(pageIndex) + "/" + QString::number(totalPageIndex);
    pdfPainter->drawText(QRect(xCurrentP, yCurrentP, contentWidth,titleImage.height()/2),
                         pageIndexString, option);


    yCurrentP += (titleImage.height()/2 + 4);
    QPen pen;
    int penHeigth = 4;
    pen.setWidth(penHeigth);
    pen.setColor(Qt::blue);
    pdfPainter->setPen(pen);
    pdfPainter->drawLine(xCurrentP, yCurrentP, contentWidth, yCurrentP);


    pen.setColor(Qt::black);
    pdfPainter->setPen(pen);
    yCurrentP += penHeigth;


	//設定标題Y坐标,相對頁眉下移100像素

    yCurrentP += 100;
	//設定PDF檔案标題
    fontSize =22;
    font.setPointSize(fontSize);
    pdfPainter->setFont(font);
    option.setAlignment(Qt::AlignCenter);
    pdfPainter->drawText(QRect(0, yCurrentP, contentWidth, textHeight),
                         QString("分析報告"), option);


	//設定PDF檔案1級标題,靠左顯示
    yCurrentP += (textHeight + 100);
    fontSize = 16;
    font.setPointSize(fontSize);
    pdfPainter->setFont(font);
    option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    pdfPainter->drawText(QRect(xCurrentP, yCurrentP, contentWidth, 80),
                         QString("通道微滴分布圖"));

  
	//設定頁腳,頁腳顯示一張公司logo
    std::string logo = filePath + "\\Imager.png";
    QPixmap logoImage;
    logoImage.load(QString::fromStdString(logo));

    yCurrentP = 3508-pageMargin -( logoImage.height()/2) + 4;
    pen.setColor(Qt::blue);
    pdfPainter->setPen(pen);
    pdfPainter->drawLine(0, yCurrentP, contentWidth, yCurrentP);
    yCurrentP = 3508-pageMargin -( logoImage.height()/2);
    option.setAlignment(Qt::AlignLeft | Qt::AlignTop);
    fontSize = 9;
    font.setPointSize(fontSize);
    pen.setColor(Qt::black);
    pdfPainter->setPen(pen);
    pdfPainter->drawPixmap(contentWidth-xCurrentP-logoImage.width()/2, yCurrentP, logoImage.width()/2, logoImage.height()/2, logoImage);
    pdfPainter->setFont(font);
    pdfPainter->drawText(QRect(0, yCurrentP, 600, logoImage.height()), timeString, option);


	//@ 完成,删除對象
    delete pdfPainter;
    delete pdfWriter;
    
    pdfFile.close();//關閉
}
           

繼續閱讀