繁体   English   中英

如何将多个StringIO传递给python-pdfkit?

[英]How do I pass multiple StringIO into python-pdfkit?

目标:-使用Django模板语言。 -在内存中渲染模板(不写磁盘)。 -将呈现的内容推送到StringIO实例。 -在python-pdfkit中使用实例。

问题:我不断收到TypeError: coercing to Unicode: need string or buffer, instance found当尝试传递列表中的多个文件时TypeError: coercing to Unicode: need string or buffer, instance found

下面的代码无需[]和一个StringIO实例即可工作。

从django.template导入加载程序,从django导入模板的上下文导入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 (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

这不是你的错。 发生这种情况是因为pdf kit假定列表中的每个元素都是文件路径而不是文件描述符。

这是相关的代码

我有类似的情况,HTML分布在多个模板中。 我将它们全部放在一个字符串中,并将StringIO传递给pdfkit。 我使用CSS管理分页符和其他wkhtmltopdf格式设置选项。

希望能有所帮助。

在文档中似乎不建议使用StringIO。

我只是尝试了一下,效果很好。 您是否有不想这样做的理由?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM