This short tutorial guides on how to create PDF in Python. It contains complete information to set the IDE, a list of steps to be followed while writing the application, and a runnable sample code to demonstrate the process to generate PDF with Python. You will learn about all the classes, methods, and properties that are required to create a new PDF and add formatted text for understanding the process thoroughly.
Steps to Create PDF in Python
- Configure the environment to use Aspose.PDF for Python via .NET to create a PDF
- Create a PDF file using the Document class and add a page to it
- Create a TextFragment class object to add text on the PDF page
- Set different properties of the text like font size, font name, background color, and foreground color
- Create a TextBuilder class object for the selected page and add a text fragment to it
- Save the resultant PDF file having newly added text in it
These steps summarize the process to generate PDF using Python. First, a new PDF file is created and a page is added to it followed by creating a TextFragment object that is used for adding the text, settings text formatting, foreground color, and background color. Once the TextFragment object is filled, the TextBuilder class object for the selected page is instantiated and the newly created TextFragment object is added to the PDF.
Code to Create PDF using Python
The above code segment demonstrates how to create PDF file using Python. The Document class in the aspose.pdf module is used for creating a new PDF file, the TextFragment object in the aspose.pdf.text module is used for setting the text and its format, the TextBuilder class object in aspose.pdf.text is used for adding the text fragment to the selected page, and the Document.save() method is finally used to save the file on the disk. You may use TextFragment object to set a number of other properties for the text and TextBuilder object to add other document elements to the PDF.
This article has taught us to create PDF file with Python. If you want to learn the process to convert a PDF file to XPS, refer to the article on how to convert PDF to XPS in Python.