QT将HTML和CSS转换为PDF文件

4
我正在使用QTextDocument并使用setHtml添加HTML。然后我使用QPrinter以A4 PDF格式QPrinter::PdfFormat打印,但打印出的PDF没有采用CSS样式表。我还尝试了QTextDocument::setDefaultHtmlsetResource
以下是代码。如何在PDF格式中获取CSS样式? 我使用ubuntu和qmake进行编译。
const int highQualityDPI = 300;
QDir::setCurrent(QCoreApplication::applicationDirPath());

QFile  htmlFile ("myhtml.html");
if (!htmlFile.open(QIODevice::ReadOnly | QIODevice::Text)){
    return -1;
}

QString htmlContent;
QTextStream in(&htmlFile);
htmlContent=in.readAll();

QFile  cssFile ("style.css");
if (!cssFile.open(QIODevice::ReadOnly | QIODevice::Text)){
    return -1;
}
QString cssContent;
QTextStream cssIn(&cssFile);
cssContent = cssIn.readAll();

QTextDocument *document = new QTextDocument();
document->addResource( QTextDocument::StyleSheetResource, QUrl( "style.css" ), cssContent );
document->setHtml( htmlContent ); 

QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);

printer.setOutputFileName("output.pdf");

document->print(&printer);
delete document;
return 0;

QTextDocument渲染富文本,而不是HTML。支持的HTML和CSS部分可以在这里看到。 - Konstantin T.
要打印HTML,您可以使用QWebEngineView :: render(QPaintDevice *,const QPoint&,const QRegion&,RenderFlags) - Konstantin T.
那么如何将HTML和CSS文件制作成PDF呢? - Ayan Banerjee
1
就像我说的那样,你可以使用QWebEngineView。它支持HTML和CSS。 - Konstantin T.
我认为QWebEngineView不能用于打印PDF。你能告诉我有哪些打印PDF的函数可以使用吗? - Ayan Banerjee
我在qmake中使用g++,因此我认为不支持QT += webenginewidgets,所以QWebEngineView可在所有编译器平台上使用,那么可以使用QWebKit吗? - Ayan Banerjee
1个回答

2

使用 QWebEngineViewQWebEnginePage 怎么样?只需要在 WebView 中加载您的 HTML 文件,然后调用 printToPdf 函数即可:

 const QString fileName = QFileDialog::getSaveFileName(0,
                                                tr("Save pdf"),
                                                getLastDirectory(),
                                                tr("PDF Files (*.pdf)"));
if (fileName.isEmpty()) {
    return;
}
ui->webView->page()->printToPdf(fileName);

要与QWebKit一起使用:

    // Set location of resulting PDF
    const QString fileName = QFileDialog::getSaveFileName(0,
                                                tr("Save pdf"),
                                                getLastDirectory(),
                                                tr("PDF Files (*.pdf)"));
    if (fileName.isEmpty()) {
       return;
    } 

    QPrinter printer(QPrinter::HighResolution);
    printer.setOutputFileName(fileName);

    // Create PDF
    ui->webview->print(&printer);

如何加载带有CSS的HTML文件?如果你的HTML在同一文件夹中正确链接了CSS文件:
QFile  htmlFile ("myhtml.html");
ui->webView->load(QUrl::fromLocalFile(QFileInfo(htmlFile).absoluteFilePath()));

我在qmake中使用g++,因此我认为不支持QT += webenginewidgets,所以QWebEngineView可在所有编译器平台上使用,那么QWebKit可以使用吗? - Ayan Banerjee
我添加了一些行,展示如何在较旧的QWebKit中打印。 - mohabouje
但是QWebView没有提供CSS的感觉。 - Ayan Banerjee
我已经添加了一些代码行,以向您展示如何正确加载HTML。 - mohabouje
ui->webView->load(QUrl::fromLocalFile(QFileInfo(file).path());这行代码存在多个问题。首先,括号不正确。其次,file变量是什么?我看你直接使用了“myhtml.html”,对吗? - Ayan Banerjee
抱歉,我没有编译代码。在我的Qt 5.4和Ubuntu 14上运行良好。 - mohabouje

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接