django - 比萨:将图像添加到PDF输出 [英] django - pisa : adding images to PDF output

查看:184
本文介绍了django - 比萨:将图像添加到PDF输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用网络上的标准示例( http://www.20seven.org/journal/2008/11/pdf-generation-with-pisa-in-django.html )将django视图/模板转换为PDF。

I'm using a standard example from the web (http://www.20seven.org/journal/2008/11/pdf-generation-with-pisa-in-django.html) to convert a django view / template into a PDF.

是否有一种简单的方法来在模板中包含图像(来自网址或服务器上的引用),以便它们显示在PDF上? p>

Is there an "easy" way to include images (either from a url or a reference on the server) in the template so they will show on the PDF?

推荐答案

我的图像有效。代码如下:

I got the images working. the code is as follows:

from django.http import HttpResponse
from django.template.loader import render_to_string
from django.template import RequestContext
from django.conf import settings
import ho.pisa as pisa
import cStringIO as StringIO
import cgi
import os

def dm_monthly(request, year, month):
    html  = render_to_string('reports/dmmonthly.html', { 'pagesize' : 'A4', }, context_instance=RequestContext(request))
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources )
    if not pdf.err:
        return HttpResponse(result.getvalue(), mimetype='application/pdf')
    return HttpResponse('Gremlins ate your pdf! %s' % cgi.escape(html))

def fetch_resources(uri, rel):
    path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))

    return path

http://groups.google.com/group/xhtml2pdf/browse_thread/thread/4cf4e5e0f4c99f55

这篇关于django - 比萨:将图像添加到PDF输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