-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy path06-creating-a-pdf-file-from-scratch.py
55 lines (38 loc) · 1.31 KB
/
06-creating-a-pdf-file-from-scratch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# ----------------------
# Using the Canvas Class
# ----------------------
from reportlab.pdfgen.canvas import Canvas
canvas = Canvas("hello.pdf")
canvas.drawString(72, 72, "Hello, World!")
canvas.save()
# ---------------------
# Setting the Page Size
# ---------------------
from reportlab.lib.units import cm, inch # noqa
print(cm)
print(inch)
canvas = Canvas("hello.pdf", pagesize=(8.5 * inch, 11 * inch))
from reportlab.lib.pagesizes import LETTER # noqa
canvas = Canvas("hello.pdf", pagesize=LETTER)
print(LETTER)
# -----------------------
# Setting Font Properties
# -----------------------
canvas = Canvas("font-example.pdf", pagesize=LETTER)
canvas.setFont("Times-Roman", 18)
canvas.drawString(1 * inch, 10 * inch, "Times New Roman (18 pt)")
canvas.save()
# The code below creates a PDF with blue text
from reportlab.lib.colors import blue # noqa
from reportlab.lib.pagesizes import LETTER # noqa
from reportlab.lib.units import inch # noqa
from reportlab.pdfgen.canvas import Canvas # noqa
canvas = Canvas("font-colors.pdf", pagesize=LETTER)
# Set font to Times New Roman with 12-point size
canvas.setFont("Times-Roman", 12)
# Draw blue text one inch from the left and ten
# inches from the bottom
canvas.setFillColor(blue)
canvas.drawString(1 * inch, 10 * inch, "Blue text")
# Save the PDF file
canvas.save()