[reportlab-users] PNG to PDF

Tim Roberts timr at probo.com
Tue Apr 3 16:35:51 EDT 2007


Grant Kelly wrote:

>

> I need to convert a PNG to PDF. The PNG is being dynamically created

> with PIL, but I would like a PDF output option as well. I'm looking at

> using pythonpoint with a fixedimage on a single slide, but was

> wondering if there was a better way to go about this.


This is an interesting request. There's no way to *convert* a PNG to
PDF. You can *embed* a bitmap into a PDF, and ReportLab can do that in
a half-dozen lines of code, but it's not "converted" in any way.

from PIL import Image
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import LETTER
from reportlab.lib.utils import ImageReader

hello = Image.open('/tmp/hello.png')
ir = ImageReader(hello)

canv = canvas.Canvas( 'try.pdf', pagesize=LETTER )
canv.setPageCompression( 0 )
canv.drawImage( ImageReader(hello), 200, 200 )
canv.showPage()
canv.save()

--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the reportlab-users mailing list