1
00:00:02,469 --> 00:00:08,410
In this video, you learn how to create PDF documents with Python from scratch.

2
00:00:09,340 --> 00:00:14,290
Specifically, we will generate this PDF via Python.

3
00:00:15,070 --> 00:00:20,230
So it's a one page PDF document that has this picture here.

4
00:00:21,220 --> 00:00:23,200
So we will add that with Python.

5
00:00:23,440 --> 00:00:26,620
It has this title, a description.

6
00:00:26,770 --> 00:00:28,020
Some text in here.

7
00:00:28,030 --> 00:00:29,230
Multi-line text.

8
00:00:30,320 --> 00:00:37,880
Similar texts in here and also more texts in a tabular format, so it's a table without borders, and

9
00:00:37,880 --> 00:00:40,070
you can also make tables with borders.

10
00:00:40,340 --> 00:00:43,790
You'll see how to create both those types of tables.

11
00:00:44,390 --> 00:00:48,260
So let's create this PDF together from scratch.

12
00:00:48,740 --> 00:00:57,470
I am on a new wrinkle here, so feel free to use any I.D. and import a PDF.

13
00:00:57,620 --> 00:01:05,450
So you need to install PDF if you don't have it already, and then you can just import from f PDF.

14
00:01:05,720 --> 00:01:07,760
That is a package you need to install.

15
00:01:08,360 --> 00:01:17,810
We only need that FPGA EV class in uppercase lizards and then right away, we need to create an PDF

16
00:01:18,110 --> 00:01:19,550
object instance.

17
00:01:20,480 --> 00:01:23,570
So that's a b c d f in parentheses.

18
00:01:23,930 --> 00:01:27,530
And here we give a bunch of arguments.

19
00:01:27,800 --> 00:01:37,060
The first one is the orientation you want to give to your PDF, so we could have P or L.

20
00:01:37,070 --> 00:01:40,370
So P stands for a portrait orientation.

21
00:01:41,030 --> 00:01:47,210
So the PDF you just so on my screen was in portrait mode.

22
00:01:48,740 --> 00:01:54,920
Then we can also give the units in p t so points.

23
00:01:56,510 --> 00:02:02,210
There are different units as well, but also just use p t, and this is the units that will be used

24
00:02:02,300 --> 00:02:04,220
for the text.

25
00:02:04,550 --> 00:02:11,690
So when we see, for example, font is equal to 12, that is the size of the font and that is a standardized

26
00:02:12,500 --> 00:02:14,720
units that you see use in.

27
00:02:14,720 --> 00:02:22,010
In a word processing program such as Microsoft Word or text of 12, for example, has a standard size,

28
00:02:22,220 --> 00:02:22,950
a small font.

29
00:02:22,950 --> 00:02:24,980
So then you have 14 and 16 and so on.

30
00:02:26,820 --> 00:02:35,610
And you may also want to specify the format, so a four would be the formats if you are in Europe,

31
00:02:35,610 --> 00:02:36,210
for example.

32
00:02:37,720 --> 00:02:41,440
And then you have a letter if you are in the U.S. and so on.

33
00:02:42,040 --> 00:02:48,190
So A4 is 298 by 210 mm.

34
00:02:48,370 --> 00:02:55,640
The dimensions of the page, once you have that object, then this is just an empty PDF document.

35
00:02:55,670 --> 00:02:57,620
For now, it doesn't have any pages.

36
00:02:57,820 --> 00:03:06,640
So the next thing we want to do is add a page using the add on the score page methods of that's.

37
00:03:06,640 --> 00:03:08,710
It's no need for arguments there.

38
00:03:10,530 --> 00:03:12,420
Then we were ready to add contents.

39
00:03:12,690 --> 00:03:20,430
So the way this works is that you should start from top to bottom, so add contents to the top of the

40
00:03:20,430 --> 00:03:25,960
PDF and then keep adding on there that content.

41
00:03:26,280 --> 00:03:33,960
Sue, the first line that you saw in the PDB that I showed you the top content was an image.

42
00:03:34,140 --> 00:03:36,060
So let's see how we can add an image.

43
00:03:36,240 --> 00:03:41,850
You point to that variable, which contains the PDF object instance.

