我正在遵循这个示例:如何从 QWebEngineView 打印:
QWebEnginePage page;
QString outputPath="/home/output.pdf";
QString inputPath="/home/input.html";
QObject::connect(&page, &QWebEnginePage::loadFinished, [&page](){page.printToPdf(outputPath); qDebug()<<123;});
page.load(QUrl::fromLocalFile(inputPath));
代码执行没有错误,但没有打印 PDF,也没有调试消息(槽从未执行过?)。
我错过了什么?
QWebEnginePage::printToPdf
说:
[...]
此方法发出将网页打印为 PDF 的异步请求并立即返回。要了解请求的结果,请连接到信号 pdfPrintingFinished()。
QWebEnginePage::pdfPrintingFinished
说:
将网页打印成 PDF 文件完成后,会发出此信号。 filePath 将包含请求创建文件的路径,如果文件创建成功,则 success 为 true,否则为 false。
#include <QApplication>
#include <QtWebEngineCore/QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWebEnginePage page;
QObject::connect(&page, &QWebEnginePage::loadFinished, [&page](bool ok)
{
if(!ok)
{
qDebug()<<"Loading failed";
return;
}
page.printToPdf("/home/output.pdf");
});
QObject::connect(&page, &QWebEnginePage::pdfPrintingFinished, [](const QString &filePath, bool success)
{
qDebug() << (success ? "PDF printed and saved successfully at: " + filePath : "PDF printing failed");
});
page.load(QUrl::fromLocalFile("/home/someUser/Desktop/input.html"));
return a.exec();
}
输出:
PDF打印失败
这意味着加载 HTML 文件没有问题,因为它已经进行了打印。
QFileInfo::isWritable
检查写入权限:
#include <QApplication>
#include <QtWebEngineCore/QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWebEnginePage page;
QObject::connect(&page, &QWebEnginePage::loadFinished, [&page](bool ok)
{
if(!ok)
{
qDebug()<<"Loading failed";
return;
}
QString outputPath = "/home/";
QFileInfo outputDir(outputPath);
if(outputDir.isDir())
{
if(outputDir.isWritable())
page.printToPdf(outputPath+"output.pdf");
else
qDebug() << outputPath << "directory does not have write permission";
}
else
{
qDebug() << outputPath << "is not a directory";
}
});
QObject::connect(&page, &QWebEnginePage::pdfPrintingFinished, [](const QString &filePath, bool success)
{
qDebug() << (success ? "PDF printed and saved successfully at: " + filePath : "PDF printing failed");
});
page.load(QUrl::fromLocalFile("/home/someUser/Desktop/input.html"));
return a.exec();
}
QDir::homePath()
获取用户主目录的绝对路径:
#include <QApplication>
#include <QtWebEngineCore/QWebEnginePage>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWebEnginePage page;
QObject::connect(&page, &QWebEnginePage::loadFinished, [&page](bool ok)
{
if(!ok)
{
qDebug()<<"Loading failed";
return;
}
const QFileInfo outputDir(QDir::homePath());
const QString outputPath = QDir::homePath() + "/";
if(outputDir.isDir())
{
if(outputDir.isWritable())
{
page.printToPdf(outputPath+"output.pdf");
qDebug()<<outputPath+"output.pdf";
}
else
qDebug() << outputPath << "directory does not have write permission";
}
else
{
qDebug() << outputPath << "is not a directory";
}
});
QObject::connect(&page, &QWebEnginePage::pdfPrintingFinished, [](const QString &filePath, bool success)
{
qDebug() << (success ? "PDF printed and saved successfully at: " + filePath : "PDF printing failed");
});
page.load(QUrl::fromLocalFile("/home/someUser/Desktop/input.html"));
return a.exec();
}
输出:
PDF 已成功打印并保存在:/home/someUser/output.pdf