2

Goal: - Use django templateing language. - Render the template in memory (no disk writes). - Push rendered content to StringIO instance. - Use instance in python-pdfkit.

Issue: I keep getting TypeError: coercing to Unicode: need string or buffer, instance found when trying to pass more than one file in the list.

The below code works without the [] and just one StringIO instance.

from django.template import loader, Context from django import template import StringIO

STATIC_URL = "https://d1i1yohwujljp9.cloudfront.net/static/"
t = loader.get_template('pdf_coverpage.html')
c = template.Context( {'STATIC_URL': STATIC_URL })
output = StringIO.StringIO()
output.write(t.render(c))
output1 = StringIO.StringIO()
output1.write(t.render(c))

pdfkit.from_file([ output, output1 ] , 'out.pdf' )

Traceback.

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\pdfkit\api.py", line 44, in from_file
    configuration=configuration)
  File "C:\Python27\lib\site-packages\pdfkit\pdfkit.py", line 37, in __init__
    self.source = Source(url_or_file, type_)
  File "C:\Python27\lib\site-packages\pdfkit\source.py", line 12, in __init__
    self.checkFiles()
  File "C:\Python27\lib\site-packages\pdfkit\source.py", line 28, in checkFiles
    if not os.path.exists(path):
  File "C:\Python27\lib\genericpath.py", line 18, in exists
    os.stat(path)
TypeError: coercing to Unicode: need string or buffer, instance found
cph
  • 458
  • 2
  • 6
  • 24

2 Answers2

1

It's not your fault. This happens because pdf kit assumes each element in the list as a file path instead of file descriptor.

here is the relevant code.

I had a similar situation of HTML spread across multiple templates. I put them all in one string and pass the StringIO to pdfkit. I used CSS to manage page breaks and other wkhtmltopdf formatting options.

Hope that helps.

nicolom
  • 360
  • 1
  • 3
  • 11
Saikiran Yerram
  • 2,994
  • 3
  • 17
  • 20
0

Using StringIO doesn't appear to be a recommended approach in the documentation.

I just tried this and it worked fine. Is there a reason you don't want to do it this way?

pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')

https://pypi.python.org/pypi/pdfkit

Charlie
  • 2,004
  • 6
  • 20
  • 40
  • I have workers that simply run across the redis queue and didn't want the overhead of hitting our LIVE services with extra thousands of requests. – cph Mar 12 '15 at 18:20