Create a PDF document using Python

PDF file format is commonly used to present and exchange documents. In addition, it provides many benefits, such as reducing MBs and rendering text, images, tables, annotations, bookmarks, and hyperlinks without losing layout and formatting. Python developers may consider these advantages and look to publish their data as PDFs. If you are one of those developers, this article will help you to learn how to create a PDF document using Python.

The following points will be covered in this article:

Installing Python PDF Creator Package

To create a PDF document programmatically, we will use a Python PDF creator package, which allows developers to generate, read, convert, and manipulate PDF files without using Adobe Acrobat.

Please download or install the package from PyPI using the pip command given below:

pip install aspose-pdf

How to Easily Create a PDF Document in Python

Firstly, we are going to explain how to create a PDF document containing a text fragment. The following are the steps to create a simple PDF document from scratch.

  1. Create an instance of Document class.
  2. Add a new Page to the pages collection of the document.
  3. Create and add a new TextFragment to the paragraphs of the PDF.
  4. Generate the PDF file using Document.Save() method.

The following code snippet demonstrates how to create a PDF document in Python.

# Initialize document object
document = aspose.pdf.Document()
# Add page
page = document.pages.add()
# Initialize textfragment object
text= "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." \
" Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris" \
" nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in " \
"reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." \
" Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " \
"officia deserunt mollit anim id est laborum."
text_fragment = aspose.pdf.text.TextFragment (text)
# Add text fragment to new page
page.paragraphs.add(text_fragment)
# Save updated PDF
document.save("output.pdf")

You will see the output as follows.

Create a PDF Document in Python

Apply Text Formatting in PDFs using Python

After creating a document let’s learn how to apply text formatting in PDFs using a Python PDF library. Please follow the steps given below.

  • First, create a new PDF document using Document class.
  • Second, pick the page where you want to place the text.
  • Then, create the objects of TextFragment and set their text and other formatting options such as position, font, color, size, etc.
  • After that, add the text fragments to the page using Page.paragraphs.add() method.
  • Finally, call the Document.Save() method to create the PDF document.

The following code snippet shows how to apply text formatting in PDFs programmatically using Python.

# Initialize document object
document = aspose.pdf.Document()
# Add page
page = document.pages.add()
# Create text fragments
text1= "Text1: Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." \
" Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris" \
" nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor"
text2= "Text2: Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
text_fragment1 = aspose.pdf.text.TextFragment (text1)
text_fragment1.position= aspose.pdf.text.Position(100, 700)
text_fragment2 = aspose.pdf.text.TextFragment (text2)
text_fragment2.position= aspose.pdf.text.Position(100, 600)
# Set text properties
text_fragment1.text_state.font_size = 12;
text_fragment1.text_state.font = aspose.pdf.text.FontRepository.find_font("TimesNewRoman");
text_fragment1.text_state.background_color=aspose.pdf.Color().aqua
text_fragment2.text_state.underline = True
text_fragment2.text_state.strike_out = True
# Add text fragments to page
page.paragraphs.add(text_fragment1)
page.paragraphs.add(text_fragment2)
# Save updated PDF
document.save("output.pdf")

You will see the output PDF as follows.

Apply text formatting in PDFs using Python

Python: Insert Images in a PDF File

We have learned to change text formatting in the previous section. Therefore, in this section, we will explain how to add images to PDF documents. Follow the steps below to do the job.

  1. Firstly, create a new PDF document using Document class.
  2. Secondly, obtain the desired page where you want to insert the image.
  3. Thirdly, add the image to the page using Page.add_image(file_path, rectangle), whereas Rectangle class is used to place the image on the page.
  4. Finally, generate the PDF document using Document.Save() method.

The following code sample shows how to add images in PDF using Python.

# Initialize document object
document = aspose.pdf.Document()
# Add page
page = document.pages.add()
# Set image coordinates
lowerLeftX = 400;
lowerLeftY = 400;
upperRightX = 150;
upperRightY = 150;
rectangle= aspose.pdf.Rectangle(lowerLeftX,lowerLeftY,upperRightX,upperRightY,True)
# Add image to page
page.add_image("aspose-logo.jpg", rectangle)
# Save updated PDF
document.save("output.pdf")

You will see the output PDF as follows.

Insert images in a PDF file using Python

Add Tables in a PDF using Python

After inserting an image, let’s add a table to your PDF file. Please write code using the steps given below:

  1. Create an object of Document class to create a new PDF.
  2. Get the page on which you want to create the table.
  3. Create an instance of Table class.
  4. Specify the borders of the table and cells using the BorderInfo class.
  5. Create and add a new Row to the Table.Rows collection.
  6. Add cells to the Row.Cells collection.
  7. Add the table to the page using Page.paragraphs.add() method.
  8. Save the PDF document using Document.Save() method.

The following code sample shows how to add a table in a PDF file using Python.

# Initialize document object
document = aspose.pdf.Document()
# Add page
page = document.pages.add()
# Create table
table = aspose.pdf.Table()
# Define table and cell borders
table.border=aspose.pdf.BorderInfo(aspose.pdf.BorderSide.ALL,2,aspose.pdf.Color().dark_gray)
table.default_cell_border= aspose.pdf.BorderInfo(aspose.pdf.BorderSide.ALL,2,aspose.pdf.Color().black)
# Add rows in the table
for i in range(1, 11):
row = table.rows.add()
# Add table cells
row.cells.add("Column (" + str(i) + ", 1)");
row.cells.add("Column (" + str(i) + ", 2)");
row.cells.add("Column (" + str(i) + ", 3)");
# Add table to the page
page.paragraphs.add(table);
# Save updated PDF
document.save("D:\\AsposeSampleResults\\output.pdf")

You will see the output PDF as follows.

Add Tables in a PDF using Python

Get a Free License

You can get a free temporary license to try the API without evaluation limitations.

Summing Up

In this article, we have explained the procedure of creating a PDF file in Python. Likewise, we have described how to add images, tables, and text formatting in a PDF document.

You may visit the documentation to learn more about the Python PDF creator library.

Please stay tuned at conholdate.com for regular updates.

Ask a Question

You can let us know your questions or queries on our forum.

See Also