44
00:03:42,330 --> 00:03:48,180
And then you just point to image to see a complete list of methods.

45
00:03:48,480 --> 00:03:50,610
You can use the help function.

46
00:03:51,000 --> 00:03:57,390
So in an interactive python shell, import PDF and then use help.

47
00:03:59,130 --> 00:03:59,790
Let me do that.

48
00:04:01,010 --> 00:04:03,080
So EPRDF package.

49
00:04:05,580 --> 00:04:09,030
So I'll install this package first before I use it in the interactive show.

50
00:04:13,500 --> 00:04:18,390
Of just import EPRDF.

51
00:04:19,420 --> 00:04:21,760
Then I'll create that PDF.

52
00:04:22,860 --> 00:04:25,620
Instance of then help PDF.

53
00:04:27,140 --> 00:04:31,400
And then you should see how to use PDF.

54
00:04:33,290 --> 00:04:40,640
And then you have a method here only to see a more cleaner list of the methods just used the PDF.

55
00:04:40,950 --> 00:04:46,880
So PDF is the variable and you'll see that image is among those methods, not the image methods.

56
00:04:48,120 --> 00:04:53,640
This gets as inputs, the path to the image you want to add.

57
00:04:54,330 --> 00:04:58,840
So I'm going to add an image in this project directory.

58
00:04:59,100 --> 00:05:02,460
I'm going to upload it from my folders.

59
00:05:02,640 --> 00:05:05,160
I have this tiger, that Jeep tag image.

60
00:05:05,340 --> 00:05:11,490
You can also find this image attached in the luxury sources and then place it in the same directory

61
00:05:11,490 --> 00:05:13,590
with your project.

62
00:05:13,680 --> 00:05:18,750
So with your main data profile and then you pass the path to that image.

63
00:05:19,410 --> 00:05:20,940
So that's the first argument.

64
00:05:22,260 --> 00:05:29,760
There are two more arguments you need to give, and that is the width of the contents of the image in

65
00:05:29,760 --> 00:05:37,380
this case, and I'll put eighty eight p t and the the height outputs 50.

66
00:05:37,500 --> 00:05:37,960
Why?

67
00:05:38,310 --> 00:05:45,990
Because I know that this image is eight hundred pixels wide and 500 pixels.

68
00:05:48,010 --> 00:05:48,400
Hi.

69
00:05:48,720 --> 00:05:57,190
So I'm trying to keep the same ratio here, not to distort the image, so I'm doing 80 and 50, so the

70
00:05:57,190 --> 00:06:02,740
image will be like 10 times smaller than what's the original size is.

71
00:06:03,400 --> 00:06:07,330
Now we can keep adding content here, and we will do that.

72
00:06:07,630 --> 00:06:14,230
But before we do that, I would just like to show you how you can finish the generation of the PDF file

73
00:06:14,560 --> 00:06:20,380
so you can point to the ultimate method and then you define the path you declared the path you wants.

74
00:06:20,770 --> 00:06:22,900
This PDF to be saved, too.

75
00:06:23,230 --> 00:06:29,560
So if you just see also the PDF, this PDF will be saved in the same project directory among these files.

76
00:06:29,980 --> 00:06:37,600
So all the wrong that and this is the generated file and see as a PDF, the tiger is just here in the

77
00:06:37,600 --> 00:06:38,080
corner.

78
00:06:38,710 --> 00:06:44,260
Now you might be wondering what if we want to put the tiger at the very right corner in here?

79
00:06:44,650 --> 00:06:52,180
I'll show you all to do that on, and you should work with image methods again and you can play around

80
00:06:52,180 --> 00:06:57,550
with the X and you also have a Y parameter to change the vertical position.

81
00:06:57,700 --> 00:07:02,650
So with X, if you set it to five phones, it's pretty.

82
00:07:03,070 --> 00:07:10,330
I think that should be a good number to place the tiger 500's points to the right or wrong again, and

83
00:07:10,330 --> 00:07:13,210
that will override the existing outputs.

84
00:07:13,480 --> 00:07:22,660
File a PDA file and you see that the image now is on the right corner, so if you just remove that,

