如何使用PyQt5将本地html文件转换为pdf?

问题描述

我尝试使用wkhtml和weasyprint库,但是所有这些都呈现空白的pdf页面。唯一起作用的选项是pdfcrowd,但这是一个付费库。我发现使用PyQt转换网页的几个选项:

import sys
from PyQt5 import QtCore,QtWidgets,QtWebEngineWidgets

app = QtWidgets.QApplication(sys.argv)
loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)
loader.page().pdfPrintingFinished.connect(
    lambda *args: print('finished:',args))
loader.load(QtCore.QUrl('https://en.wikipedia.org/wiki/Main_Page'))

def emit_pdf(finished):
    loader.show()
    loader.page().printToPdf("test.pdf")

loader.loadFinished.connect(emit_pdf)

app.exec()

但是,我不太确定如何使它适应本地保存的html文件

解决方法

您必须使用QUrl.fromLocalFile()将文件路径作为URL传递,也不必创建QWebEngineView,而只需使用QWebEnginePage:

import os
import sys

from PyQt5 import QtCore,QtWidgets,QtWebEngineWidgets


def html_to_pdf(html,pdf):
    app = QtWidgets.QApplication(sys.argv)

    page = QtWebEngineWidgets.QWebEnginePage()

    def handle_print_finished(filename,status):
        print("finished",filename,status)
        QtWidgets.QApplication.quit()

    def handle_load_finished(status):
        if status:
            page.printToPdf(pdf)
        else:
            print("Failed")
            QtWidgets.QApplication.quit()

    page.pdfPrintingFinished.connect(handle_print_finished)
    page.loadFinished.connect(handle_load_finished)
    page.load(QtCore.QUrl.fromLocalFile(html))
    app.exec_()


if __name__ == "__main__":

    CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
    filename = os.path.join(CURRENT_DIR,"index.html")
    print(filename)

    html_to_pdf(filename,"test.pdf")