在 Django 站点中将 HTML 渲染为 PDF [英] Render HTML to PDF in Django site

查看:42
本文介绍了在 Django 站点中将 HTML 渲染为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 django 网站,我正在寻找一种简单的解决方案来将动态 html 页面转换为 pdf.

页面包含来自 Google 可视化 API(基于 JavaScript,但必须包含这些图表)的 HTML 和图表.

解决方案

尝试 Reportlab.

下载并照常使用 python setup.py install 安装

您还需要使用 easy_install 安装以下模块:xhtml2pdf、html5lib、pypdf.

这是一个用法示例:

首先定义这个函数:

将 cStringIO 导入为 StringIO从 xhtml2pdf 导入比萨从 django.template.loader 导入 get_template从 django.template 导入上下文从 django.http 导入 HttpResponse从 cgi 导入转义def render_to_pdf(template_src, context_dict):模板 = get_template(template_src)上下文 = 上下文(context_dict)html = template.render(上下文)结果 = 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',{'页面大小':'A4','mylist':结果,})

模板:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><头><title>我的标题</title><style type="text/css">@页 {大小:{{ pagesize }};边距:1cm;@frame页脚{-pdf-frame-content:页脚内容;底部:0cm;左边距:9cm;右边距:9cm;高度:1cm;}}</风格><身体><div>{% 用于 mylist 中的项目 %}呈现我的内容{% 结束为 %}

<div id="footerContent">{%block page_foot%}页<pdf:pagenumber>{%endblock%}

希望有帮助.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