85
00:07:23,050 --> 00:07:25,450
it will go again to the left corner.

86
00:07:26,110 --> 00:07:28,750
Next, let's add a title.

87
00:07:28,900 --> 00:07:32,440
A big heading for the PDF documents.

88
00:07:33,370 --> 00:07:43,720
So to add text now you use PDF of that cell and you need to pass a Y, which is the width of the cell.

89
00:07:44,800 --> 00:07:52,840
So the PDF creation is based on sales, so you add a table and you add sales to the table.

90
00:07:53,740 --> 00:08:03,490
Now the with if you set it to zero, that means that sale will occupy the entire width of that particular

91
00:08:03,490 --> 00:08:04,030
room.

92
00:08:06,080 --> 00:08:12,710
And the height also of the texts may be 30 to 50 and see what happens.

93
00:08:13,820 --> 00:08:22,610
Finally, text is the argument that contains the text that you want to place in that part of a PDF.

94
00:08:22,880 --> 00:08:25,670
So let's see Malayan Tiger.

95
00:08:26,130 --> 00:08:30,820
We're creating of this PDF with data about this kind of tiger.

96
00:08:30,830 --> 00:08:39,530
So let's say a biology reports, or you could also implement a bill with data about the customer or

97
00:08:39,530 --> 00:08:40,940
whatever you need in your job.

98
00:08:41,870 --> 00:08:43,130
So this is just an example.

99
00:08:44,270 --> 00:08:45,980
Let's execute it like this.

100
00:08:48,240 --> 00:08:55,110
But we will get an error because we haven't defined the forms just yet.

101
00:08:56,040 --> 00:08:58,380
So we see it has something to do with the funds.

102
00:09:00,310 --> 00:09:08,250
Therefore, before you create a cell, you should define the forms of that cell using PDA of the set

103
00:09:08,620 --> 00:09:09,140
font.

104
00:09:09,430 --> 00:09:12,790
So this method should be above the cell method.

105
00:09:15,530 --> 00:09:19,580
And then you say family is perhaps times it's one of the families.

106
00:09:21,140 --> 00:09:23,260
Style, let's set it to bolt.

107
00:09:23,530 --> 00:09:27,680
Use a Capital B for that and the size of a font.

108
00:09:28,370 --> 00:09:32,060
Let's say 24 points run again.

109
00:09:33,570 --> 00:09:41,970
This time I have a type of family, a sort of family run again, and we should see the update, its

110
00:09:42,210 --> 00:09:43,380
outputs this time.

111
00:09:43,680 --> 00:09:49,860
Somalian tiger, it's by default at the left position of this next cell.

112
00:09:50,100 --> 00:09:54,420
So we got the image cell in here of next cell.

113
00:09:54,480 --> 00:09:55,200
Let's please.

114
00:09:55,200 --> 00:09:55,980
It's in the center.

115
00:09:57,000 --> 00:09:59,430
You do that in the cell itself.

116
00:10:00,420 --> 00:10:03,870
So a line is equal to capital C..

117
00:10:04,050 --> 00:10:09,030
So you see style with capital B. Alignment with Capital C, which means the center.

118
00:10:10,560 --> 00:10:19,500
And so this time we got it for right now, if you add a border of one.

119
00:10:23,080 --> 00:10:31,060
And Ron, you see how the sale looks like, so now you understand that this is really a sell.

120
00:10:31,510 --> 00:10:34,870
So it occupies the full width of the PDF.

121
00:10:38,400 --> 00:10:47,940
Text area, and if you change the words to something else, for example, 200, you've see that the

122
00:10:47,940 --> 00:10:50,370
width of the cell changes.

123
00:10:50,760 --> 00:10:56,160
But that is not the best use when you want to keep the text of the sensor.

124
00:10:56,430 --> 00:10:59,760
So just keep that with zero and you'll be fine.

125
00:11:01,260 --> 00:11:03,720
So that's what we want.

126
00:11:04,740 --> 00:11:12,030
I'm going to remove the border as well and keep adding more cells, so PDAF that phones for another

127
00:11:12,030 --> 00:11:12,960
cell family.

