create pdf in python

How to Create PDF File in Python

Sometimes you may need to add ‘export to pdf’ functionality to your python application or website. In this article, we will look at how to export data to PDF using python. You can use these steps to export HTML to PDF or even plain text to PDF.


How to Create PDF File in Python

We will use fpdf library to create PDF. It stands for free pdf and is commonly used in python applications to create pdf files.

You can install it with the following command.

$ sudo pip install fpdf


1. Create PDF file from text

In this case, we will import fpdf library, add a page, set its font, and insert a cell with our text in it. Finally, we save it as pdf file.

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF()
  
#add page
pdf.add_page()
  
# set font style and size
pdf.set_font("Arial", size = 15)
  
# create a cell, specify its alignment and position
pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C')
  
# add another cell
pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C')
  
# save the pdf with name .pdf
pdf.output("fedingo.pdf")   


2. Save Text File as PDF

Sometimes you may need to convert a text file into PDF. You can also do this with fpdf. In this case, we will loop through our text file, line by line and add them one by one to our pdf file, before saving it. In the above example, we have added two lines to our PDF file. This is just an extension of it. Let us say you want to save text file data.txt to data.pdf. Here is an example to create pdf file out of it.

#import fpdf
from fpdf import FPDF
   
#create object instance
pdf = FPDF()
  
#add page
pdf.add_page()
  
# set font style and size
pdf.set_font("Arial", size = 15)
  
# open the text file in read mode
f = open("data.txt", "r")
 
# insert the texts in pdf
for x in f:
       pdf.cell(200, 10, txt = x, ln = 1, align = 'C')
 
  
# save the pdf with name .pdf
pdf.output("data.pdf")   


3. Convert Webpage to PDF

If you want to convert your web page/HTML to PDF then we recommend using pdfkit instead. We have provided step by step tutorial to convert webpage to PDF using pdfkit. Here is how to convert webpage to PDF.


4. Convert DocX to PDF

If you want to convert DocX file to PDF then it is advisable to use docx2pdf which is specifically built for this purpose.

We have provided detailed tutorial about converting DocX files to PDF document. Here is how to convert DocX to PDF files.

As you can see, python is a very versatile language that allows you to convert various kinds of data inputs to PDF documents. Exporting to PDF is a useful and sought after function in most websites and applications. You can use any of the above methods to generate PDF from your data and share it with your users.

Also read:

How to Redirect IP to Domain URL in NGINX
How to Fix Mixed Content/Insecure Content Issue in Apache/PHP
How to Force HTTPS in .htaccess in Apache
How to Set Content-Disposition in Header in Attachment
How to Redirect IP to Domain URL in Apache

Leave a Reply

Your email address will not be published.