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

查看:25
本文介绍了django - pisa:将图像添加到 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.

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

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

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