Text to PDF Converter Using Python
A Python program that converts plain text files into PDF format using the FPDF library. Supports Unicode fonts, UTF-8 text reading, and automatic line wrapping for clean and professional PDF output.
Explanation
A Text-to-PDF Converter is a useful utility program that transforms plain text files into neatly formatted PDF documents. PDF files are widely used for printing, sharing, and long-term storage because they preserve formatting on any device. This converter uses Python’s FPDF library to read a text file, apply a proper font, and generate a professional-looking PDF output.
The program reads a .txt file using UTF-8 encoding, supports Unicode text,
and writes the content into the PDF with proper line wrapping and alignment.
Requirements
-
pip install fpdf2
Imports the FPDF library used to create PDF files we need to first import use this line to install fpdf.
- python version greater than or equal to 3
- Reads text file using UTF-8 encoding.
- Line wrapping handled automatically using
multi_cell(). - User chooses input and output filenames.
Code Explanation
Imports the FPDF library used to create PDF files.
Prints a formatted title in the console for user friendliness.
pdf.add_page()
Creates a new PDF object and adds a blank page to start writing content.
pdf.set_font("arial", "", 14)
Loads the Arial Unicode font (supports all languages) and sets it as active with size 14.
To download the arial-unicode-ms.ttf click here.
You need to place the downloaded arial-unicode-ms.ttf file in same folder of your txtToPdf.py script file.
Asks the user for the input filename, such as notes.txt.
Opens the selected text file safely using UTF-8 encoding.
Reads all text content from the file into a variable.
Writes the content into the PDF.
multi_cell() automatically handles:
➤ Line wrapping
➤ New lines
➤ Paragraph spacing
Asks the user what the final PDF file should be named.
Generates the final PDF file and saves it using the name provided by the user.
Key Points
- Uses Python’s FPDF library.
- Supports Unicode using a proper TTF font.
- Reads text file using UTF-8 encoding.
- Line wrapping handled automatically using
multi_cell(). - User chooses input and output filenames.
Full Python Program
from fpdf import FPDF
print(" TXT To PDF ".center(40,"+"))
pdf = FPDF()
pdf.add_page()
pdf.add_font("arial", "", "arial-unicode-ms.ttf")
pdf.set_font("arial", "", 14)
file_path = input("Enter the Input file name with extension (e.g filename.txt) :")
with open(file_path, "r", encoding="utf-8", errors="replace") as ou:
content = ou.read()
pdf.multi_cell(0, 10, text=content, border=0, align='L')
output_name = input("Enter the PDF file name with extension (e.g filename.pdf) :")
pdf.output(output_name)
print("The pdf file has been generated successfully")
Output :
>py txtToPdf.py ++++++++++++++ TXT To PDF ++++++++++++++ Enter the Input file name with extension (e.g filename.txt) :textFileName.txt Enter the PDF file name with extension (e.g filename.pdf) :output.pdf The pdf file has been generated successfully
