Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place image over PDF

Tags:

python

pdf

How can I place an image over an existing PDF file at an specific coordinate location. The pdf represents a drawing sheet with one page. The image will be scaled. I'm checking ReportLab but can't find the answer. Thanks.

like image 612
Eric Acevedo Avatar asked May 27 '10 22:05

Eric Acevedo


People also ask

How do I overlay an image in PDF?

Place an image or object into a PDFOpen the PDF in Acrobat, and then choose Tools > Edit PDF > Add Image . In the Open dialog box, locate the image file you want to place. Select the image file, and click Open. Click where you want to place the image, or click-drag to size the image as you place it.

How do I insert a picture into a PDF file without editing it?

Method 2: Drag and Drop the Image into the PDF Save the image file to your desktop. Open the PDFelement window and resize it, so you can view your desktop. Drag the image to where you want to insert it in the PDF. Choose the "Add as an Image" option and the picture will be inserted into the PDF.

How do I embed an object in a PDF?

To attach a file, go to Insert > Attach File in PDF. To embed a file, go to Insert > Embed File in PDF. Browse to and select the file that you want to insert, and click Select on the Select File dialog.


2 Answers

Its been 5 years, I think these answers need some TLC. Here is a complete solution.

The following is tested with Python 2.7

Install dependencies

pip install reportlab  pip install pypdf2 

Do the magic

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) 

References:

New home of pypdf: http://mstamy2.github.io/PyPDF2/

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

Reportlab complete user guide: https://www.reportlab.com/docs/reportlab-userguide.pdf

like image 102
Dr Manhattan Avatar answered Oct 18 '22 19:10

Dr Manhattan


http://pybrary.net/pyPdf/:

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() 

I think it's like watermark, see the manual for better idea

like image 21
Mohammad Efazati Avatar answered Oct 18 '22 19:10

Mohammad Efazati