使用Python创建 PDF 文档
在本文中,我们将学习如何在Python创建 PDF。一个非常有名的模块叫做 pypdf2 用于修改和读取现有的 pdf,但它的主要缺点是它不能创建新的 pdf 文件。所以今天我们正在学习另一个名为 reportlab 的Python模块,它可以帮助我们创建新的 pdf 文件并在其上编辑我们的心脏内容。
模块要求:
Reportlab:该模块用于处理 PDF 文件。
pip install reportlab
循序渐进的方法:
第1步:
我们首先导入模块和类。 Canvas 用于在 pdf 上绘制东西,ttfonts 和 pdfmetrics 将帮助我们在 pdf 中使用自定义 TTF 字体,颜色将帮助我们轻松选择颜色而无需记住它们的十六进制值。
Python3
# importing modules
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
from reportlab.lib import colors
Python3
# initializing variables with values
fileName = 'sample.pdf'
documentTitle = 'sample'
title = 'Technology'
subTitle = 'The largest thing now!!'
textLines = [
'Technology makes us aware of',
'the world around us.',
]
image = 'image.jpg'
Python3
# creating a pdf object
pdf = canvas.Canvas(fileName)
# setting the title of the document
pdf.setTitle(documentTitle)
Python3
# registering a external font in python
pdfmetrics.registerFont(
TTFont('abc', 'SakBunderan.ttf')
)
# creating the title by setting it's font
# and putting it on the canvas
pdf.setFont('abc', 36)
pdf.drawCentredString(300, 770, title)
Python3
# creating the subtitle by setting it's font,
# colour and putting it on the canvas
pdf.setFillColorRGB(0, 0, 255)
pdf.setFont("Courier-Bold", 24)
pdf.drawCentredString(290, 720, subTitle)
Python3
# drawing a line
pdf.line(30, 710, 550, 710)
# creating a multiline text using
# textline and for loop
text = pdf.beginText(40, 680)
text.setFont("Courier", 18)
text.setFillColor(colors.red)
for line in textLines:
text.textLine(line)
pdf.drawText(text)
Python3
# drawing a image at the
# specified (x.y) position
pdf.drawInlineImage(image, 130, 400)
# saving the pdf
pdf.save()
Python3
# importing modules
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
from reportlab.lib import colors
# initializing variables with values
fileName = 'sample.pdf'
documentTitle = 'sample'
title = 'Technology'
subTitle = 'The largest thing now!!'
textLines = [
'Technology makes us aware of',
'the world around us.',
]
image = 'image.jpg'
# creating a pdf object
pdf = canvas.Canvas(fileName)
# setting the title of the document
pdf.setTitle(documentTitle)
# registering a external font in python
pdfmetrics.registerFont(
TTFont('abc', 'SakBunderan.ttf')
)
# creating the title by setting it's font
# and putting it on the canvas
pdf.setFont('abc', 36)
pdf.drawCentredString(300, 770, title)
# creating the subtitle by setting it's font,
# colour and putting it on the canvas
pdf.setFillColorRGB(0, 0, 255)
pdf.setFont("Courier-Bold", 24)
pdf.drawCentredString(290, 720, subTitle)
# drawing a line
pdf.line(30, 710, 550, 710)
# creating a multiline text using
# textline and for loop
text = pdf.beginText(40, 680)
text.setFont("Courier", 18)
text.setFillColor(colors.red)
for line in textLines:
text.textLine(line)
pdf.drawText(text)
# drawing a image at the
# specified (x.y) position
pdf.drawInlineImage(image, 130, 400)
# saving the pdf
pdf.save()
第2步:
接下来,我们将在文档中写入和绘制的所有内容初始化为特定变量,以便在需要时轻松调用它们。
蟒蛇3
# initializing variables with values
fileName = 'sample.pdf'
documentTitle = 'sample'
title = 'Technology'
subTitle = 'The largest thing now!!'
textLines = [
'Technology makes us aware of',
'the world around us.',
]
image = 'image.jpg'
第 3 步:
接下来,我们用 pdf 的名称初始化一个画布对象,并将标题设置为 documentTitle。
蟒蛇3
# creating a pdf object
pdf = canvas.Canvas(fileName)
# setting the title of the document
pdf.setTitle(documentTitle)
第四步:
接下来,我们使用 pdfmetrics 和 TTFont 将我们的外部字体注册到 reportlab 字体并为其分配一个名称。接下来,我们设置新字体的大小。然后我们使用 drawCentredString函数在 pdf 上绘制字符串,该函数以 x 和 y 值作为文本的中心写入文本,并相应地调整文本的左、右、上和下。请注意,我们需要文件夹中存在 TTF 文件才能执行命令。
蟒蛇3
# registering a external font in python
pdfmetrics.registerFont(
TTFont('abc', 'SakBunderan.ttf')
)
# creating the title by setting it's font
# and putting it on the canvas
pdf.setFont('abc', 36)
pdf.drawCentredString(300, 770, title)
第 5 步:
接下来对于副标题,我们做同样的事情,除了这次副标题的颜色是蓝色,这次我们使用标准字体,该字体与 reportlab 原生一起提供。
蟒蛇3
# creating the subtitle by setting it's font,
# colour and putting it on the canvas
pdf.setFillColorRGB(0, 0, 255)
pdf.setFont("Courier-Bold", 24)
pdf.drawCentredString(290, 720, subTitle)
第 6 步:
接下来,我们画一条线,然后在列表中输入我们之前定义的几行文本。第一行定义文本的起始 x 和 y 位置。接下来的两行设置文本的字体、字体大小和字体颜色。接下来的两行遍历列表中的每个元素并将其作为一行添加到文本中。最后一行将文本绘制到屏幕上。
蟒蛇3
# drawing a line
pdf.line(30, 710, 550, 710)
# creating a multiline text using
# textline and for loop
text = pdf.beginText(40, 680)
text.setFont("Courier", 18)
text.setFillColor(colors.red)
for line in textLines:
text.textLine(line)
pdf.drawText(text)
第 7 步:
最后,我们使用drawInlineImage函数在pdf上绘制一张图片,其中参数是图片的路径和图片的x和y坐标。在这种情况下,图像与py文件在同一目录中,因此根据相对路径,我们只需要写带有扩展名的文件名,如果在其他目录中,则需要相关的正确相对路径应该使用。
蟒蛇3
# drawing a image at the
# specified (x.y) position
pdf.drawInlineImage(image, 130, 400)
# saving the pdf
pdf.save()
下面是完整的程序:
蟒蛇3
# importing modules
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
from reportlab.lib import colors
# initializing variables with values
fileName = 'sample.pdf'
documentTitle = 'sample'
title = 'Technology'
subTitle = 'The largest thing now!!'
textLines = [
'Technology makes us aware of',
'the world around us.',
]
image = 'image.jpg'
# creating a pdf object
pdf = canvas.Canvas(fileName)
# setting the title of the document
pdf.setTitle(documentTitle)
# registering a external font in python
pdfmetrics.registerFont(
TTFont('abc', 'SakBunderan.ttf')
)
# creating the title by setting it's font
# and putting it on the canvas
pdf.setFont('abc', 36)
pdf.drawCentredString(300, 770, title)
# creating the subtitle by setting it's font,
# colour and putting it on the canvas
pdf.setFillColorRGB(0, 0, 255)
pdf.setFont("Courier-Bold", 24)
pdf.drawCentredString(290, 720, subTitle)
# drawing a line
pdf.line(30, 710, 550, 710)
# creating a multiline text using
# textline and for loop
text = pdf.beginText(40, 680)
text.setFont("Courier", 18)
text.setFillColor(colors.red)
for line in textLines:
text.textLine(line)
pdf.drawText(text)
# drawing a image at the
# specified (x.y) position
pdf.drawInlineImage(image, 130, 400)
# saving the pdf
pdf.save()
输出: