我的目标:我希望能够使用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