Week #1
Library fpdf
Nowadays pdf is the most important source for a text document. Every here a there we always encounter a pdf file. And for those pdf file, there are plenty of software and that allows us to read, create, and convert any text file.
Python is truly the best dynamic programming language with lots of features and tons of packages. Keeping a good hand in python is very necessary for every programmer and when we talk about some enthusiastic DIY kind of person who just wants to build something from scratch. As some software provides prebuild solution for your daily life they come with premium features which require to pay a huge amount for performing the task.
In this episode, I will teach you how to use fpdf (a python package for creating a pdf file). So let get started.
What is fpdf?
Fpdf is a Python package for generating or creating a pdf file. This package contains a lot of predefined features and allows page format, margins. It also allows managing page header and footer. It also supports Unicode encoding. It has support for images(jpg, png, gif ), barcode.
Where to start?
It's simple just you have to navigate to your terminal and have to type the following command to install fpdf library to your machine.
Pip install fpdf
Now as we have installed the fpdf library now we have dive in to our project.
For that just create a new project and open it in a text editor.
1st thing we have to do is to import the library into our project.
From fpdf import FPDF
Now as we have declared the import file we have to instantiate fpdf class
pdf = fpdf()
Now as we have declared the object it to.e to declare a page
pdf.add_page()
Now we have to add styling
Syntax
pdf.set_font("font name", size=n)
here n = font size
Example
pdf.set_font("Roboto", size=10)
As we have declared font name and font size, now its time to create a reactangular box (invisible) inside which we keep our text.
Syntax
pdf.cell(width, height,ln, align, txt)
Example
pdf.cell(200, 100,ln = 1, align = c, txt = "This is a demo pdf designed using python")
Finally we have to make an output of the file
Syntax
pdf.output("file_name.pdf")
Conclusion
We have created an application which enable us to create pdf files. This library i.e. fpdf not only support python but have a huge base for its implementation. This program can be upgraded to very high and complexity where we can convert or merge pdf. So practice more and more to upgrade your knowledge base and make some thing great.
Stay safe and stay connected
If you like please share this blog.
Thanks
Comments
Post a Comment