我的目標:我希望能夠使用qt 5.6.1將qrc中的HTML頁面存儲爲PDF文件。將QWebEngineView呈現給打印機
限制:
- 我不能使用QWebEnginePage ::打印也不QWebEnginePage :: printToPdf方法,因爲它們已經在5.8版本和5.7分別爲
- 使用被添加QTextDocument而不是一個選項,因爲我需要一個完整的HTML支持,而不僅僅是有限的子集QTextDocument報價
問題:下面確實的代碼顯示在屏幕上的頁面,那麼doc.pdf被創建,這意味着該頁面已經被加載沒有錯誤,但是當我打開創建的PDF文件的空白頁面顯示。
任何想法我做錯了或失蹤?
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWebEngineView *view = new QWebEngineView(this);
setCentralWidget(view);
QFile file(":/print.htm");
QString str;
if (file.open(QFile::ReadOnly | QFile::Text))
{
str.append(file.readAll());
view->setHtml(str);
file.close();
}
connect(view, &QWebEngineView::loadFinished, this, &MainWindow::on_loadFinished);
}
void MainWindow::on_loadFinished(bool ok)
{
if (ok)
{
QPrinter printer(QPrinter::HighResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPageSize(QPageSize(QPageSize::A4));
printer.setPageOrientation(QPageLayout::Portrait);
printer.setColorMode(QPrinter::GrayScale);
printer.setOutputFileName("doc.pdf");
static_cast<QWebEngineView *>(sender())->render(&printer);
}
}
在我看來,更新是唯一的解決方案:http://lists.qt-project.org/pipermail/interest/2016-April/021781 .html或https://forum.qt.io/topic/53238/q webengineview-print-method – m7913d
是的。我已經做到了,現在使用QWebEnginePage :: printToPdf方法。謝謝(你的)信息! – scopchanov
@ m7913d順便說一句,作爲答案,我會接受它。讓未來遇到這個問題的人知道該怎麼做。 – scopchanov