Adding images to PDFs is not much different than adding text. Here is how to add an image as a flowable:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python

import os
import urllib2
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Image

filename = './python-logo.png'

def get_python_image():
    """ Get a python logo image for this example """
    if not os.path.exists(filename):
        response = urllib2.urlopen(
            'http://www.python.org/community/logos/python-logo.png')
        f = open(filename, 'w')
        f.write(response.read())
        f.close()

get_python_image()

doc = SimpleDocTemplate("image.pdf", pagesize=letter)
parts = []
parts.append(Image(filename))
doc.build(parts)

Ignore the get_python_image function. It is in there only to make this example easily runnable. As you see, I import the Image Flowable from platypus. Image takes a minimum of one argument, which is the path to the image to use. You can build it into a SimpleDocTemplate just like a Paragraph or a Spacer.

Writing images to a canvas is easy, but has one gotcha.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python

import os
import urllib2
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas

filename = './python-logo.png'

def get_python_image():
    """ Get a python logo image for this example """
    if not os.path.exists(filename):
        response = urllib2.urlopen(
            'http://www.python.org/community/logos/python-logo.png')
        f = open(filename, 'w')
        f.write(response.read())
        f.close()

get_python_image()

c = canvas.Canvas('imageabs.pdf', pagesize=letter)
width, height = letter
c.drawImage(filename, inch, height - 2 * inch) # Who needs consistency?
c.showPage()
c.save()

We use the drawImage method of our Canvas instance. Here is the gotcha. Unlike drawString and its siblings, drawImage takes its content, the path to the image, first and its coordinates second. This is poor design by the ReportLab developers. It is still easy to add the image though. Just remember that if you get an obtuse error like this:

AttributeError: 'float' object has no attribute 'jpeg_fh'

Or this:

AttributeError: 'int' object has no attribute 'jpeg_fh'

Then the arguments are out of order.

Posted by Tyler Lesmann on January 28, 2009 at 6:07
Tagged as: python reportlab
Comments
#1 Marcelo wrote this 4 years, 7 months ago

Hello, how i put image in existis PDF, i have a pdf file with text but me nescessity insert image in header.

#2 Tyler Lesmann wrote this 4 years, 7 months ago

In reportlab, create a new PDF with your image in the position you want it. Then, use PyPDF's mergePage to put the image on a page of an existing PDF.

http://stackoverflow.com/questions/1180115/add-text-to-existing-pdf-using-python
About adding text, but still applicable.

http://pybrary.net/pyPdf/
Example features usage of mergePage.

#3 lorenzi wrote this 4 years, 7 months ago

Tanks man for help,
I have txt files and need convert PDF files and ADD image im header.
i try use PyPDF but print error, I using this script http://code.activestate.com/recipes/189858/ to convert TXT to PDF and I need insert a image in header of all pages.
I try input your code in this script but not sucess.

Post a comment