Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将网页从QtWebKit.webView打印到pdf PyQt4_Python_Python 2.7_Pyqt_Pdf Generation_Qtwebkit - Fatal编程技术网

Python 将网页从QtWebKit.webView打印到pdf PyQt4

Python 将网页从QtWebKit.webView打印到pdf PyQt4,python,python-2.7,pyqt,pdf-generation,qtwebkit,Python,Python 2.7,Pyqt,Pdf Generation,Qtwebkit,嗨,我想实现的是,我有一个QWidget,它带有一个定制的QtWebKit.QWebView,显示一个特定的网站。我想使用python和PyQt将该网页保存为pdf from PyQt4 import QtCore, QtGui from PyQt4 import QtWebKit from Save_Evidence import * import sys ##### Custom WebView ############# class Browser(QtWebKit.QWebView):

嗨,我想实现的是,我有一个QWidget,它带有一个定制的QtWebKit.QWebView,显示一个特定的网站。我想使用python和PyQt将该网页保存为pdf

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纸上


目前,该网页的屏幕截图相当长(高度),因此必须在多个网页上显示。

如果要将网页另存为pdf,请使用:

编辑

如果您使用的是Windows,则可以通过以下方式提高质量:

    printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)

谢谢顺便说一句,当我打开代码生成的report.pdf时,它有点模糊,我几乎需要200%的缩放才能使文本更清晰。“你知道怎么解决这个问题吗?”创造者说。我尝试了一些随机页面,没有得到任何明显的模糊。我在我的答案中添加了一个可能的解决方案,如果您使用Windows,该解决方案可能会有所帮助。@TheCreator 232:我已经尝试了您的两种方法,保存为png和保存为pdf。在我看来,最好使用save to png结果,然后——在某种程度上——将长的png分割成png,然后将它们组合起来创建一个多页的PDF。这将创造一个像素完美的屏幕抓拍结果(这实际上是我的意图)感谢这篇文章!
    printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)