[Image-SIG] pdf to image
Carl Karsten
carl at personnelware.com
Sat Jan 12 17:36:06 CET 2008
For the PyCon registration web page, we would like to display a preview of what
the persons badge will look like based on what they have entered. (full name,
nick name, extra lines in the fonts that will be used - helps make sure things
will fit, etc.)
The badge gets created as a pdf=mkpdf(data set) - we can just give the person
the pdf, but we are trying to put it on the web page with <img src..> so we need
to convert the pdf to an image. I can't find a clean way of doing it. I
found no ghostscript bindings, and the 2 imagemagick bindings either screwed up
stdio or segfaulted. The best I could do is below, and it is causing this to
show up in the error log:
close failed: [Errno 10] No child processes
and stack dumps:
> subprocess.call(gs_command)
>
> File "/usr/local/lib/python2.5/subprocess.py", line 443, in call
> return Popen(*popenargs, **kwargs).wait()
>
> File "/usr/local/lib/python2.5/subprocess.py", line 1110, in wait
> pid, sts = os.waitpid(self.pid, 0)
>
> OSError: [Errno 10] No child processes
here is the current code:
def sample(pdf):
tempDir = tempfile.mkdtemp( prefix = "badgePreview_" )
pdfname = path.join( tempDir, 'badge.pdf' )
imgname = path.join( tempDir, 'badge.png' )
open(pdfname,'wb').write(pdf)
# convert pdf to some image format (uses ext to determine type)
# system('convert %s %s' % (pdfname, imgname))
# gs -sDEVICE=png16m -sOutputFile=x.png -dBATCH -dNOPAUSE badge.pdf
gs_command = ['gs',
'-q',
'-sDEVICE=png16m',
'-dBATCH', '-dNOPAUSE',
'-sOutputFile=%s'%imgname,
pdfname ]
subprocess.call(gs_command)
# crop the badge (pdf may be 8x11 page)
# and write to a string that can be reutnred
buffer = StringIO()
Image.open(imgname).crop([0,0,300,235]).save(buffer, 'png')
img = buffer.getvalue()
# clean up
buffer.close()
remove(pdfname)
remove(imgname)
rmdir(tempDir)
return img
So.... anyone have some nifty code?
Carl K
More information about the Image-SIG
mailing list