在 PDF 文件中导出小部件

export widget in PDF file

我想导出 pdf 文件中的标签。我收到错误消息:

'QLabel' object has no attribute 'document'

我设法用 textedit 做到了(这就是 textedit 仍在代码中的原因)。知道我应该怎么做吗?

谢谢

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 有一个 QTextDocument 有一个 print_() 方法,但 QLabel 不是这样,所以你必须使用 render() 方法 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()