首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图像置于PDF之上

将图像置于PDF之上
EN

Stack Overflow用户
提问于 2010-05-28 06:29:29
回答 7查看 42.3K关注 0票数 34

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

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2011-03-16 15:24:09

http://pybrary.net/pyPdf/

代码语言:javascript
复制
from pyPdf import PdfFileWriter, PdfFileReader

output = PdfFileWriter()
input1 = PdfFileReader(file("document1.pdf", "rb"))
watermark = PdfFileReader(file("watermark.pdf", "rb"))

input1.mergePage(watermark.getPage(0))

# finally, write "output" to document-output.pdf
outputStream = file("document-output.pdf", "wb")
output.write(input1)
outputStream.close()

我认为它类似于watermark,更好的想法请参阅手册

票数 26
EN

Stack Overflow用户

发布于 2015-07-30 01:16:13

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

以下代码使用Python 2.7进行了测试

安装依赖项

代码语言:javascript
复制
pip install reportlab 
pip install pypdf2

施展魔法

代码语言:javascript
复制
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/

报告实验室文档:http://www.reportlab.com/apis/reportlab/2.4/pdfgen.html

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

票数 50
EN

Stack Overflow用户

发布于 2012-05-26 21:17:55

我组合了ReportLab (http://www.reportlab.com/software/opensource/rl-toolkit/download/)和pyPDF (http://pybrary.net/pyPdf/)来直接插入图像,而不必预先生成PDF:

代码语言:javascript
复制
from pyPdf import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from StringIO import StringIO


# Using ReportLab to insert image into PDF
imgTemp = StringIO()
imgDoc = canvas.Canvas(imgTemp)

# Draw image on Canvas and save PDF in buffer
imgPath = "path/to/img.png"
imgDoc.drawImage(imgPath, 399, 760, 160, 160)    ## at (399,760) with size 160x160
imgDoc.save()

# Use PyPDF to merge the image-PDF into the template
page = PdfFileReader(file("document.pdf","rb")).getPage(0)
overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0)
page.mergePage(overlay)

#Save the result
output = PdfFileWriter()
output.addPage(page)
output.write(file("output.pdf","w"))
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2925484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档