简体   繁体   English

xhtml2pdf和django,文档大小各不相同

[英]xhtml2pdf and django, varying document size

I'm trying to build a view which would render itself to PDF. 我正在尝试构建一个将自己呈现为PDF的视图。 Each time I accessed the view, I had some random issues with the structure of rendered document / table. 每次我访问视图时,我都会对渲染文档/表的结构有一些随机问题。 Tracking the error, I've came down to rendering completely static html code, and found out, that - each request, the resulting document size is different . 跟踪错误,我发现完全静态的HTML代码,并发现, 每个请求,结果文档大小是不同的

    template = get_template(self.get_report_template_name())
    html = template.render(Context({}))
    strobj = StringIO.StringIO()
    pisa.CreatePDF(html.encode("UTF-8"), strobj, encoding='UTF-8')
    return HttpResponse('len: %d' % strobj.len);

as you can see, each time the very same template is rendered, with empty context, to make sure nothing changes. 正如您所看到的,每次渲染完全相同的模板时,使用空上下文,以确保没有任何更改。 anyway, the template doesn't use django templating language at all 无论如何,模板根本不使用django模板语言

the above code returns a bit different result each time I refresh the page 每次刷新页面时,上面的代码返回一个不同的结果

len: 2573, len: 2595 len: 2234, len: 2601, len: 2244, len: 2632, len:2573,len:2595 len:2234,len:2601,len:2244,len:2632,

etc ... (some of the values are repeated multiple time). 等...(某些值重复多次)。

when saved & displayed these documents - they contains "broken" page structure, like incorrectly displayed table cell or something. 当保存并显示这些文档时 - 它们包含“破损”的页面结构,如错误显示的表格单元格或其他内容。 Only one of these looks correct. 其中只有一个看起来是正确的。

Any suggestions where to find the problem? 有什么建议在哪里找到问题?

I've been through the same problem. 我遇到了同样的问题。 Tried to fix it for hours... The only solution I've found was to downgrade xhtml2pdf to version 0.0.4. 试图修复它几个小时...我发现的唯一解决方案是将xhtml2pdf降级到版本0.0.4。

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

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