无法以高分辨率将 QWidget 打印为 PDF

问题描述 投票:0回答:1

我正在尝试使用 QPrinter 将 QWidget 打印为 A4 页面大小的 pdf。但无法以更好的质量生成它。 PDF 看起来很模糊。请在下面找到我尝试过的代码。任何有关 pdf 质量的帮助都非常值得赞赏。

import os
import datetime
from PySide6.QtCore import QSize
from PySide6.QtGui import QPageSize
from PySide6.QtWidgets import QApplication, QWidget, QLineEdit, QLabel, QTableWidget, QVBoxLayout, QTableWidgetItem, QHeaderView
from PySide6.QtPrintSupport import QPrinter


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.setStyleSheet("")
        self.setFixedSize(QSize(794, 1123))
        self.line = QLineEdit()
        self.l1 = QLabel("This is testing")
        self.table_widget = QTableWidget(4,5, self)
        self.table_widget.setHorizontalHeaderLabels(["Item", "Batch"])
        self.table_widget.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
        self.table_widget.setItem(0,0, QTableWidgetItem("Item Name"))
        self.table_widget.setItem(0,1, QTableWidgetItem("Batch no"))
        self.table_widget.setItem(0,2, QTableWidgetItem("Item Name"))
        self.table_widget.setItem(0,3, QTableWidgetItem("Item Name"))
        self.table_widget.setItem(1,0, QTableWidgetItem("Item Name"))
        self.table_widget.setItem(1,1, QTableWidgetItem("Item Name"))
        self.table_widget.setItem(1,2, QTableWidgetItem("Item Name"))
        self.table_widget.setItem(1,3, QTableWidgetItem("Item Name"))
        self.table_widget.setItem(2,0, QTableWidgetItem("Item Name"))
        self.table_widget.setItem(2,1, QTableWidgetItem("Item Name"))
        layout = QVBoxLayout()
        layout.addWidget(self.table_widget)
        self.setLayout(layout)
        

    def exportToPDF(self, filename):
        printer = QPrinter()
        printer.setPageSize(QPageSize.A4)
        printer.PrinterMode.HighResolution
        printer.setOutputFormat(QPrinter.PdfFormat)
        printer.setOutputFileName(filename)

        self.render(printer)


if __name__ == "__main__":
    app = QApplication([])

    example = Example()
    example.show()
    example.exportToPDF("invoice_" + str(datetime.datetime.now().strftime('%Y%m%d%H%M')) + ".pdf")
    os.system("start " + "invoice_" + str(datetime.datetime.now().strftime('%Y%m%d%H%M')) + ".pdf")
    
    app.exec()

请找到此处附带的 pdf - https://drive.google.com/file/d/1p1HbfRQixZ-lfERtfzlF_ZPFX3pkT_tv/view?usp=sharing

python pdf-generation qwidget pyside6 qprinter
1个回答
0
投票

我首先要感谢@musicamante、@ekhumoro 和@KJ 的帮助。我终于找到了解决办法。我在这里分享更新后的代码。

import os
import datetime
from PySide6.QtCore import QSize
from PySide6.QtGui import QPageSize, QFont
from PySide6.QtWidgets import QApplication, QWidget, QLineEdit, QLabel, QTableWidget, QVBoxLayout, QTableWidgetItem, QHeaderView
from PySide6.QtPrintSupport import QPrinter


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.setFixedSize(QSize(2480, 3508))
        self.table_widget = QTableWidget(35,5, self)
        self.table_widget.setHorizontalHeaderLabels(["Item", "Batch", "MRP", "Quantity", "Amount"])
        self.table_widget.horizontalHeader().setFont(QFont("Arial", 30))
        self.table_widget.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
        self.table_widget.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.ResizeToContents)
        self.table_widget.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeMode.ResizeToContents)
        self.table_widget.horizontalHeader().setSectionResizeMode(3, QHeaderView.ResizeMode.ResizeToContents)
        self.table_widget.horizontalHeader().setSectionResizeMode(4, QHeaderView.ResizeMode.ResizeToContents)
        self.table_widget.verticalHeader().setDefaultSectionSize(100)
        self.table_widget.setFont(QFont("Arial", 30))
        for i in range(0,35):
            for j in range(0,5):
                self.table_widget.setItem(i,j, QTableWidgetItem("Item Name_"+str(i)+"_"+str(j)))

        layout = QVBoxLayout()
        layout.addWidget(self.table_widget)
        self.setLayout(layout)
        

    def exportToPDF(self, filename):
        printer = QPrinter()
        pageSize = QPageSize(QPageSize.A4)
        printer.setPageSize(pageSize)
        printer.setResolution(300)
        printer.setOutputFormat(QPrinter.PdfFormat)
        printer.setOutputFileName(filename)

        self.render(printer)


if __name__ == "__main__":
    app = QApplication([])

    example = Example()
    example.show()
    example.exportToPDF("invoice_" + str(datetime.datetime.now().strftime('%Y%m%d%H%M')) + ".pdf")
    os.system("start " + "invoice_" + str(datetime.datetime.now().strftime('%Y%m%d%H%M')) + ".pdf")
    
    app.exec()
© www.soinside.com 2019 - 2024. All rights reserved.