Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

print Full webview



  • I want to print out the full webpage content more than one pages. Below is the code which I tried to print.

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport, QtWebEngineWidgets
    class main(QtWebEngineWidgets.QWebEngineView):
    def init(self,windows,parent = None):
    super(main,self).init(parent)

        self._windows = windows
        self._windows.append(self)
        self.load(QtCore.QUrl("https://twitter.com/CodeRentor"))
    
    #Menu items on mouse Right click for Print 
    def contextMenuEvent(self, event):       
        cmenu = QtWidgets.QMenu(self)
    
        printAct = cmenu.addAction("print")
        action = cmenu.exec_(self.mapToGlobal(event.pos()))
        if action == printAct:
            self._preview()
    
    def createWindow(self, windows):
        if windows == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
            webView = main(self._windows)
            webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
            webView.resize(900, 780) # <----
            webView.show()
            return webView
        elif windows == QtWebEngineWidgets.QWebEnginePage.WebDialog:
            webView = main(self._windows)
            webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
            webView.resize(900, 780) # <----
            webView.show()
            return webView
        return super(main, self).createWindow(windows)
    
    def _preview(self):
        dialog = QtPrintSupport.QPrintPreviewDialog()
        dialog.paintRequested.connect(self._request)
        dialog.exec_()
    def _request(self,printer):
        printer.newPage()
        painter = QtGui.QPainter()
        painter.begin(printer)
        screen = self.grab()
        painter.drawPixmap(0, 0, screen)
        painter.end()
    

    if name == "main":
    app = QtWidgets.QApplication(sys.argv)
    windows = []
    web = main(windows)
    web.show()
    sys.exit(app.exec_())

    I want to print out the full webpage content more than one pages. Below is the code which I tried to print.

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport,tWebEngineWidgets
    class main(QtWebEngineWidgets.QWebEngineView):
     def __init__(self,windows,parent = None):
           super(main,self).__init__(parent)
    
         self._windows = windows
        self._windows.append(self)
        self.load(QtCore.QUrl("https://twitter.com/CodeRentor"))
    
    #Menu items on mouse Right click for Print 
    def contextMenuEvent(self, event):       
        cmenu = QtWidgets.QMenu(self)
    
        printAct = cmenu.addAction("print")
        action = cmenu.exec_(self.mapToGlobal(event.pos()))
        if action == printAct:
            self._preview()
    
    def createWindow(self, windows):
        if windows == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
            webView = main(self._windows)
            webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
            webView.resize(900, 780) # <----
            webView.show()
            return webView
        elif windows == QtWebEngineWidgets.QWebEnginePage.WebDialog:
            webView = main(self._windows)
            webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
            webView.resize(900, 780) # <----
            webView.show()
            return webView
        return super(main, self).createWindow(windows)
    
    def _preview(self):
        dialog = QtPrintSupport.QPrintPreviewDialog()
        dialog.paintRequested.connect(self._request)
        dialog.exec_()
    def _request(self,printer):
        printer.newPage()
        painter = QtGui.QPainter()
        painter.begin(printer)
        screen = self.grab()
        painter.drawPixmap(0, 0, screen)
        painter.end()
    if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    windows = []
    web = main(windows)
    web.show()
     sys.exit(app.exec_())
    

    through above the code only able to print the current present on screen but i want to print the whole web page continously.

    I also tried to print through QtextDocument But that unable to detect the webpage css or javascripts correctly. Below is the code which is tried also

    myDocument = QTextDocument()
    myDocument.setHtml(self.currentHtml) # <= self.currentHtml is the current page 
    converted to Html
    font = QFont()
    myDocument.setDefaultFont(font)
    myDocument.defaultStyleSheet()
    myDocument.setPageSize(QSizeF(printer.pageRect().size()))
    myDocument.print_(printer)

Log in to reply