简体   繁体   中英

export widget in PDF file

I would like to export a label in a pdf file. I got the error message:

'QLabel' object has no attribute 'document'

I managed to do it with textedit (that's why the textedit is still in the code). Any idea of how I should do it ?

thanks

import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow,  QAction, QTextEdit, QFileDialog,QLabel
from PyQt5.QtPrintSupport import QPrinter
from PyQt5.Qt import QFileInfo


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "PyQt5 export pdf"
        self.top = 200
        self.left = 500
        self.width = 680
        self.height = 480
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.createEditor()
        self.CreateMenu()
        self.show()

    def CreateMenu(self):
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('File')
        exportpdfAction = QAction(QIcon("pdf.png"), "Export PDF", self)
        exportpdfAction.triggered.connect(self.printPDF)
        fileMenu.addAction(exportpdfAction)

    def createEditor(self):
        self.label = QLabel("I would like to print this")
        self.textEdit = QTextEdit(self) #I can print that if I want
        self.setCentralWidget(self.label)

    def printPDF(self):
        fn, _ = QFileDialog.getSaveFileName(self, 'Export PDF', None, 'PDF files (.pdf);;All Files()')
        if fn != '':
            if QFileInfo(fn).suffix() == "" : fn += '.pdf'
            printer = QPrinter(QPrinter.HighResolution)
            printer.setOutputFormat(QPrinter.PdfFormat)
            printer.setOutputFileName(fn)
            self.label.document().print_(printer)


App = QApplication(sys.argv)
window = Window()
App.exec()

QTextEdit has a QTextDocument that has a print_() method, but it is not the case with QLabel so you will have to use the render() method with QPainter :

from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport


def print_widget(widget, filename):
    printer = QtPrintSupport.QPrinter(QtPrintSupport.QPrinter.HighResolution)
    printer.setOutputFormat(QtPrintSupport.QPrinter.PdfFormat)
    printer.setOutputFileName(filename)
    painter = QtGui.QPainter(printer)

    # start scale
    xscale = printer.pageRect().width() * 1.0 / widget.width()
    yscale = printer.pageRect().height() * 1.0 / widget.height()
    scale = min(xscale, yscale)
    painter.translate(printer.paperRect().center())
    painter.scale(scale, scale)
    painter.translate(-widget.width() / 2, -widget.height() / 2)
    # end scale

    widget.render(painter)
    painter.end()


class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        title = "PyQt5 export pdf"
        top, left, width, height = 200, 500, 680, 480
        self.setWindowTitle(title)
        self.setGeometry(left, top, width, height)
        self.createEditor()
        self.CreateMenu()
        self.show()

    def CreateMenu(self):
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu("File")
        exportpdfAction = QtWidgets.QAction(QtGui.QIcon("pdf.png"), "Export PDF", self)
        exportpdfAction.triggered.connect(self.printPDF)
        fileMenu.addAction(exportpdfAction)

    def createEditor(self):
        self.label = QtWidgets.QLabel("I would like to print this")
        self.setCentralWidget(self.label)

    def printPDF(self):
        fn, _ = QtWidgets.QFileDialog.getSaveFileName(
            self, "Export PDF", None, "PDF files (.pdf);;All Files()"
        )
        if fn:
            if QtCore.QFileInfo(fn).suffix() == "":
                fn += ".pdf"

            print_widget(self.label, fn)


if __name__ == "__main__":
    import sys

    App = QtWidgets.QApplication(sys.argv)
    window = Window()
    App.exec()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM