在Django网站上呈现HTML到PDF

在Django网站上呈现HTML到PDF

本文介绍了在Django网站上呈现HTML到PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的django动力网站,我正在寻找一个简单的解决方案,将动态HTML页面转换为pdf。



页面包含来自Google可视化API的HTML和图表是基于javascript的,但是包括这些图是必须的)。

解决方案

尝试从。



下载并像往常一样安装python setup.py install

您还需要安装以下模块:xhtml2pdf,html5lib,pypdf with easy_install。



这是一个用法例如:



首先定义此功能:

  import cStringIO as StringIO 
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.template import上下文
from django.http import HttpResponse
从cgi import escape


def render_to_pdf(template_src,co ntext_dict)
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()

pdf = pisa.pisaDocument(StringIO.StringIO(html.encode(ISO-8859-1)),结果)
如果不是pdf.err:
返回HttpResponse(result.getvalue (),content_type ='application / pdf')
return HttpResponse('我们有一些错误< pre>%s< / pre>'%escape(html))

然后可以这样使用:

  def myview(request):
#读取数据或任何你需要的
返回render_to_pdf(
'mytemplate.html',
{
'pagesize':'A4',
'mylist':results,
}

模板

 <!DOCTYPE HTML PUBLIC -  / / W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\"> 
< html>
< head>
< title>我的标题< / title>
< style type =text / css>
@page {
size:{{pagesize}};
保证金:1厘米;
@frame footer {
-pdf-frame-content:footerContent;
bottom:0cm;
margin-left:9cm;
margin-right:9cm;
height:1cm;
}
}
< / style>
< / head>
< body>
< div>
{%for mylist%}
RENDER MY CONTENT
{%endfor%}
< / div>
< div id =footerContent>
{%block page_foot%}
页面< pdf:pagenumber>
{%endblock%}
< / div>
< / body>
< / html>

希望有帮助。


For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.

Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).

解决方案

Try the solution from Reportlab.

Download it and install it as usual with python setup.py install

You will also need to install the following modules: xhtml2pdf, html5lib, pypdf with easy_install.

Here is an usage example:

First define this function:

import cStringIO as StringIO
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from cgi import escape


def render_to_pdf(template_src, context_dict):
    template = get_template(template_src)
    context = Context(context_dict)
    html  = template.render(context)
    result = StringIO.StringIO()

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

Then you can use it like this:

def myview(request):
    #Retrieve data or whatever you need
    return render_to_pdf(
            'mytemplate.html',
            {
                'pagesize':'A4',
                'mylist': results,
            }
        )

The template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>My Title</title>
        <style type="text/css">
            @page {
                size: {{ pagesize }};
                margin: 1cm;
                @frame footer {
                    -pdf-frame-content: footerContent;
                    bottom: 0cm;
                    margin-left: 9cm;
                    margin-right: 9cm;
                    height: 1cm;
                }
            }
        </style>
    </head>
    <body>
        <div>
            {% for item in mylist %}
                RENDER MY CONTENT
            {% endfor %}
        </div>
        <div id="footerContent">
            {%block page_foot%}
                Page <pdf:pagenumber>
            {%endblock%}
        </div>
    </body>
</html>

Hope it helps.

这篇关于在Django网站上呈现HTML到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:24