Create PDF document programmatically

For developers, there are many scenarios where it becomes necessary to programmatically generate PDF files. You may need to programmatically generate PDF reports and other PDF files in your software. It is rather long and inefficient to write your own code and functions from scratch. To create a PDF file with Python, there is a better solution - Aspose.PDF for Python via .NET.

How to Create PDF File using Python

To create a PDF file usingPython, the following steps can be used.

  1. Create an object of Document class
  2. Add a Page object to the Pages collection of the Document object
  3. Add TextFragment to paragraphs collection of the page
  4. Save the resultant PDF document

    import aspose.pdf as ap

    # Initialize document object
    document = ap.Document()
    # Add page
    page = document.pages.add()
    # Initialize textfragment object
    text_fragment = ap.text.TextFragment("Hello,world!")
    # Add text fragment to new page
    page.paragraphs.add(text_fragment)
    # Save updated PDF
    document.save("output.pdf")