128
00:11:16,960 --> 00:11:22,720
Is going to be times again, the style is Bolt again.

129
00:11:25,810 --> 00:11:38,430
The size will be 14 %dv they'll sell W equal to zero height is equal to perhaps 50 and 60.

130
00:11:38,450 --> 00:11:47,260
This time I want to place the scription no Ron and see that we will have an issue in here.

131
00:11:47,590 --> 00:11:54,400
You see, the scription starts right here in the same room as the Malayan tiger.

132
00:11:56,020 --> 00:12:03,880
Text the problem here is here that we didn't declare that we want a break line anywhere, so we want

133
00:12:03,880 --> 00:12:04,510
to do that.

134
00:12:04,750 --> 00:12:11,530
And you do that in the Malayan tiger cell without and is equal to one.

135
00:12:12,250 --> 00:12:20,800
So that means once this cell and so we will have a break line and then the next sale will start in a

136
00:12:20,800 --> 00:12:21,790
new position.

137
00:12:22,330 --> 00:12:23,680
So you see description now.

138
00:12:24,220 --> 00:12:25,240
We didn't censor it.

139
00:12:25,240 --> 00:12:26,830
So that's why it's on the left.

140
00:12:27,610 --> 00:12:28,690
So the description?

141
00:12:30,340 --> 00:12:32,490
Let's add another cell.

142
00:12:33,010 --> 00:12:38,400
So before that, we need to use land is equal to one again and then Peter.

143
00:12:38,410 --> 00:12:46,120
But since forms family times and size is 12 this time.

144
00:12:46,330 --> 00:12:52,050
So we'll have some description of both the tiger here and then PGA of the sale.

145
00:12:53,780 --> 00:13:04,040
With this with zero height is 50 and 60 to 60, that's argument a sequel to some long texts.

146
00:13:04,310 --> 00:13:07,620
So what I'll do is I'll creates a multi-line text.

147
00:13:07,700 --> 00:13:15,170
You want a sequel to those three quotes in here, and I will get some texts from the Malian Tiger Wikipedia

148
00:13:15,170 --> 00:13:15,550
page.

149
00:13:15,560 --> 00:13:23,030
So just find this Wikipedia page on Google and just copy two or three lines and pays them there.

150
00:13:23,810 --> 00:13:32,510
Close them with three other quotes one two three, and we have a multiplying string there, which provides

151
00:13:32,630 --> 00:13:35,620
for the text the argument.

152
00:13:36,560 --> 00:13:43,670
Now the problem now is that if I run this, you see the output does not look as you'd expect.

153
00:13:44,060 --> 00:13:47,930
So after the description here we have quite a lot of space.

154
00:13:48,440 --> 00:13:49,360
That's one problem.

155
00:13:49,400 --> 00:13:57,320
The other problem is that this text is not rabbits, so we don't see the text off of this anymore.

156
00:13:57,710 --> 00:13:59,540
Let's fix these two problems.

157
00:13:59,720 --> 00:14:07,730
First, this issue with a space in here, we have that big space because the heights of the description

158
00:14:07,730 --> 00:14:08,720
cell is 50.

159
00:14:09,260 --> 00:14:11,210
That, to me, is the height of the cell.

160
00:14:11,810 --> 00:14:19,700
As you can see, when we visualize a border, the height is this.

161
00:14:19,910 --> 00:14:22,210
So that is 50.

162
00:14:22,700 --> 00:14:28,400
We want to decrease that so that we have less space between that and that.

163
00:14:28,790 --> 00:14:36,800
Actually, we want to decrease the height of both the text of the description and the text of this description

164
00:14:36,800 --> 00:14:37,340
label.

165
00:14:37,670 --> 00:14:44,000
So let's make that 15 and the other one 15 run.

166
00:14:45,430 --> 00:14:46,410
Cloth outputs.

167
00:14:46,550 --> 00:14:47,650
Yeah, it looks better.

168
00:14:49,720 --> 00:14:56,290
Of course, we don't want that border line of description label.

169
00:14:56,650 --> 00:15:03,310
And no, let's fix the text wrapping issue to fix that.

170
00:15:03,320 --> 00:15:04,360
You cannot fix it.

