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
It's not your fault. This happens because pdf kit assumes each element in the list as a file path instead of file descriptor.
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.