Convert Text and Text File to PDF using Python - GeeksforGeeks (2024)

Improve

Improve

Improve

Like Article

Like

Save Article

Save

Report issue

Report

PDFs are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system. Converting a given text or a text file to PDF (Portable Document Format) is one of the basic requirements in various projects that we do in real life. So, if you don’t know how to convert a given text to PDF then this article is for you. In this article, you will come to know the way to convert text and text file to PDF in Python. FPDF is a Python class that allows generating PDF files with Python code. It is free to use and it does not require any API keys. FPDF stands for Free PDF. It means that any kind of modification can be done in PDF files. The main features of this class are:

  • Easy to use
  • It allows page format and margin
  • It allows to manage page header and footer
  • Python 2.5 to 3.7 support
  • Unicode (UTF-8) TrueType font subset embedding
  • Barcode I2of5 and code39, QR code coming soon …
  • PNG, GIF and JPG support (including transparency and alpha channel)
  • Templates with a visual designer & basic html2pdf
  • Exceptions support, other minor fixes, improvements and PEP8 code cleanups

To install the fpdf module type the below command in the terminal.

pip install fpdf

Approach:

  1. Import the class FPDF from module fpdf
  2. Add a page
  3. Set the font
  4. Insert a cell and provide the text
  5. Save the pdf with “.pdf” extension

Example:

Python3

# Python program to create

# a pdf file

from fpdf import FPDF

# save FPDF() class into a

# variable pdf

pdf = FPDF()

# Add a page

pdf.add_page()

# set style and size of font

# that you want in the pdf

pdf.set_font("Arial", size = 15)

# create a cell

pdf.cell(200, 10, txt = "GeeksforGeeks",

ln = 1, align = 'C')

# add another cell

pdf.cell(200, 10, txt = "A Computer Science portal for geeks.",

ln = 2, align = 'C')

# save the pdf with name .pdf

pdf.output("GFG.pdf")

Output: Convert Text and Text File to PDF using Python - GeeksforGeeks (1)

Converting text file to PDF

Now if we want to make the above program more advance what we can do is that from a given text file extract the data using file handling and then insert it into the pdf file. The approach is all same as above, one thing you have to do is extract the data from a text file using file handling. Note: Refer this article to know more about file handling in Python. Example: Let’s suppose the text file looks like this – Convert Text and Text File to PDF using Python - GeeksforGeeks (2)

Python3

# Python program to convert

# text file to pdf file

from fpdf import FPDF

# save FPDF() class into

# a variable pdf

pdf = FPDF()

# Add a page

pdf.add_page()

# set style and size of font

# that you want in the pdf

pdf.set_font("Arial", size = 15)

# open the text file in read mode

f = open("myfile.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("mygfg.pdf")

Output: Convert Text and Text File to PDF using Python - GeeksforGeeks (3)



Last Updated : 25 May, 2022

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

Convert Text and Text File to PDF using Python - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 6157

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.