【发布时间】:2017-06-12 13:29:02
【问题描述】:
我的目标:我希望能够使用 qt 5.6.1 将来自 qrc 的 HTML 页面存储为 PDF 文件。
限制:
- 我无法使用 QWebEnginePage::print 和 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);
}
}
【问题讨论】:
-
是的。我已经这样做了,现在我使用 QWebEnginePage::printToPdf 方法。感谢您的信息!
标签: c++ windows qt pdf rendering