本帖最后由 hrpzcf 于 2022-12-4 19:57 编辑
发问题请带上能复现问题的最小代码,越难的问题越需要。
等待加载完成再保存 PDF
# coding: utf-8
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import *
# class MainWindow(QMainWindow):
# def __init__(self):
# super().__init__()
# self.centerw = QWidget()
# self.wpage = QWebEngineView()
# self.wpage.load(QUrl("https://fishc.com.cn/forum-173-1.html"))
# self.btn = QPushButton("保存")
# self.lay = QVBoxLayout()
# self.lay.addWidget(self.wpage, 1)
# self.lay.addWidget(self.btn, 0)
# self.btn.clicked.connect(self.save_pdf)
# self.centerw.setLayout(self.lay)
# self.setCentralWidget(self.centerw)
# self.show()
# def save_pdf(self):
# self.wpage.page().printToPdf("pppppp.pdf")
# print("已保存, 耐心等待 pdf 文件出现...")
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.page = QWebEnginePage()
self.page.load(QUrl("https://fishc.com.cn/forum-173-1.html"))
self.btn = QPushButton("保存", self)
self.btn.clicked.connect(self.save_pdf)
self.btn.setGeometry(10, 10, 75, 28)
self.show()
def save_pdf(self):
self.page.printToPdf("pppppp.pdf")
print("已保存, 耐心等待 pdf 文件出现...")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
|