简体   繁体   English

使用 FPDF 插入 PDF

[英]insert a PDF with FPDF

It is possible to insert an image in a pdf document with FPDF using this command:可以使用以下命令使用 FPDF 在 pdf 文档中插入图像:

fpdf.image(name, x = None, y = None, w = 0, h = 0, type = '', link = '')

My question is: is it possible to do the same but instead of inserting an image I want to insert another PDF ( corresponding to a graphic) as an image ( or as a new page if it is the only possibility)我的问题是:是否可以做同样的事情,而不是插入图像我想插入另一个 PDF(对应于图形)作为图像(或者作为新页面,如果这是唯一的可能性)

Thank you in advance先感谢您

I'll try to answer the best I can;我会尽力回答; please let me know if it is not clear.如果不清楚,请告诉我。

In order to add a PDF inside another PDF, you can use the template method.为了在另一个 PDF 中添加一个 PDF,您可以使用template方法。

From the docs :文档

  1. Create an instance of FPDF创建 FPDF 实例
from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)

  1. Insert the PDF file at the right position and size在右边插入PDF文件position和大小
pdf.template('the_pdf_to_insert.pdf', x = 10, y = 10, w = 80, h = 30, type = 'P')

Make sure that the orientation is set the right way.确保方向设置正确。 Use either P for portrait or L for landscape.使用P表示纵向或L表示横向。

  1. And the last step, save the file最后一步,保存文件
pdf.output('document_with_image.pdf')

Hope it helps:)希望能帮助到你:)

From what I know, one can convert the PDF file that you want to insert into an image format that FPDF supports.据我所知,可以将要插入的 PDF 文件转换为 FPDF 支持的图像格式。

pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 14)
Insert the PDF file at the correct position在正确的 position 处插入 PDF 文件
pdf.template('pdf_to_insert.pdf', x = 50, y = 50, w = 60, h = 40, type = 'P')
pdf.output('output_image.pdf')

You can convert the PDF file that you want to insert into an image format that FPDF supports (such as with the pillow library).您可以将要插入的 PDF 文件转换为 FPDF 支持的图像格式(例如使用 pillow 库)。

Once you have converted the PDF file to an image format, you can use the fpdf.image method as you did above to insert it into your PDF document.将 PDF 文件转换为图像格式后,您可以像上面那样使用 fpdf.image 方法将其插入到 PDF 文档中。 For example, to insert the image at the top-left corner of the page, you could do the following:例如,要在页面的左上角插入图像,您可以执行以下操作:

pdf = FPDF()
pdf.add_page()
pdf.image("my_image.jpg", x = 0, y = 0)
pdf.output("output.pdf")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM