gpt4 book ai didi

python - 将图像放在 PDF 上

转载 作者:IT老高 更新时间:2023-10-28 22:10:49 24 4
gpt4 key购买 nike

如何将图像放置在特定坐标位置的现有 PDF 文件上。 pdf 代表一页的图纸。图像将被缩放。我正在检查 ReportLab,但找不到答案。谢谢。

最佳答案

已经 5 年了,我认为这些答案需要一些 TLC。这是一个完整的解决方案。

以下是用 Python 2.7 测试的

安装依赖

pip install reportlab 
pip install pypdf2

施展魔法

from reportlab.pdfgen import canvas
from PyPDF2 import PdfFileWriter, PdfFileReader

# Create the watermark from an image
c = canvas.Canvas('watermark.pdf')

# Draw the image at x, y. I positioned the x,y to be where i like here
c.drawImage('test.png', 15, 720)

# Add some custom text for good measure
c.drawString(15, 720,"Hello World")
c.save()

# Get the watermark file you just created
watermark = PdfFileReader(open("watermark.pdf", "rb"))

# Get our files ready
output_file = PdfFileWriter()
input_file = PdfFileReader(open("test2.pdf", "rb"))

# Number of pages in input document
page_count = input_file.getNumPages()

# Go through all the input file pages to add a watermark to them
for page_number in range(page_count):
print "Watermarking page {} of {}".format(page_number, page_count)
# merge the watermark with the page
input_page = input_file.getPage(page_number)
input_page.mergePage(watermark.getPage(0))
# add page from input file to output document
output_file.addPage(input_page)

# finally, write "output" to document-output.pdf
with open("document-output.pdf", "wb") as outputStream:
output_file.write(outputStream)

引用资料:

pypdf的新家: http://mstamy2.github.io/PyPDF2/

Reportlab 文档: http://www.reportlab.com/apis/reportlab/2.4/pdfgen.html

Reportlab 完整用户指南: https://www.reportlab.com/docs/reportlab-userguide.pdf

关于python - 将图像放在 PDF 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2925484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com