171
00:15:04,360 --> 00:15:09,850
With this methods, you need to use another method when you want to add to multi-line texting a PDF

172
00:15:09,850 --> 00:15:11,140
file with Python.

173
00:15:11,560 --> 00:15:16,480
In that case, you use a multi underscore sale methods.

174
00:15:17,620 --> 00:15:19,030
That's all we have to change.

175
00:15:19,300 --> 00:15:23,890
So run that again, see the artifacts and it looks better.

176
00:15:25,390 --> 00:15:27,040
So that fixes the issue.

177
00:15:27,550 --> 00:15:30,310
Let's add some more cells.

178
00:15:31,990 --> 00:15:37,030
So I want to add to know these to some data.

179
00:15:38,170 --> 00:15:44,320
So the kingdom and then next to that, I want to at Animalia and the phylum Chordata.

180
00:15:44,590 --> 00:15:46,270
So let's add these dots on here.

181
00:15:47,080 --> 00:15:47,860
We have to.

182
00:15:49,200 --> 00:15:55,500
Do these two more times, so Seth won't sell one to one here, one here.

183
00:15:56,250 --> 00:16:00,570
So the first one was the kingdom.

184
00:16:02,640 --> 00:16:07,440
Kingdom there, so that's a sin bolt.

185
00:16:08,460 --> 00:16:12,930
And then after Kingdom, we don't want the line, this thing, we don't want a brake line.

186
00:16:13,140 --> 00:16:16,470
We want the next sale, which is Animalia.

187
00:16:17,250 --> 00:16:22,380
So we want that sale to be just next to that's even the same in the same line.

188
00:16:22,860 --> 00:16:26,070
So that's why I removed the N1 ports.

189
00:16:26,430 --> 00:16:27,660
I don't want this to be bought.

190
00:16:27,940 --> 00:16:31,110
Remove the style completely.

191
00:16:33,010 --> 00:16:41,110
And, yeah, kingdom, but we have a lot of space because now we need to play around with the width,

192
00:16:41,500 --> 00:16:45,070
which is zero, so zero means the sale.

193
00:16:46,190 --> 00:16:52,130
Expands from here to there, and the next sale starts where the previous sale ends.

194
00:16:52,550 --> 00:16:54,740
So let's make the width smaller.

195
00:16:57,390 --> 00:17:02,790
A width of 100 should do it for both run.

196
00:17:04,619 --> 00:17:07,170
Cereals, fruits, and it comes closer.

197
00:17:08,780 --> 00:17:10,400
Of course, you can also use.

198
00:17:16,270 --> 00:17:25,660
Colin there to make it look cleaner and grammatically correct, so I'll copy those to and put them here

199
00:17:25,660 --> 00:17:25,930
again.

200
00:17:26,380 --> 00:17:28,119
So this is the last one.

201
00:17:28,569 --> 00:17:31,540
That is the film called Tom.

202
00:17:36,170 --> 00:17:36,590
Run.

203
00:17:38,980 --> 00:17:40,270
And that's the ultimate.

204
00:17:41,370 --> 00:17:45,660
I'll increase the heights a little bit to that, because this is too small.

205
00:17:46,260 --> 00:17:50,820
25, 25, 25, 25 run.

206
00:17:52,380 --> 00:17:53,330
And that's the ultimate.

207
00:17:55,260 --> 00:17:59,850
So that's a neat PDF document that we generated via Python.

208
00:18:00,060 --> 00:18:01,350
This was just an example.

209
00:18:01,650 --> 00:18:06,600
And it was manual work, so we added those dots on manually.

210
00:18:06,600 --> 00:18:15,300
Both in the next video is what I'll do is I will get the data from an Excel file and our creates multiple

211
00:18:15,300 --> 00:18:19,380
PDF reports using data from that Excel file.

212
00:18:19,560 --> 00:18:26,370
So the Python script will gather data from the Excel file, and it will automatically generate multiple

213
00:18:26,370 --> 00:18:29,610
PDF documents and with those data insights.

214
00:18:29,910 --> 00:18:31,170
So I'll talk to you in the next video.

215
00:18:31,170 --> 00:18:31,620
This you.

