PDF (Portable Document Format) files are widely used in the world to share digital documents. These files can get large, but they are easy to handle and work well on most computer systems.
PDF files maintain their formatting even when transferred to other systems, which is beneficial for document sharing purposes. In this article, we will explore how to store data into PDF format and convert text files to PDF files in Python programming language.
Storing data into PDF format
Python programming language includes several modules for creating and manipulating PDF files. One such module is the FPDF module.
The FPDF module allows us to create PDF pages and store data into them. The following steps show how to use the FPDF module to store data in a PDF document.
Step 1: Importing FPDF module and creating a PDF page
First, we need to install the FPDF module in Python. After installing the module, we have to import the module by using the following command:
“` from fpdf import FPDF “`
Next, we have to create a PDF page using the “`add_page()“` function.
The following code shows the use of the “`add_page()“` function:
“` python
# Importing FPDF module
from fpdf import FPDF
# Certificate class
class Certificate(FPDF):
# Function to create page
def create_certificate(self):
# Adding a page
self.add_page()
“`
In this example, we have created a Certificate class that inherits FPDF. The “`create_certificate()“` function creates the certificate page that we can use to store data.
Step 2: Setting font attributes and inserting data
Now that we have a page, we have to set the font attributes for the text we will insert. By default, the FPDF module uses the Helvatica font.
We can set the font using the “`set_font()“` function. The following code shows how to use the “`set_font()“` function:
“` python
# Defining font attributes
font_family = ‘Arial’
font_size = 16
font_style = ‘B’
# Setting font attributes for text
self.set_font(font_family, font_style, font_size)
“`
Here, we have defined the font family, size, and style attributes for our text.
The “`set_font()“` function sets the font attributes according to these values. The “`cell()“` function is used to place text on the page.
The following code shows how to use the “`cell()“` function:
“` python
# Adding text to the page
self.cell(0, 10, ‘Certificate of Completion’, 0, 1, ‘C’)
“`
In this example, we have inserted the text ‘Certificate of Completion’ in the center of the page. The “`cell()“` function takes six parameters.
The first two parameters represent the width and height of the cell to be created. The third parameter is the text to be inserted.
The fourth parameter is a flag that can be set to 0 or 1, representing whether to draw a border around the cell or not. The fifth parameter represents whether to move the cursor to a new line after inserting the text or not.
Lastly, the sixth parameter is the alignment of the text inside the cell. The “`output()“` function is used to save the PDF document to a file.
The following code shows how to use the “`output()“` function:
“` python
# Saving document
self.output(‘certificate.pdf’)
“`
In this example, we have used the “`output()“` function to save the PDF document as ‘certificate.pdf’ in the current working directory. This function can also be used to save the document to a specific file path or to output the document as a downloadable file.
Conversion of text file to PDF file in Python
Converting a text file to a PDF file is an essential task in many document processing scenarios. The following steps show how to convert a text file to a PDF file in Python.
Step 1: Opening and reading a text file
To convert a text file to a PDF file, we first need to open and read the text file. We use the “`open()“` function in Python to open the file.
The following code shows how to open a file in read mode:
“` python
# Opening the text file in read mode
f = open(‘myfile.txt’, ‘r’)
“`
Here, we have opened the file ‘myfile.txt’ in read mode and assigned the file object to a variable named ‘f’. The content of the file can then be read using the “`read()“` function.
The following code shows how to read the file content:
“` python
# Reading file content
file_content = f.read()
“`
Here, we have read the content of the file and assigned it to a variable named ‘file_content’. Step 2: Inserting text into PDF form using the cell() function
After reading the content of the text file, we can use the “`cell()“` function to insert the text into a PDF form.
The following code shows how to use the “`cell()“` function:
“` python
# Creating a PDF document
pdf = FPDF()
# Adding a page
pdf.add_page()
# Defining font attributes
font_family = ‘Arial’
font_size = 10
font_style = ”
# Setting font attributes for text
pdf.set_font(font_family, font_style, font_size)
# Inserting text into the PDF form
pdf.cell(0, 10, file_content, 0, 1)
“`
Here, we have created a PDF document with the FPDF module, added a page to the document, set the font attributes for the text and used the “`cell()“` function to insert the text into the PDF form.
Conclusion
PDF files are versatile and useful for sharing digital documents while preserving the formatting. Python programming language has several modules available for creating and manipulating PDF files.
The FPDF module is a popular module for storing data into PDF format. Converting a text file to a PDF file is an essential task in many document processing scenarios, and Python provides convenient methods to achieve this.
With the ability to store data into PDF format and convert text files to PDF files, Python programmers have a powerful toolset available for document processing purposes. The use of Portable Document Format (PDF) files is widespread in the world of business as they are compatible with most computer systems, they allow for easy sharing of digital documents, and they retain their formatting consistency when transferred to other systems.
Python programming language is particularly useful in creating and manipulating PDF files, with the FPDF module being one of the most popular tools for handling such tasks. In the previous section, we explored how to store data in PDF format using the FPDF module in Python programming.
We discussed the essential steps required for storing data in PDF format, including importing the FPDF module, creating a PDF page, setting font attributes and inserting data, and saving the PDF document. In this section, we will expand this topic further by exploring additional features of the FPDF module that make it more powerful and versatile.
We will also delve into how the Pandas library can be used to read data from a CSV file and convert it into PDF format.
Inserting Images in PDF Documents
The FPDF module also enables users to insert images in their PDF documents. The “`image()“` function is used to embed images in a PDF document.
The following example demonstrates how to use the “`image()“` function:
“`python
from fpdf import FPDF
pdf = FPDF()
# Creating a page
pdf.add_page()
# Setting the image width and height
image_width = 100
image_height = 100
# Loading the image
pdf.image(“example-image.png”, x=10, y=10, w=image_width, h=image_height)
# Saving the PDF document
pdf.output(“example-image.pdf”)
“`
Here, we create a PDF file with an image of dimensions 100 x 100 pixels at coordinates (10,10) using the “`image()“` function.
Changing Page Margins and Orientation
Python programmers can also customize several page parameters, such as size, margins, and orientation. The following example demonstrates how to change the page size and margins of a PDF document:
“`python
from fpdf import FPDF
pdf = FPDF()
# Changing the page orientation
pdf.set_orientation(“Landscape”)
# Changing the page size
pdf.add_page(format = (100,120))
# Changing the margins
pdf.set_left_margin(10)
pdf.set_right_margin(10)
pdf.set_top_margin(20)
pdf.set_bottom_margin(20)
# Adding a title
pdf.set_title(“Example PDF”)
# Adding text and saving the PDF document
pdf.cell(200, 10, “Example Text”, 0, 2)
pdf.output(“example.pdf”)
“`
In this example, we changed the orientation of the page by using the “`set_orientation()“` function, which has two orientation modes, Portrait and Landscape. The page size was changed by using the “`add_page(format = (100,120))“` function, which sets the width and height of the page to 100 mm and 120 mm, respectively.
The margins were also changed with the “`set_left_margin()“`, “`set_right_margin()“`, “`set_top_margin()“`, “`set_bottom_margin()“` functions.
Converting CSV Files to PDF Format
Another common task in document processing is converting data from a CSV (Comma Separated Values) file to a PDF format. The Pandas library in Python provides an efficient way to read data from a CSV file and convert it into a PDF document.
The following example demonstrates how to use Pandas library for data conversion:
“`python
import pandas as pd
from fpdf import FPDF
df = pd.read_csv(“example.csv”)
pdf = FPDF()
# Adding a page
pdf.add_page()
# Setting the font family, size and style
pdf.set_font(“Arial”, size=12, style=”B”)
# Inserting the header
for col in df.columns:
pdf.cell(40, 10, str(col), 1)
# Inserting data into PDF document
for row in df.itertuples():
pdf.ln()
for col in row[1:]:
pdf.cell(40, 10, str(col), 1)
# Saving the PDF document
pdf.output(“example.pdf”)
“`
In this example, we first read a CSV file named “example.csv” using the Pandas library. Then, we create a PDF document with the FPDF module.
The “`set_font()“` function is used to set the font family, size, and style of the text. The header is inserted using a for loop for each column in the data set.
Lastly, we use a nested for loop and “`cell()“` function to insert the data into the PDF document iteratively.
Conclusion
The FPDF module in Python programming language provides extensive functionality for manipulating PDF documents, including inserting text and images, customizing page size, margins, and orientation, and generating PDF documents from data in CSV files. These features make it a popular tool for professionals in various fields such as business, education, and research.
Also, the Pandas library can be used in combination with this module to read data from CSV files and convert them into PDF documents. In conclusion, the FPDF module in Python programming provides essential tools for creating and manipulating PDF files.
We have explored the fundamental steps of storing data in PDF format, including importing the FPDF module, creating a PDF page, setting font attributes, and inserting data. Additionally, we have demonstrated how to include images, customize page size and margins, and generate PDF documents from data stored in CSV files.
The FPDF module is a powerful tool for professionals in various fields who need to process and share digital documents. By using Python programming and FPDF modules, users can efficiently handle most tasks required for document processing.