Course Content
Python PySide6
About Lesson

In this Python PySide6 lesson we are going to learn about Python PySide6 PDF Exporter App, for doing this functionality we are going to use QPrintDialog.

 

To use QPrintDialog for exporting a file as PDF you can follow these steps:

 

Import the necessary modules:

 

 

Create QMainWindow object:

 

 

Create QTextEdit widget to hold the text you want to export as PDF:

 

 

Create QAction object for exporting the file as PDF and add it to the menu bar:

 

 

Create a method to handle the exporting of the file as PDF:

In the export_as_pdf method the QPrintDialog is shown to allow the user to select printer and other print options. If the user cancels the dialog the method returns without exporting the file.

If the user selects printer, the getSaveFileName method of QFileDialog is called to allow the user to choose the filename and location to save the PDF file. If the user cancels this dialog, the method returns without exporting the file.

A QPdfWriter object is created with the filename chosen by the user.

Finally the print_ method of the QTextEdit widget’s document object is called to print the document to the QPdfWriter object.

 

 

This is the complete code

 

 

 

Run the code and this will be the result.

Python PySide6 PDF Exporter App
Python PySide6 PDF Exporter App