[Python] PyQt6 save to pdf

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to print a PyQt6 Window into a PDF File. It works, but the window appears to be very small in the top left corner of the PDF page. Does anyone know how to solve it?

    I would appreciate some help. :)

    (Save to PDF code block is below).

    def save_to_pdf(self):
    """Save the display window as a PDF."""
    # Create a QPrinter object
    printer = QPrinter(QPrinter.PrinterMode.HighResolution)
    printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat)

    # Ask the user for a file name and location to save the PDF
    file_dialog = QFileDialog(self)
    file_dialog.setAcceptMode(QFileDialog.AcceptMode.AcceptSave)
    file_dialog.setNameFilter("PDF Files (*.pdf)")
    file_dialog.setDefaultSuffix("pdf")
    file_dialog.setWindowTitle("Save as PDF")
    if file_dialog.exec():
    file_name = file_dialog.selectedFiles()[0]
    printer.setOutputFileName(file_name)

    # Create a painter to render the content
    from PyQt6.QtGui import QPainter
    painter = QPainter(printer)


    # Render the window onto the painter
    self.render(painter)
    painter.end()

    Continue reading...

Compartilhe esta Página