我在這個問題上糾結了幾次。我需要通過創建包含一些資料的 pdf 檔案來創建報告。所以我的計劃是不使用QTextCursor,而是使用 HTML CSS 在我的 pdf 檔案中創建內容。不幸的是,我無法使用QWebEngine小部件解決方案,因為我正在編譯到 MinGW 并且設定 MVSC 2017 環境非常混亂。可悲的是,我不得不使用QTextDocument 和他的 HTML 子集支持。
我的問題是我無法在我的 pdf 檔案中放置背景圖片。
我想將此影像作為背景影像:
讓我抓狂的是,我使用 css 屬性在我的 pdf 檔案中顯示背景影像。
background-image: url(:/images/blue-gradient-background.jpg);
并且似乎 Qt 考慮到了這一點,但它仍然不顯示背景。
看一看:
第一個 pdf 檔案不包含 html 內容上的 css 屬性背景影像,但第二個包含,您可以看到檔案的大小更大,因此它考慮了 css 屬性。但它沒有顯示背景:
左邊是第一個檔案,右邊是第二個具有 css 屬性的檔案。
我使用資源中的 html 檔案來生成報告:
<!DOCTYPE html>
<html>
<style>
body {
background-image: url(:/images/blue-gradient-background.jpg);
}
h1 {
color: blue;
}
#table-content {
}
#table-content table {
margin: 1em
}
</style>
<body>
<div align="center"><h1>[title-template]</h1></div>
<div align="center" id="table-content">[table-template]</div>
</body>
</html>
這個 html 檔案在類的建構式中打開并讀取:
FileEditor::FileEditor()
{
QFile htmlTemplate(":/html/report.html");
if (htmlTemplate.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&htmlTemplate);
m_reportPageHTMLTemplate = in.readAll();
}
}
然后我使用具有此功能的自定義類,該類使用QPrinter和QPainter來寫入資料:
void FileEditor::writeReportPdfFile(const QString &i_path, const S12xConf &i_s12xConf, const S12xData &i_s12xData, const BenchTestData &i_benchTestData)
{
QDir directory(i_path); // we create a QDir with the path
if (!directory.exists()) // if the directory does not exist then
{
directory.mkpath("."); // we create the path
}
QString fileName = getCurrentHourDate() "_S12xNG_Test_Report.pdf";
QPrinter pdfPrinter(QPrinter::HighResolution);
pdfPrinter.setOutputFormat(QPrinter::PdfFormat);
pdfPrinter.setPageSize(QPageSize(QPageSize::A4));
pdfPrinter.setOutputFileName(i_path "/" fileName);
pdfPrinter.setFullPage(true);
QPainter painter(&pdfPrinter);
painter.setBackgroundMode(Qt::OpaqueMode);
QTextDocument measuresDoc;
QTextDocument averagesDoc;
QTextDocument inOutDoc;
measuresDoc.documentLayout()->setPaintDevice(&pdfPrinter);
measuresDoc.setPageSize(pdfPrinter.pageRect().size());
averagesDoc.documentLayout()->setPaintDevice(&pdfPrinter);
averagesDoc.setPageSize(pdfPrinter.pageRect().size());
inOutDoc.documentLayout()->setPaintDevice(&pdfPrinter);
inOutDoc.setPageSize(pdfPrinter.pageRect().size());
setMeasuresTextDocument(&measuresDoc);
setAveragesTextDocument(&averagesDoc);
setInputsOutputsTextDocument(&inOutDoc);
measuresDoc.drawContents(&painter); // We print TextDocument of the Measures into the document
pdfPrinter.newPage(); // We inject the current page and continue printing on new page
averagesDoc.drawContents(&painter); // We print the TextDocument of the Averages into the document
pdfPrinter.newPage(); // We inject the current page and continue printing on new page
inOutDoc.drawContents(&painter); // We print the TextDocument of the Inputs/Outputs into the document
measuresDoc.undo();
averagesDoc.undo();
inOutDoc.undo();
}
這是將 html 資料添加到度量 QTextDocument 的函式:
void FileEditor::setMeasuresTextDocument(QTextDocument *o_measuresDoc)
{
QString htmlContent = m_reportPageHTMLTemplate;
htmlContent.replace("[title-template]","I. Measures Data");
o_measuresDoc->setHtml(htmlContent);
}
那么我做錯了什么?提前感謝您的回復。
uj5u.com熱心網友回復:
我找到了答案!
它是關于 QTextDocument 的 drawContents 方法的。您需要添加第二個引數才能查看檔案中的背景影像。它是一個 QRectF 物件,代表背景影像將被剪裁到的矩形的大小。
void QTextDocument::drawContents(QPainter *p, const QRectF &rect = QRectF()) 用painter p繪制檔案的內容,剪裁為矩形。如果 rect 是一個空矩形(默認),則檔案被繪制為未剪裁。
所以我所做的是:
easuresDoc.drawContents(&painter, pdfPrinter.paperRect()); // We print TextDocument of the Measures into the document
pdfPrinter.newPage(); // We inject the current page and continue printing on new page
averagesDoc.drawContents(&painter, pdfPrinter.paperRect()); // We print the TextDocument of the Averages into the document
pdfPrinter.newPage(); // We inject the current page and continue printing on new page
inOutDoc.drawContents(&painter, pdfPrinter.paperRect()); // We print the TextDocument of the Inputs/Outputs into the document
pdfPrinter.paperRect 是沒有邊距的頁面矩形。
此外,如果您希望不重復地列印背景影像,則需要將列印機放入QPrinter::PrinterResolution
QPrinter pdfPrinter(QPrinter::PrinterResolution);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/428367.html
標籤:html css qt pdf qtextdocument
