Python Export File as PDF with Pyside2

In this Python article, i want to show you Export File as PDF with Pyside2, for this purpose we are using QPrinter class. the QPrinter class is a paint device that paints on a printer. this device represents a series of pages of printed output, and is used in almost exactly the same way as other paint devices such as QWidget and QPixmap . A set of additional functions are provided to manage device-specific features, such as orientation and resolution, and to step through the pages in a document as it is generated. when printing directly to a printer on Windows or macOS, QPrinter uses the built-in printer drivers. on X11, QPrinter uses the Common Unix Printing System (CUPS) to send PDF output to the printer. As an alternative, the printProgram() function can be used to specify the command or utility to use instead of the system default.

 

 

Note: you can watch the complete video for this article at the end.

 

 

 

Also you can check my previous articles on Pyside2 GUI Development.

1: Getting Started With Pyside2 | Qt For Python 

2: Pyside2 GUI Creating First Window 

3: Pyside2 GUI Creating Window Icon

4: Pyside2 GUI How To Create Icon Modes

5: Pyside2 GUI How To Create Tooltip

6: Pyside2 GUI QPushButton With Signal And Slot

7: Pyside2 GUI Making Center The Window 

8: Python GUI How To Create AboutBox

9: Python GUI How to Create Digital Clock in Pyside2

10: How To Create StatusBar In Pyside2 

11: Pyside2 Creating QProgressBar

12: Pyside2 Layout Managment with QHBoxLayout

13: Pyside2 GridLayout Example

14: Pyside2 Creating Calendar 

15: Pyside2 Create CheckBox

16: Pyside2 Creating FontComboBox

17: Pyside2 Creating QCompleter 

18: Pyside2 GUI Creating Slider 

19: Pyside2 Create MenuBar & MenuItems

20: Pyside2 Creating TextEdit 

21: Pyside2 Creating Print Preview Dialog

22: How to Create Print Dialog in Pyside2

 

 

 

 

So now this is the complete source code for this article

 

 

 

 

In the above code we are using some codes from the previous article.

1: Pyside2 Create MenuBar & MenuItems

2: Pyside2 Creating TextEdit 

3: Pyside2 Creating Print Preview Dialog

4: How to Create Print Dialog in Pyside2

 

 

 

you can check the above articles to know more about this code, but we are going to just focus on exporting a file as pdf in pyside2.

 

 

 

These are the imports that we need in this article.

 

 

 

OK now in here we are going to set our window title, also we need to set the geometry of the window, like x and y position of the window, also width and height of the window .

 

 

 

Also we need to create our QTextEdit in our main class, first we have created the object of QTextEdit, after that we have set the font for our QTextEdit. also you need to set the textedit as central widget for the window.

 

 

 

 

In here we have created menuBar object and we have added some items to the menuBar.

 

 

 

 

OK now in here we have added some menu items with the icons and also shortcuts. make sure that you have some icons in your working directory. we are using QAction for this purpose, so in applications many common commands can be invoked via menus, toolbar buttons, and keyboard shortcuts. since  the user expects each command to be performed in the same way, regardless of the user interface used, it is useful to represent each command as an action. Actions can be added to menus and toolbars, and will automatically keep them in sync. for example, in a word processor, if the user presses a Bold toolbar button, the Bold menu item will automatically be checked. also we have added our new menu item action that is used for exporting as pdf.

 

 

 

 

These are the four signals for our two menu items, and the newest one is for our exporting file as pdf that we have connected that signal to a method called pdf_export(),  we will create this method later.

 

 

 

 

Now after creation of menu actions, we need to add that to the related menubar like this. basically in this example we want to add the menu items in our file menu and also the newest menu item for our export as pdf  in view menu .

 

 

 

 

These are the methods that we have already covered, you can check the articles about these in the above link.

 

 

 

OK now this is the important point of this article, basically this is the creation of pdf export method.

 

As you can see in the code first we have created QFileDialog, after that we are checking our filename. and after that we have created QPrinter object and also we have set the output format to PDF.

 

 

 

Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. also we have created the object of our window class in here.

 

 

 

 

If you run the complete code this will be the result

Python Export File as PDF with Pyside2
Python Export File as PDF with Pyside2

 

 

 

 

 

Also you can watch the complete video for this article

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×