Python 3 FPDF Library Script to Export Text File to PDF File With Multiple Pages in Terminal
1K views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-fpdf-library-script-to-export-text-file-to-pdf-file-with-multiple-pages-in-terminal/
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you a
0:04
Python script which actually converts a
0:07
text file into PDF document so if you
0:10
have a bunch of text inside a txt file
0:14
something like this so I have this file
0:17
here i will be converting this file to a
0:19
PDF document using this Python
0:21
open-source library
0:24
FPDF so if I execute this script right
0:28
here just let me just execute this I say
0:33
python app py so just see on
0:37
the it is saying that the module is not
0:42
so first of all you need to install this
0:44
module so fp pip install fpdf so it will
0:48
install this and then if I execute the
0:52
script so here you just need to replace
0:55
the file name so the file name is filet
0:58
txt so I will just replace this and
1:01
execute this script you will see it will
1:03
create the PDF saved
1:07
as so if I open this now you will see
1:10
that the PDF has been successfully
1:12
inserted
1:14
successfully with full formatting you
1:17
will see multiple lines are there so let
1:19
me just open it in the browser
1:22
so your uh text file has been
1:25
successfully converted to PDF document
1:27
you will see
1:28
that we have that bunch of text inside
1:32
this txt file like this and we converted
1:36
this to PDF so for
1:39
uh making this application we are using
1:42
this library FP PDF which is a very
1:45
popular PHP PDF processing library but
1:48
you can even use it inside Python as
1:50
well so this is its official website
1:54
FPDF
1:55
library so for this you just need to
1:58
install the Python module for this so
2:02
just search for this module
2:04
FPDF so this is the command here pip
2:07
install FPDF so
2:10
I simply install this i have given all
2:14
the script in the description of the
2:16
video so let me just show you the script
2:18
here step by step how to do this so
2:21
first of all just create a simple Python
2:24
file and just import this
2:27
FPDF from this we need to import this
2:30
class
2:31
FPDF and then we just need to initialize
2:34
this FPDF library like this after that
2:38
we just need to set some options for
2:40
first of all you have a bunch of methods
2:43
right here we will see you can set meta
2:45
properties regarding your PDF document
2:48
so one such method is uh set margins you
2:51
can set the margin of the PDF document
2:55
the left margin you can set the top
2:58
margin all the four sides you can set
3:01
the margin right
3:05
here after setting the margin we can
3:08
simply add a new page for adding the new
3:11
page we have this method which is add
3:14
underscore page so this will add a new
3:16
page in the PDF document after adding a
3:19
blank page now we can simply insert the
3:22
text before that we can set the font
3:24
here we can even set a custom font as
3:27
well let's suppose you just need to
3:29
provide the name of the font which is
3:31
AIAL and then the size of the font you
3:34
can even control it let's suppose I say
3:36
20 and after that we read all the data
3:40
which is there inside the filet txt so
3:43
this file is present in the same
3:45
directory so we are reading this
3:48
file in the read mode so inside read
3:51
mode and here we just need to provide
3:54
the encoding which will be your
4:00
UTF8 and then we just say as f and then
4:05
we simply read the file f dot the read
4:09
function so it will read all the text
4:11
which is present in the txt file and
4:14
store it inside your text variable after
4:17
that we contains this function here
4:18
which is multi- cell it will add the
4:22
text in the PDF document you just need
4:25
to provide the X coordinate and the Y
4:27
coordinate and actually the
4:29
text
4:32
TXT and whatever is the
4:35
text and then you just need to provide
4:38
the alignment so J is the alignment
4:41
after that we simply save the PDF file
4:44
by using this output method and here you
4:47
just provide the output PDF file name
4:50
that's all so this is the overall script
4:53
right here uh if
4:55
I again execute this you will see it
4:58
will first of all read
5:05
the let me say python app py so you will
5:08
see it will create this uh result dot
5:11
PDF
5:12
file and if I try to open this you will
5:15
see now it has successfully splitted
5:18
across multiple pages depending upon the
5:21
length of the text here you will
5:25
see as the text was larger you will see
5:28
it has splitted the text into multiple
5:30
pages so now it contains two pages so in
5:34
this easy way you can create
5:37
text you can create PDF document
5:40
directly from the txt file using this
5:42
package f PDF inside
5:45
Python you can adjust the font size
5:47
let's suppose I change the text size to
5:51
40 so now you will see the text size
5:55
will look like this so you can adjust
5:58
the size here of the
5:59
text i think 15 looks good
6:03
so so if you need the full source code
6:06
the link is given in the description so
6:08
in this way you can make the PDF
6:11
document using the text file also check
6:14
out my website freemediattools.com
6:17
uh which contains thousands of tools