I'm trying to insert a barcode image into Reportlab. I know there are a lot of questions asked on this, but all of them assume that you already have the image file in the directory or on the filesystem.
Due to the fact that Reportlab has issues with EAN13 barcodes, I decided to use another package called pyBarcode to generate the image for me.
Initially I saved the image in a StringIO instance and passed it directly to reportlab.platypus.flowables.Image
but that didn't seem to work. Then I read the documentation:
Formats supported by PIL/Java 1.4 (the Python/Java Imaging Library) are supported.
Does this mean that if I pass a PIL image, this should work? I got an exception when I tried the following:
>>> import PIL
>>> from reportlab.platypus.flowables import Image
>>> fp = StringIO(the_barcode.getvalue())
>>> barcode_image = PIL.Image.open(fp)
>>> doc = SimpleDocTemplate('barcode.pdf')
>>> story = [Image(barcode_image)]
>>> Traceback (most recent call last):
File "create.py", line 57, in <module>
main()
File "create.py", line 24, in main
save_pdf(fp, STYLE, ART, COLOR, SIZE)
File "create.py", line 28, in save_pdf
fp = StringIO(fp.getvalue())
File "/home/mark/.virtualenvs/barcode/local/lib/python2.7/site-packages/reportlab-2.6-py2.7-linux-i686.egg/reportlab/platypus/flowables.py", line 402, in __init__
if not fp and os.path.splitext(filename)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']:
File "/home/mark/.virtualenvs/barcode/lib/python2.7/posixpath.py", line 95, in splitext
return genericpath._splitext(p, sep, altsep, extsep)
File "/home/mark/.virtualenvs/barcode/lib/python2.7/genericpath.py", line 91, in _splitext
sepIndex = p.rfind(sep)
File "/home/mark/.virtualenvs/barcode/local/lib/python2.7/site-packages/PIL/Image.py", line 512, in __getattr__
raise AttributeError(name)
AttributeError: rfind
Somehow PIL Image doesn't seem to work either. What should I pass as the first argument to Reportlab's Image function if I don't have the filename of the image (because my images are created in memory)?
I had no luck with the proposed methods.
Checking the code in pdfdoc.py shows, that the AttributError results from treating the StringIO as a filename:
if source is None:
pass # use the canned one.
elif hasattr(source,'jpeg_fh'):
self.loadImageFromSRC(source) #it is already a PIL Image
else:
# it is a filename
Further checking the source, shows that jpeg_fh is an attribute of class ImageReader in reportlab.lib.utils. ImageReader accepts both StringIO and PIL images.
So wrapping the StringIO in an ImageReader solved the problem for me:
import PIL
from reportlab.lib.utils import ImageReader
io_img = StringIO(data)
pil_img = PIL.Image.open(StringIO(data))
reportlab_io_img = ImageReader(io_img)
reportlab_pil_img = ImageReader(pil_img)
canvas.drawImage(reportlab_io_img, ...)
canvas.drawImage(reportlab_pil_img, ...)