The Portable Document Format, or PDF, is everywhere. But it’s still a format that causes headaches for the average person. Sure, you can send a text, Word file, HTML, PowerPoint or any other file. But other formats, while just as easy to attach to an email, aren’t quite as easy to share as PDF. They might not look quite the same when opened on different machines or can’t be opened on a Mac.
There may be font dependencies, or differing page sizes or other application or user settings that affect appearance. There may be undesirable information such as slide-show notes, metadata or “Track Changes” information that you might not want to share!
That’s why it is very necessary to convert text to PDF and we will show you how to do it using Python.
“What industries badly need is a universal way to communicate documents across a wide variety of machine configurations, operating systems and communication networks. These documents should be viewable on any display and should be printable on any modern printers. If this problem can be solved, then the fundamental way people work will change.”
— John E. Warnock, the co-founder of Adobe
So, if you don’t know how to convert a text file to PDF using Python 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.
Why Choose PDF Over Text File
There are tons of reasons to choose a PDF over a Python text file. We are Mentioning a few of them:
- Easy
- Portability
- Flexibility
- Security
- Authentication
- Semantics
- Non-Proprietary
Converting Text to PDF Using Python
As we all know, python is an ocean of libraries. There are many libraries that can convert a text or a text file to PDF. But in this specific article, we are using the simplest and most popular library to achieve our goal: convert a text file to PDF.
So, enough of talks we are revealing the name of the library we are using and that is FPDF. The PyFPDF package is a port of the “Free”-PDF package that was written in PHP.
Main Features of FPDF
The main features of PYFDF are:
- Easy to use (and easy to extend)
- Many simple examples and scripts available in many languages
- No external dependencies or extensions (optionally PIL for GIF support)
- No installation, no compilation or other libraries (DLLs) required
- Small and compact code, useful for testing new features and teaching
Installation
Installing PyFPDF is easy since it was designed to work with pip. Here’s how:
pip install fpdf
Support
The FPDF supports Python 2.5+ to 3.4+ all versions.
Now, let’s directly move to various programs to convert Python text to PDF.
Python Program to Convert Text to PDF
Prerequisites
- To run the below Python script, you must already have the latest version of Python 3.x installed on your device.
- This example uses the FPDF library to generate the PDF. Therefore, install it before you start.
Steps
Step 1: import the library package’s class FPDF :
#Importing the FPDF library.
from fpdf import FPDF
Let’s move to the next step.
Step 2: Saving FPDF class into a variable named PDF and adding a page:
pdf = FPDF()
pdf.add_page()
Step 3: Setting the style and size of font that you want in the PDF:
pdf.set_font("Arial", size = 15)
You can change the font and font size according to your need.
Step 4: Creating two cell
pdf.cell(200, 10, txt = "Python Pool",
ln = 1, align = 'C')
# add another cell
pdf.cell(200, 10, txt = "Start your Python Learning journey now with Python Pool",
ln = 2, align = 'C')
Step 5: Saving the PDF
pdf.output("PP.pdf")
Note:
If you go diving into the source, you will find that the PyFPDF package only supports the following page sizes:
- A3
- A4
- A5
- letter
- legal
The PyFPDF has a set of core fonts hard-coded into its FPDF class:
self.core_fonts={'courier': 'Courier', 'courierB': 'Courier-Bold', 'courierBI': 'Courier-BoldOblique', 'courierI': 'Courier-Oblique', 'helvetica': 'Helvetica', 'helveticaB': 'Helvetica-Bold', 'helveticaBI': 'Helvetica-BoldOblique', 'helveticaI': 'Helvetica-Oblique', 'symbol': 'Symbol', 'times': 'Times-Roman', 'timesB': 'Times-Bold', 'timesBI': 'Times-BoldItalic', 'timesI': 'Times-Italic', 'zapfdingbats': 'ZapfDingbats'}
Writing the whole program in a single window:
#Importing the FPDF library.
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size = 15)
pdf.cell(200, 10, txt = "Python Pool",
ln = 1, align = 'C')
# add another cell
pdf.cell(200, 10, txt = "Start your Python Learning journey now with Python Pool",
ln = 2, align = 'C')
pdf.output("PP.pdf")
Output:
Converting a Whole Text File to PDF Using Python and FPDF
In the above example, we have learned how to convert a text or a phrase of text to PDF. But in this advance python script, we will learn how to convert the whole.txt file.
Firstly we need a plain text file. Which you can create in notepad itself.
And save it with .txt extension. Like the example shown below.
Program:
# Python program to convert text file to PDF using FPDF
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size = 15)
f = open("input.txt", "r")
for x in f:
pdf.cell(200, 10, txt = x, ln = 1, align = 'C')
pdf.output("purepython.pdf")
Output:
Other Libraries to Convert Text to PDF
There are many libraries available in Python’s Galaxy to convert text to PDF some of them are:
- PDFMiner
- pdftotext
- pdfkit
- pypdf2
And many more…
Must Read:
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python
Conclusion
The creation of PDF file is frequently done whether you are a student or a working professional. In this article, we learned how to convert Python text to PDF with the help of FPDF package. FPDF is a fairly nice project that lets you do basic PDF generation. Now, if you have followed all the steps properly, you don’t need to buy a tool to convert PDF. You can directly use this script and get the job done.
Try to run the programs on your side and let us know if you have any queries.
Happy Coding!