how to print html page with image in Qprinter pyqt5

后端 未结 1 1598
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-22 18:38

i have generated a report for my program using html code but it doesn\'t show image in Qprinter.

def run(self):
    view = QtWebEngineWidgets.QWebEngineView()
           


        
1条回答
  •  盖世英雄少女心
    2021-01-22 19:07

    Qt Webengine executes the tasks asynchronously as printing, and since view and printer are local variables they will be eliminated when the synchro function is finished. The solution is to keep those objects even when you finish running.

    Not necessary to use QWebEngineView since you will not show anything, just QWebEnginePage.

    On the other hand the docs states that external resources such as images are loaded based on the URL that is passed second parameter. So the solution is to pass a url using the current directory as a basis.

    import os
    # ...
    def run(self):
        current_dir = os.path.dirname(os.path.abspath(__file__))
        self._page = QtWebEngineWidgets.QWebEnginePage()
        self._page.setHtml('''
        ... logo ...
        ''', QtCore.QUrl.fromLocalFile(os.path.join(current_dir, "index.html")))
        self._printer = QtPrintSupport.QPrinter()
        self._printer.setPaperSize(QtCore.QSizeF(80 ,297), QtPrintSupport.QPrinter.Millimeter)
        r = QtPrintSupport.QPrintDialog(self._printer)
        if r.exec_() == QtPrintSupport.QPrintDialog.Accepted:
            self._page.print(self._printer, self.print_completed)
    

    0 讨论(0)
提交回复
热议问题