3
안녕하세요, 나는 특정 웹 사이트를 표시하는 사용자 지정 QtWebKit.QWebView가있는 QWidget이 있습니다. 파이썬과 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 용지에 인쇄 할 수 있습니다.
지금 웹 페이지의 스크린 샷은 매우 길어서 한 페이지 이상에 표시되어야합니다. 당신은 PDF로 웹 페이지를 저장하려면
감사합니다. 그런데 코드로 생성 된 report.pdf를 열면 그 종류가 흐릿 해져서 거의 200 % 줌이 걸리므로 텍스트가 좀 더 명확 해집니다. 어떤 생각을 어떻게 해결할 것인가. – TheCreator232
@ thecreator232. 난 몇 가지 임의의 페이지를 시도하고 어떤 중요한 흐리게 얻을하지 않습니다. 나는 당신이 Windows에있는 경우 도움이 될 내 대답에 대한 가능한 한 가지 해결책을 추가했습니다. – ekhumoro
@ TheCreator232 : 나는 당신의 방법을 모두 시도해보고 png에 저장하고 pdf로 저장하십시오. 필자의 의견으로는 png 결과를 저장 한 다음 어떤 식 으로든 사용하는 것이 더 좋습니다. 긴 PNG를 PNG로 슬라이스 한 다음 여러 페이지 PDF를 결합하여 여러 페이지 PDF를 만듭니다. 이것은 픽셀 완벽한 화면 스냅 결과를 생성합니다 (실제로 내 의도입니다) 이 게시물 주셔서 감사합니다! – swdev