QT如何生產pdf文件,網上有許多文章介紹,我也是看了網上的文章,看他們的代碼,自己琢磨琢磨,才有了本編博客;
其他什么就不詳細說了,本篇博客介紹的QPdfWriter和QPainter繪制PDF文件;對pdf這里是繪制出來的,沒有什么規范的格式,都是通過xy坐標繪制出來的。
QPdfWriter類設置pdf的基礎設置;QPainter類繪制文本,圖片矩形等;
以下代碼參考博客:Qt中使用QPdfWriter類結合QPainter類繪制并輸出PDF文件-CSDN博客
自己稍微做了調整了修改!
反正得自己會QPainter繪制,否則你無法繪制pdf文件出來!!!
頭文件:
#include <QPdfWriter>
#include <QDesktopServices>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QtPrintSupport>
一、步驟
繪制pdf有以下步驟:
1.選擇需要導出的pdf文件路徑;
2.創建pdf文件;
3.創建生成pdf類,作為繪圖設備;
4.繪制PDF;
5.查看繪制好的pdf。
void Widget::exportPdf()
{//一、選擇保存pdf文件路徑QString sPath = QFileDialog::getSaveFileName(this, tr("另存為"), "/", tr("Text Files (*.pdf)"));if(sPath.isEmpty()){return;}qDebug() << sPath;//二、創建pdf文件QFile pdfFile(sPath);pdfFile.open(QIODevice::WriteOnly);//三、創建生成pdf類,作為繪圖設備QPdfWriter *pPdfWriter = new QPdfWriter(&pdfFile);pPdfWriter->setResolution(300); // 將打印設備的分辨率設置為屏幕分辨率pPdfWriter->setPageSize(QPagedPaintDevice::A4); // 設置紙張為A4紙pPdfWriter->setPageMargins(QMarginsF(30, 30, 30, 30)); // 設置頁邊距 順序是:左上右下//四、開始繪制PDF//paintPdf(pPdfWriter);delete pPdfWriter;pdfFile.close();//通過其它PDF閱讀器來打開剛剛繪制的PDFQDesktopServices::openUrl(QUrl::fromLocalFile(sPath));
}
二、效果展示
三、代碼實現
#include "widget.h"
#include "ui_widget.h"#include <QFileDialog>
#include <QStandardPaths>
#include <QPdfWriter>
#include <QDebug>
#include <QDesktopServices>
#include <QMessageBox>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QtPrintSupport>#include <pdfgenerator.h>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);createPDF();
}Widget::~Widget()
{delete ui;
}void Widget::createPDF()
{int y = 0;QString path = "/home/UOS/Desktop/3.pdf";PdfGenerator *pdfGenerator = new PdfGenerator;bool flag = pdfGenerator->setFileName(path);if (!flag) {return ;}int nPdfWidth = pdfGenerator->getPdfWidth();pdfGenerator->beginPage();QPixmap p;p.load(":/007.jpg");int pHeight = pdfGenerator->drawImage(QRectF(0, y, 150, 150), p);y += pHeight + 50;// 繪制表格y += 100;QFont f = QFont("宋體", 18, 36);QColor blackColor = QColor(0,0,0);pdfGenerator->drawRect(QRectF(0, y, nPdfWidth, 200), blackColor, 2, QColor(100, 0, 200, 50));pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 200), "這是標題文本!", QFont("宋體", 32, 64));// 繪制科目y += 200;int nClassWidthFlag = nPdfWidth / (8);int nClassWidth = nClassWidthFlag;QList<QString> classList;classList << "語文" << "數學" << "英語" << "歷史" << "政治" << "地理" << "音樂" << "體育";int classx = 0;for (int i = 0; i < classList.count(); i++) {pdfGenerator->drawRect(QRectF(classx, y, nClassWidth, 100), blackColor, 2);pdfGenerator->drawText(QRectF(classx, y, nClassWidth, 100), classList.at(i), QFont("宋體", 12, 20), QColor(200, 100, 100), Qt::AlignCenter);classx += nClassWidth;if (7 == i + 1) {int n = nPdfWidth - (classx + nClassWidth);nClassWidth += n;}}// 繪制個人信息y += 100;int nPersonDescriptionWidth = nClassWidthFlag * 2;QList<QList<QString>> descriptionList;QList<QString> list;list << "小米" << "男" << "15";descriptionList << list;list.clear();list << "小明" << "男" << "20";descriptionList << list;list.clear();list << "小紅" << "女" << "21";descriptionList << list;list.clear();list << "姓名" << "性別" << "年齡";int nPersonDescriptionHeight = 120 + descriptionList.count() * 100;pdfGenerator->drawRect(QRect(0, y, nPersonDescriptionWidth, nPersonDescriptionHeight));pdfGenerator->drawText(QRect(0, y, nPersonDescriptionWidth, nPersonDescriptionHeight), "個人信息",QFont("宋體", 20, 40), QColor(100, 100, 255));int nMessageX = nPersonDescriptionWidth;int nMessageWidth = nPersonDescriptionWidth;for (int i = 0; i < list.count(); i++) {pdfGenerator->drawRect(QRect(nMessageX, y, nMessageWidth, 120));pdfGenerator->drawText(QRect(nMessageX, y, nMessageWidth, 120), list.at(i), QFont("宋體", 18, 30), QColor(200, 50, 50));nMessageX += nPersonDescriptionWidth;if (list.count()-1 == i + 1) {int n = nPdfWidth - (nMessageX + nPersonDescriptionWidth);nMessageWidth += n;}}y += 120;int nPersonX = nPersonDescriptionWidth;int nPersonWidth = nPersonDescriptionWidth;for (int i = 0; i < descriptionList.count(); ++i) {QList<QString> list = descriptionList.at(i);for (int j = 0; j < list.count(); ++j) {pdfGenerator->drawRect(QRect(nPersonX, y, nPersonWidth, 100));pdfGenerator->drawText(QRect(nPersonX, y, nPersonWidth, 100), list.at(j), pdfGenerator->getBaseFont());nPersonX += nPersonDescriptionWidth;if (list.count()-1 == j + 1) {int n = nPdfWidth - (nPersonX + nPersonDescriptionWidth);nPersonWidth += n;}}y += 100;nPersonX = nPersonDescriptionWidth;nPersonWidth = nPersonDescriptionWidth;}// 繪制詳細信息descriptionList.clear();list.clear();list << "小明" << "swim" << "擅長蛙泳" << "170cm";descriptionList << list;list.clear();list << "小紅" << "dance" << "街舞鼻祖" << "160cm";descriptionList << list;list.clear();list << "小黃" << "run" << "短跑小王子" << "166cm";descriptionList << list;list.clear();list << "小綠" << "jump" << "跳高運動員" << "196cm";descriptionList << list;list.clear();list << "姓名" << "愛好" << "特點" << "身高";int nDetailedInformationWidth = nClassWidthFlag * 2;int nDetailedInformationHeight = 120 + descriptionList.count() * 100;pdfGenerator->drawRect(QRect(0, y, nDetailedInformationWidth, nDetailedInformationHeight));pdfGenerator->drawText(QRect(0, y, nDetailedInformationWidth, nDetailedInformationHeight), "詳細信息",QFont("宋體", 20, 40), QColor(100, 100, 255));int nInformationTotalWidth = nPdfWidth - nDetailedInformationWidth;int nInformationOneWidth = nInformationTotalWidth / descriptionList.count();int nDetailedX = nDetailedInformationWidth;int nDetailedWidth = nInformationOneWidth;for (int i = 0; i < list.count(); i++) {pdfGenerator->drawRect(QRect(nDetailedX, y, nDetailedWidth, 120));pdfGenerator->drawText(QRect(nDetailedX, y, nDetailedWidth, 120), list.at(i), QFont("宋體", 20, 30));nDetailedX += nDetailedWidth;if (list.count()-1 == i + 1) {int n = nPdfWidth - (nDetailedX + nDetailedWidth);nDetailedWidth += n;}}y += 120;int nInformationX = nDetailedInformationWidth;int nInformationWidth = nInformationOneWidth;for (int i = 0; i < descriptionList.count(); ++i) {QList<QString> list = descriptionList.at(i);for (int j = 0; j < list.count(); ++j) {pdfGenerator->drawRect(QRect(nInformationX, y, nInformationWidth, 100));pdfGenerator->drawText(QRect(nInformationX, y, nInformationWidth, 100), list.at(j),pdfGenerator->getBaseFont(), QColor(200,0,0));nInformationX += nInformationWidth;if (list.count()-1 == j + 1) {int n = nPdfWidth - (nInformationX + nInformationWidth);nInformationWidth += n;}}y += 100;nInformationX = nDetailedInformationWidth;nInformationWidth = nInformationOneWidth;}y += 50;pdfGenerator->drawPolygon(QRect(600, y, 800, 600));pdfGenerator->drawRect(QRect(600, y, 800, 600));// 換頁// if(y + 100 >= pdfGenerator->getPdfHeight()) {// pdfGenerator->newPage();// y = 10;// }pdfGenerator->newPage();y = 0;/************************************************************************************************************/// 外部大矩形框pdfGenerator->drawRect(QRect(0, y, nPdfWidth, pdfGenerator->getPdfHeight()), QColor(0,0,0), 8);// 左上角m級f = pdfGenerator->getBaseFont();f.setBold(true);f.setPointSize(13);pdfGenerator->drawText(QRectF(10, y, 360, 100), "m級:非m", f, QColor(0,0,0), Qt::AlignLeft);// 頁碼int pageX = 900;f = pdfGenerator->getBaseFont("宋體", 12, 0);pdfGenerator->drawText(QRectF(pageX, y, 500, 100), "第 1 頁 Page", f, QColor(0,0,0), Qt::AlignLeft);y += 80;pdfGenerator->drawText(QRectF(pageX, y, 1444, 100), "共 2 頁 This report includes page", f, QColor(0,0,0), Qt::AlignLeft);y += 100;// 標題1f = pdfGenerator->getBaseFont("宋體", 20, 8);pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 120), "嘻嘻嘻這是一份關于檢測相關的世界的詳細報告哈哈", f, QColor(0,0,0), Qt::AlignCenter);y += 120;// 標題1英語f = pdfGenerator->getBaseFont("Times New Roman", 11, 8);QString english = "abc abc He he he, this is a detailed report about the world related to testing. Ha ha. abc abc";pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 120), english, f, QColor(0,0,0), Qt::AlignCenter);y += 150;// 標題2f = pdfGenerator->getBaseFont("宋體", 30, 8);pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 130), "一二三四五六報告", f, QColor(0,0,0), Qt::AlignCenter);y += 160;// 標題2英語f = pdfGenerator->getBaseFont("Times New Roman", 20, 12);pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 130), "Tihs is a Report", f, QColor(0,0,0), Qt::AlignCenter);y += 160;// 報告編號f = pdfGenerator->getBaseFont("宋體", 10, 0);pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 50), "報告編號:( 2025 )報告的 第 15 號", f, QColor(0,0,0), Qt::AlignCenter);y += 80;// Report No.f = pdfGenerator->getBaseFont("Times New Roman", 10, 0);pdfGenerator->drawText(QRectF(0, y, nPdfWidth, 50), "Report No.", f, QColor(0,0,0), Qt::AlignCenter);int nameX = 200; // 距離左邊的距離int lineX = 500; // 橫線距離左邊的距離int lineWidth = 1322; // 橫線寬度y += 100;for (int i = 0; i < 6; i++) {int tmpY = y + 30;// 你的名稱f = pdfGenerator->getBaseFont();pdfGenerator->drawText(QRectF(nameX, y, 250, 60), "你的名稱:", f, QColor(0,0,0), Qt::AlignLeft);y += 80;// 英文f = pdfGenerator->getBaseFont("Times New Roman", 10, 0);pdfGenerator->drawText(QRectF(nameX, y, 250, 60), "Your name", f, QColor(0,0,0), Qt::AlignLeft);y += 70;// 畫橫線f = pdfGenerator->getBaseFont();pdfGenerator->drawLine(QPointF(lineX, y), QPointF(lineX + lineWidth, y));// 畫文本pdfGenerator->drawText(QRectF(lineX, tmpY, lineWidth, 80), "這是名字呀", f, QColor(0,0,0), Qt::AlignCenter);y += 30;// 自動換頁處理
// if(y + 200 >= pdfGenerator->getPdfHeight()) {
// pdfGenerator->newPage();
// y = 10;
// }}y += 50;// 簽發人f = pdfGenerator->getBaseFont();pdfGenerator->drawText(QRectF(nameX, y, 400, 60), "簽發人:(簽字)", f, QColor(0,0,0), Qt::AlignLeft);int dataX = 1111;// 發證日期f = pdfGenerator->getBaseFont();pdfGenerator->drawText(QRectF(dataX, y, 400, 60), "發證日期:", f, QColor(0,0,0), Qt::AlignLeft);y += 90;// 簽發人 英文f = pdfGenerator->getBaseFont("Times New Roman", 10);pdfGenerator->drawText(QRectF(nameX, y, 400, 60), "Signature of leader", f, QColor(0,0,0), Qt::AlignLeft);// 發證日期 英文f = pdfGenerator->getBaseFont("Times New Roman", 10);pdfGenerator->drawText(QRectF(dataX, y, 400, 60), "lssued date", f, QColor(0,0,0), Qt::AlignLeft);y += 200;// 發證單位f = pdfGenerator->getBaseFont();pdfGenerator->drawText(QRectF(dataX, y, 600, 60), "發證單位:(蓋章位置)", f, QColor(0,0,0), Qt::AlignLeft);y += 90;// 發證單位 英文f = pdfGenerator->getBaseFont("Times New Roman", 10);pdfGenerator->drawText(QRectF(dataX, y, 400, 60), "lssued by(stamp)", f, QColor(0,0,0), Qt::AlignLeft);y += 200;// 地址f = pdfGenerator->getBaseFont("宋體", 10);f.setBold(true);pdfGenerator->drawText(QRectF(nameX, y, 600, 66), "地址(Add):", f, QColor(0,0,0), Qt::AlignLeft);f = pdfGenerator->getBaseFont("宋體", 10);pdfGenerator->drawText(QRectF(nameX + 330, y, 800, 66), "廣東省廣州市天河區xx街道xx村xx號", f, QColor(0,0,0), Qt::AlignLeft);// 郵編f = pdfGenerator->getBaseFont("宋體", 10);f.setBold(true);pdfGenerator->drawText(QRectF(nameX + 1100, y, 600, 66), "郵編(Post Code):", f, QColor(0,0,0), Qt::AlignLeft);f = pdfGenerator->getBaseFont("宋體", 10);pdfGenerator->drawText(QRectF(nameX + 1550, y, 300, 66), "123456", f, QColor(0,0,0), Qt::AlignLeft);y += 100;// 電話f = pdfGenerator->getBaseFont("宋體", 10);f.setBold(true);pdfGenerator->drawText(QRectF(nameX, y, 600, 66), "電話(Tel):", f, QColor(0,0,0), Qt::AlignLeft);f = pdfGenerator->getBaseFont("宋體", 10);pdfGenerator->drawText(QRectF(nameX + 330, y, 800, 66), "002-123456789", f, QColor(0,0,0), Qt::AlignLeft);// 傳真f = pdfGenerator->getBaseFont("宋體", 10);f.setBold(true);pdfGenerator->drawText(QRectF(nameX + 800, y, 600, 66), "傳真(Fax):", f, QColor(0,0,0), Qt::AlignLeft);f = pdfGenerator->getBaseFont("宋體", 10);pdfGenerator->drawText(QRectF(nameX + 1050, y, 300, 66), "002-123456789", f, QColor(0,0,0), Qt::AlignLeft);y += 100;// 電子郵箱f = pdfGenerator->getBaseFont("宋體", 10);f.setBold(true);pdfGenerator->drawText(QRectF(nameX, y, 600, 66), "電子信箱(E-mail):", f, QColor(0,0,0), Qt::AlignLeft);f = pdfGenerator->getBaseFont("宋體", 10);pdfGenerator->drawText(QRectF(nameX + 400, y, 800, 66), "youxiang666@qq.com", f, QColor(0,0,0), Qt::AlignLeft);pdfGenerator->endPage();// 通過其它PDF閱讀器來打開剛剛繪制的PDFQDesktopServices::openUrl(QUrl::fromLocalFile(path));
}
四、源碼分享
pdfgenerator.h
#ifndef PDF_GENERATOR_H
#define PDF_GENERATOR_H#include <QObject>
#include <QPdfWriter>
#include <QPainter>
#include <QFont>
#include <QImage>
#include <QPageSize>
#include <QFile>class PdfGenerator : public QObject {Q_OBJECT
public:explicit PdfGenerator(const QString &fileName, QPagedPaintDevice::PageSize size = QPagedPaintDevice::PageSize::A4, QObject *parent = nullptr);explicit PdfGenerator(QObject *parent = nullptr);~PdfGenerator();qreal getPdfWidth();qreal getPdfHeight();/*** @brief getBaseFont 獲得基本的字體* @param family* @param pointSize* @param weight* @return*/QFont getBaseFont(const QString &family = "宋體", int pointSize = 12, int weight = -1);/*** @brief setMargins 設置pdf邊距,分別是 左-上-右-下* @param left* @param top* @param right* @param bottom*/void setMargins(qreal left, qreal top, qreal right, qreal bottom);/*** @brief setResolution 設置分辨率,一般為300* @param dpi*/void setResolution(int dpi = 300);/*** @brief newPage 新建下一頁*/void newPage();/*** @brief beginPage 開始繪畫* @return 成功返回true;失敗返回false*/bool beginPage();/*** @brief endPage 結束會話* @return 成功返回true;失敗返回false*/bool endPage();/*** @brief setFileName 設置pdf文件名,內部會open* @param fileName* @param size* @return*/bool setFileName(const QString &fileName, QPagedPaintDevice::PageSize size = QPagedPaintDevice::PageSize::A4);/*** @brief drawLine 繪制線段* @param start 起始坐標* @param end 結束坐標* @param color 線段顏色* @param width 線段寬度*/void drawLine(const QPointF &start, const QPointF &end, const QColor &color = QColor(0,0,0), qreal width = 2);/*** @brief drawText 繪制文字* @param rect 繪制的位置(矩形)* @param text 繪制的文本* @param font 繪制字體* @param color 字體顏色* @param align 對齊*/void drawText(const QRectF &rect, const QString &text, const QFont &font, const QColor &color = QColor(0,0,0), Qt::Alignment align = Qt::AlignCenter);/*** @brief drawImage 繪制圖片* @param rect 繪制的位置(矩形)* @param imagePath 圖片路徑*/int drawImage(const QRectF &rect, const QString &imagePath);int drawImage(const QRectF &rect, const QPixmap &pixmap);int drawImage(const QRectF &rect, const QImage &image);/*** @brief drawRect 繪制矩形* @param rect 繪制的位置(矩形)* @param borderColor 邊框的顏色* @param borderWidth 邊框寬度* @param fillColor 填充的顏色,默認透明,不填充*/void drawRect(const QRectF &rect, const QColor &borderColor = QColor(0,0,0), qreal borderWidth = 2, const QColor &fillColor = Qt::transparent);/*** @brief drawEllipse 繪制橢圓* @param rect 繪制的位置(矩形)* @param borderColor 邊框的顏色* @param borderWidth 邊框寬度* @param fillColor 填充的顏色,默認透明,不填充*/void drawEllipse(const QRectF &rect, const QColor &borderColor = QColor(0,0,0), qreal borderWidth = 2, const QColor &fillColor = Qt::transparent);/*** @brief drawPolygon 畫三角形,三角形在矩形區域內* @param rect 繪制的位置(矩形)* @param borderColor 邊框的顏色* @param borderWidth 邊框寬度* @param fillColor 填充的顏色,默認透明,不填充*/void drawPolygon(const QRectF &rect, const QColor &borderColor = QColor(0,0,0), qreal borderWidth = 2, const QColor &fillColor = Qt::transparent);// 想設計其他繪制接口繼續往下加private:QPdfWriter *m_writer = nullptr;QPainter *m_painter = nullptr;/// pdf可繪制區域QRect m_pageRect;/// pdf文件QFile m_pdfFile;
};#endif // PDF_GENERATOR_H
pdfgenerator.cpp
#include "pdfgenerator.h"
#include <QtDebug>PdfGenerator::PdfGenerator(const QString &fileName, QPagedPaintDevice::PageSize size, QObject *parent) : QObject (parent)
{m_pdfFile.setFileName(fileName);m_writer = new QPdfWriter(&m_pdfFile);m_writer->setPageSize(size); // 設置紙張m_writer->setResolution(300); // 設置分辨率m_writer->setPageMargins(QMarginsF(20, 20, 20, 20), QPageLayout::Millimeter); // 設置頁邊距m_pageRect = m_writer->pageLayout().paintRectPixels(m_writer->resolution());// 計算可繪制區域m_pageRect = QRect(0, 0, m_writer->width(), m_writer->height());if(!m_pdfFile.open(QIODevice::WriteOnly))return ;}PdfGenerator::PdfGenerator(QObject *parent) : QObject (parent)
{}PdfGenerator::~PdfGenerator()
{if (m_painter) {if (m_painter->isActive()){m_painter->end();}delete m_painter;}if (m_writer) {m_writer->deleteLater();}
}qreal PdfGenerator::getPdfWidth()
{return m_pageRect.width();
}qreal PdfGenerator::getPdfHeight()
{return m_pageRect.height();
}QFont PdfGenerator::getBaseFont(const QString &family, int pointSize, int weight)
{QFont f(family, pointSize, weight);return f;
}void PdfGenerator::setMargins(qreal left, qreal top, qreal right, qreal bottom)
{m_writer->setPageMargins(QMarginsF(left, top, right, bottom), QPageLayout::Millimeter);m_pageRect = m_writer->pageLayout().paintRectPixels(m_writer->resolution()); // 更新繪制區域
}void PdfGenerator::setResolution(int dpi)
{m_writer->setResolution(dpi);
}void PdfGenerator::newPage()
{// 創建新頁m_writer->newPage();
}bool PdfGenerator::beginPage()
{bool bRet = false;if(nullptr == m_painter){m_painter = new QPainter(m_writer);}//啟用抗鋸齒m_painter->setRenderHint(QPainter::Antialiasing);if (nullptr != m_painter){m_painter->begin(m_writer);//m_painter->reset(new QPainter(m_writer.data()));bRet = m_painter->isActive();}qDebug() << "beginPage bRet is " << bRet;return bRet;
}bool PdfGenerator::endPage() {if (m_painter && m_painter->isActive()){m_painter->end();m_writer->deleteLater();m_pdfFile.close();return true;}m_pdfFile.close();return false;
}bool PdfGenerator::setFileName(const QString &fileName, QPagedPaintDevice::PageSize size)
{m_pdfFile.setFileName(fileName);// 打開創建并以寫的方式打開文件if(!m_pdfFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {return false;}m_writer = new QPdfWriter(&m_pdfFile);m_writer->setPageSize(size); // 設置紙張m_writer->setResolution(300); // 設置分辨率m_writer->setPageMargins(QMarginsF(20, 20, 20, 20), QPageLayout::Millimeter); // 設置頁邊距m_pageRect = m_writer->pageLayout().paintRectPixels(m_writer->resolution());// 計算可繪制區域m_pageRect = QRect(0, 0, m_writer->width(), m_writer->height());return true;
}// 繪制線段
void PdfGenerator::drawLine(const QPointF &start, const QPointF &end, const QColor &color, qreal width)
{if (!m_painter->isActive())return;m_painter->save();m_painter->setPen(QPen(color, width));m_painter->drawLine(start, end);m_painter->restore();
}// 繪制文本(支持對齊)
void PdfGenerator::drawText(const QRectF &rect, const QString &text, const QFont &font, const QColor &color, Qt::Alignment align)
{if (!m_painter->isActive())return;m_painter->save();m_painter->setFont(font);m_painter->setPen(color);m_painter->drawText(rect, static_cast<int>(align), text);m_painter->restore();
}// 繪制圖片(根據大小比例,來放大縮小圖片)
int PdfGenerator::drawImage(const QRectF &rect, const QString &imagePath)
{if (!m_painter->isActive())return -10;QPixmap pixmap;if (!pixmap.load(imagePath)) {return -1;}int nPdfWidth = m_pageRect.width();int imageBorder = rect.width();float x = (float)(nPdfWidth - imageBorder * 2) / (float)pixmap.width();pixmap = pixmap.scaled(nPdfWidth - imageBorder * 2, x * pixmap.height(), Qt::IgnoreAspectRatio); //根據大小比例,來放大縮小圖片m_painter->drawPixmap(imageBorder, static_cast<int>(rect.y()), pixmap);return pixmap.height();
}int PdfGenerator::drawImage(const QRectF &rect, const QPixmap &pixmap)
{if (!m_painter->isActive())return -1;if (pixmap.isNull())return -1;int nPdfWidth = m_pageRect.width();int imageBorder = rect.width();float x = (float)(nPdfWidth - imageBorder * 2) / (float)pixmap.width();QPixmap pixmapTmp = pixmap.scaled(nPdfWidth - imageBorder * 2, x * pixmap.height(), Qt::IgnoreAspectRatio); //根據大小比例,來放大縮小圖片m_painter->drawPixmap(imageBorder, static_cast<int>(rect.y()), pixmapTmp);return pixmapTmp.height();
}int PdfGenerator::drawImage(const QRectF &rect, const QImage &image)
{if (!m_painter->isActive())return -1;if (image.isNull())return -1;int nPdfWidth = m_pageRect.width();int imageBorder = rect.width();float x = (float)(nPdfWidth - imageBorder * 2) / (float)image.width();QImage imageTmp = image.scaled(nPdfWidth - imageBorder * 2, x * image.height(), Qt::IgnoreAspectRatio); //根據大小比例,來放大縮小圖片m_painter->drawImage(rect, image);return image.height();
}// 繪制矩形(支持填充)
void PdfGenerator::drawRect(const QRectF &rect, const QColor &borderColor, qreal borderWidth, const QColor &fillColor)
{if (!m_painter->isActive())return;m_painter->save();m_painter->setBrush(QBrush(fillColor)); // 填充m_painter->setPen(QPen(borderColor, borderWidth));m_painter->drawRect(rect);m_painter->restore();
}// 繪制橢圓
void PdfGenerator::drawEllipse(const QRectF &rect, const QColor &borderColor, qreal borderWidth, const QColor &fillColor)
{if (!m_painter->isActive())return;m_painter->save();m_painter->setBrush(QBrush(fillColor));m_painter->setPen(QPen(borderColor, borderWidth));m_painter->drawEllipse(rect);m_painter->restore();
}void PdfGenerator::drawPolygon(const QRectF &rect, const QColor &borderColor, qreal borderWidth, const QColor &fillColor)
{if (!m_painter->isActive())return;QPen pen(borderColor, borderWidth); // 底邊線寬為5m_painter->setPen(pen);QBrush brush(fillColor);m_painter->setBrush(brush);QPoint points[3] = {QPoint(rect.x(), rect.y() + rect.height()), // 左下角點QPoint(rect.x() + rect.width(), rect.y() + rect.height()), // 右下角點QPoint((rect.width() + rect.x() + rect.x()) / 2, rect.y()) // 上頂點};m_painter->drawPolygon(points, 3);
}