Okay.
So once this package has been set up, let's go ahead and let's start using this package.
So what I would do is I would go ahead and create a folder on my desktop.
So I've already created that and that folder is named as PDF, and I'll simply open this thing up in
VS code.
So let me quickly open up vs code and let's start working with this package.
Okay.
So here I've actually opened up the folder which is PDF and over here let's create a new python file
so that we could write some python code in it.
So I'll name this code as demo.py, which is the by default name which I always use.
And over here, let's go ahead and let's learn how exactly we could use that fpdf package.
So we all know that the Fpdf package which we have installed is used for creating a PDF file.
So now the question is how exactly we could use the code inside that package.
So let's first import that package and let's see what kind of things which we have in there.
So over here I would say from Fpdf import that's going to be Fpdf.
So what this line of code does is that from the Fpdf package, it imports this PDF and if you want to
see what exactly this pdf is, you could actually go ahead and press command or Ctrl and click this.
As you can see, you will be redirected to this particular class.
And mind you, don't go ahead and modify this because this is the code inside the package which we have
just installed.
And if you take a look at this, this is a huge class which is pre-written.
And here we also have a constructor over here as well, which could be used to go ahead and create an
object of this fpdf class.
So you'll be able to see that there are a whole bunch of parameters being passed here.
And what you could do is if you want to create an object of this fpdf, you could simply do that.
So if I want to go ahead and create an object of Fpdf, I could say PDF or any object name equals that's
going to be f PDF.
And as this is a class, I could say something like this.
So this line of code is nothing, but it's similar to what we have worked with before.
So in the previous case we had actually worked with creating classes, creating objects.
And whenever we have to create an object of the class, we do the same thing as over here.
Now, that's exactly what we have done here as well.
We have essentially created an object of this inbuilt class.
Now, if you take a look at this inbuilt class, it has this particular constructor and you'll also
be able to see that some by default values are already assigned to this constructor.
So, for example, over here we have the values for orientation.
We have the unit which is set to M, So this orientation basically means the type of page you want to
create.
So if you want to create a page in portrait mode, you could do that.
But if you want the page to be present in a landscape format, you could specify that over here as well.
But the by default value of this is actually set to portrait.
And similarly, you also have to work with units whenever we are working with pages.
So the basic unit, which is by default set here, is millimeters.
Now the formatting of the page is set to A4 as well.
That means you will be working with an A4 size paper.
And then you also have another parameter here which is now deprecated, which means that you no longer
could use it.
Okay, so now once we have created the object of this, now the question is why exactly have you created
the object of this class?
And that's because whenever you have to make use of a package, you have to create an object of that
particular instance so that you could access the different methods which are available here.
So if you scroll down a little bit, you'll be able to see that there are a whole bunch of methods here
inside this particular class itself.
So for example, here we have the class.
If you scroll down after the constructor, we have this method, which is write HTML, page model,
EPA, EPW pages count, so on and so forth.
So these different methods which are written inside these particular packages could be utilized by us
once we have created those objects.
And now, as this package is used to create a page, there is a method which is called as Add page method,
which we could use to create or add a page.
So now whenever you have to create a PDF document, we have to first create a page.
So here I'll make use of the object.
So PDF is the object of this class and we all know that a method has to be invoked on an object.
So I could say dot add underscore page and that will create the page for us.
Now if you want to take a look at the code for Add page, you don't have to find it out over here.
You could again, go ahead, hold the command key or the Ctrl key and click on this and you will be
able to see that.
Now you are directly redirected to the method, which is the Add page method.
And if you also take a look at this, this add page method is actually present this class, which is
fpdf.
So let's scroll all the way down over here.
Let's simply click on this thing one more time.
And here we have this method.
Now if you take a look at the method inside the package, you'll also be able to see that there's a
whole bunch of.
So over here as well, which is you could set the orientation, you could set the formatting, so on
and so forth.
So if you want, you could set them over here.
But for now, we are going to leave it empty.
And this is going to work without constructors as well because the by default value is automatically
set here.
So after using the Add page method, now whenever a page is created, it's a rule that we must always
set the font which we want to use on that page.
So in order to set the font, there's a method which is called as set font, so I could say.
PDF dot set underscore font.
And here if you're confused what kind of parameters you need to pass in, you could again do the same
thing that is command click and you'll be able to see the kind of data you need to pass here.
So the first parameter which you need to pass is the family.
And here they will also mention the kind of data you need to pass here.
So you need to mention the font family in terms of a string.
So let's pass in the font family here as let's say Helvetica.
So hell.
Way to go, which is a font family.
Then let's take a look at the next parameter which you need to pass in.
Then you have to pass in the font style.
So if you hover over this, as you can see, it gives you the option for font styles.
So it could be bold italic, underline bold and underlined, underline and bold, so on and so forth.
So what you could do here is you could give a comma and you could simply say B, because that is one
option here which you could pass in.
So it shows you those options over here as well.
If you hover on that and the next parameter which you have to pass in over here is the size.
And the size has to be mentioned in terms of an integer.
So here, let's go ahead and let's say we want the font size to be 16 and that's it.
We have now set the font of a page.
Now once we have the page and once we have set the font, we can now finally go ahead and add some text
to the page.
But in Fpdf you cannot directly add a text to the page.
Instead, you actually have to add a cell to the page and you have to add the text to that particular
cell.
Now, a cell in Fpdf is a rectangular area which could contain text, It could contain border as well.
It could also have a background color and the foreground color as well.
And it could be aligned and placed anywhere on the page as we want.
So in order to create a cell, I would say again PDF, which is the object dot cell.
And again, if you're confused what kind of parameters you have to pass in to this one, you could click
on this and straight away you'll be able to see that there's a whole bunch of parameters which you could
pass here, so you could pass in the width, you could pass in the height, you could pass in the text.
So for now, let's pass in these three parameters here.
So here I would say that I want to set the width of this thing to let's say 40, let's say height as
ten, and let's say I want to display the text as Hello world and that's it, I'll save this and we
should be good to go.
Now this actually created our page.
This actually set the font and this actually created a cell and kind of added it to the page.
But now the question is how exactly would you generate an output out of this file?
Because our ultimate goal is to go ahead and create an output.
So if I want to produce an output here, I would simply say PDF dot output.
And I here have to mention the name of the file or the path in which I want to create that file.
So let's say I want to create that output file over here itself.
So in order to do that, I would mention the path over here as the name of the file.
Let's say I name this file as sample PDF.
So now if I save this code, I have to run this.
So let me go back to the terminal.
And over here, first of all, I'll navigate to the path where we have the file.
So I'll go to CD desktop and the name of my folder is PDF.
So CD pdf.
If I type in LHS, I'll be able to see the demo.py file.
So I will say python three demo.py.
And if you're on windows you have to type in python demo.py.
So if I hit enter the code executed and it stopped.
So let's go back to VS code and you'll be able to see that we have a sample pdf file generated up over
here.
So let's open this file and let's check the contents.
So I'll simply minimize this and if I open this thing up, as you can see, we have two files.
One is our code file and then we have this PDF file created as well.
So if I open it up.
As you can see, it created a PDF file which contains the letters Hello World.
So that means we have successfully created our very own pdf file by making use of Python and the Fpdf
package.
Now in the next lecture, let's go ahead and let's learn how you could customize this particular page
according to our own needs.
That is, let's say if you want to add some kind of more data like a header and footer.
So let's learn about that in the next one.