3
嗨,我試圖完成的是,我有一個QWidget與顯示特定網站的自定義QtWebKit.QWebView。我想使用python和PyQt將該網頁保存爲pdf。從QtWebKit.webView打印網頁到pdf PyQt4
from PyQt4 import QtCore, QtGui
from PyQt4 import QtWebKit
from Save_Evidence import *
import sys
##### Custom WebView #############
class Browser(QtWebKit.QWebView):
def __init__(self,parent =None):
QtWebKit.QWebView.__init__(self,parent)
self.loadFinished.connect(self._result_available)
def _result_available(self, ok):
frame = self.page().mainFrame()
self.html = unicode(frame.toHtml()).encode('utf-8')
#################################
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Form(QtGui.QWidget):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(640, 480)
self.gridLayout = QtGui.QGridLayout(Form)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.webView = Browser(Form)
self.webView.setUrl(QtCore.QUrl(_fromUtf8("https://malwr.com/submission/")))
self.webView.setObjectName(_fromUtf8("webView"))
self.verticalLayout.addWidget(self.webView)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.setDisabled(True)
self.horizontalLayout.addWidget(self.pushButton)
self.buttonBox = QtGui.QDialogButtonBox(Form)
self.buttonBox.setLayoutDirection(QtCore.Qt.RightToLeft)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel)
self.buttonBox.setCenterButtons(False)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.horizontalLayout.addWidget(self.buttonBox)
self.verticalLayout.addLayout(self.horizontalLayout)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton , QtCore.SIGNAL("clicked()") , self.saveReport)
QtCore.QObject.connect(self.webView , QtCore.SIGNAL(" loadFinished (bool)") , self.printIt)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Malwr.com", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Form", "Save Report", None, QtGui.QApplication.UnicodeUTF8))
def printIt(self, val):
if str(self.webView.url().toString()).find("https://malwr.com/analysis/") == 0:
xpage = self.webView.page()
self.HTML = unicode(xpage. currentFrame().toHtml()).encode('utf-8')
f =xpage. currentFrame().contentsSize()
self.pushButton.setEnabled(True)
def saveReport(self):
self.webView.page().setViewportSize(xpage. currentFrame().contentsSize())
image = QtGui.QImage(self.webView.page().viewportSize(),QtGui.QImage.Format_ARGB32)
painter = QtGui.QPainter(image)
self.webView.page().mainFrame().render(painter)
painter.end()
image.save(QtCore.QString("output-report"),"png")
output = QtCore.QFile()
output.open(1, QtCore.QIODevice.WriteOnly)
image.save(output, 'PNG')
class MyForm(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
這是我現有的代碼,它允許我將網頁保存爲圖像。我需要以這種方式保存網頁,以便將其打印到A4紙上。
現在網頁的屏幕截圖很長(高度),所以它必須在多個頁面上顯示。
謝謝。 順便說一下,當我打開由代碼生成的report.pdf,它是一種模糊,它需要我近200%的縮放,使文本更清晰一點。任何想法如何解決這個問題。 – TheCreator232
@ thecreator232。我嘗試了幾個隨機頁面,沒有得到任何明顯的模糊。如果您使用的是Windows,我的答案中增加了一個可能的解決方案。 – ekhumoro
@TheCreator232:我已經嘗試了兩種方法,保存爲PNG並保存爲PDF格式。在我看來,最好使用保存到PNG結果,然後 - 以某種方式 - 將長PNG分割成PNG,然後將它們組合起來以創建多頁PDF。這將創建一個像素完美的屏幕捕捉結果(這實際上是我的意圖) 感謝這篇文章! – swdev