Writing to save printer and pdf using qt designer. Error occurs when outputting.I want to save the entire main window window as a printer and pdf.

Asked 1 years ago, Updated 1 years ago, 372 views

from PyQt5.QtGui import *
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem, QVBoxLayout, QPushButton, QDialog
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPdfWriter, QPagedPaintDevice, QPainter, QScreen, QPixmap
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
import sys
import random


QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
form_class = uic.loadUiType("qt semple7.ui")[0]

class WindowClass(QMainWindow, form_class) :

    def __init__(self) :
        super().__init__()
        self.setupUi(self)

        self.pushButton_3.clicked.connect(self.btnClick)
        self.pushButton_5.clicked.connect(self.btnClick2)


    def btnClick(self):
        # printer creation, running
        printer = QPrinter()
        dlg = QPrintDialog(printer, self)
        if dlg.exec() == QDialog.Accepted:
            # Creating a Patter
            qp = QPainter()
            qp.begin(printer)        

            # margin ratio
            wgap = printer.pageRect().width()*0.1
            hgap = printer.pageRect().height()*0.1

            # Place the widget in the center of the screen
            xscale = (printer.pageRect().width()-wgap)/self.table.width()
            yscale = (printer.pageRect().height()-hgap)/self.table.height()
            scale = xscale if xscale < yscale else yscale        
            qp.translate(printer.paperRect().x() + printer.pageRect().width()/2, printer.paperRect().y() + printer.pageRect().height()/2)
            qp.scale(scale, scale);
            qp.translate(-self.table.width()/2, -self.table.height()/2);        

            # Printing
            self.table.render(qp)

            qp.end()  

    def btnClick2(self):
        # Create pdf
        pdf = QPdfWriter('test.pdf')
        pdf.setPageSize(QPagedPaintDevice.A4)

        # Screen capture.        
        screen = QApplication.primaryScreen()
        img = screen.grabWindow(self.winId(), 0,0, self.rect().width(),self.rect().height())

        # Trinomial operator (a if test is true, a, or b is true, or b)
        # Image size is based on large values and PDF size is based on small values (prevent screen overrun)
        img_size = img.width() if img.width()-img.height() > 0 else img.height()
        pdf_size = pdf.width() if pdf.width()-pdf.height() < 0 else pdf.height()

        # Obtaining the Optimal Ratio
        ratio = pdf_size / img_size

        # writing to pdf
        qp = QPainter()
        qp.begin(pdf)
        point = QPoint(0, 0)
        qp.drawPixmap(point, img.width()*ratio, img.height()*ratio, img)
        qp.end()


if __name__ == "__main__" :
    app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()

python qt pdf

2023-02-18 04:33

1 Answers

Hello! Looking at the code, self.Cannot find variable table. The widget that you want to print using the printer is self.Would you like to change it to table? If you find and print this part, there will be no error.

Also, pdf before QPainter prints the PDF file in the PDF output part.The begin() method must be called. Try changing to qp.begin(pdf). And qp after completing the PDF file creation.Call end() to finish.

Below is the revised code.

from PyQt5.QtGui import *
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem, QVBoxLayout, QPushButton, QDialog
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPdfWriter, QPagedPaintDevice, QPainter, QScreen, QPixmap
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
import sys
import random


QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
form_class = uic.loadUiType("qt semple7.ui")[0]

class WindowClass(QMainWindow, form_class) :

    def __init__(self) :
        super().__init__()
        self.setupUi(self)

        self.pushButton_3.clicked.connect(self.btnClick)
        self.pushButton_5.clicked.connect(self.btnClick2)


    def btnClick(self):
        # printer creation, running
        printer = QPrinter()
        dlg = QPrintDialog(printer, self)
        if dlg.exec() == QDialog.Accepted:
            # Creating a Patter
            qp = QPainter()
            qp.begin(printer)

            # margin ratio
            wgap = printer.pageRect().width()*0.1
            hgap = printer.pageRect().height()*0.1

            # Place the widget in the center of the screen
            xscale = (printer.pageRect().width()-wgap)/self.centralWidget().width()
            yscale = (printer.pageRect().height()-hgap)/self.centralWidget().height()
            scale = xscale if xscale < yscale else yscale
            qp.translate(printer.paperRect().x() + printer.pageRect().width()/2, printer.paperRect().y() + printer.pageRect().height()/2)
            qp.scale(scale, scale);
            qp.translate(-self.centralWidget().width()/2, -self.centralWidget().height()/2);

            # Printing
            self.centralWidget().render(qp)

            qp.end()

    def btnClick2(self):
        # Create pdf
        pdf = QPdfWriter('test.pdf')
        pdf.setPageSize(QPagedPaintDevice.A4)

        # Creating a Patter
        qp = QPainter()
        qp.begin(pdf)

        # Screen capture.
        screen = QApplication.primaryScreen()
        img = screen.grabWindow(self.winId(), 0, 0, self.rect().width(), self.rect().height())

        # Trinomial operator (a if test is true, a, or b is true, or b)
        # Image size is based on large value, PDF size is small value

`


2023-02-20 06:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.