2010-02-01 14 views
30

Bir django görünümünü/şablonunu PDF'ye dönüştürmek için web üzerinden standart bir örnek (http://www.20seven.org/journal/2008/11/pdf-generation-with-pisa-in-django.html) kullanıyorum.django - pisa: resimleri PDF çıktısına ekleme

Şablonda PDF'de görüntülenecek şekilde şablonda (URL'de veya sunucuda başvuruda bulunmak) "kolay" bir yol var mı?

+2

Link dead - işte bu: http://www.microsoft.com/windows/2008/11/11/pdf-generation-with-pisa-in-django/ – Gady

cevap

32

Görüntüleri çalıştım. kod aşağıdaki gibidir:

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 

Bu

Hep IText/ISharp ile sonradan görüntüleri ekleyebilir http://groups.google.com/group/xhtml2pdf/browse_thread/thread/4cf4e5e0f4c99f55

+0

Django olmayan kullanıcılar için fyi, "MEDIA_ROOT" dönüştürdüğünüz html belgesinin fiziksel olarak bulunduğu dizini ve 'MEDIA_URL' bu konumun * http-dostu * sürümüdür (örneğin yerel bir html dosyası için: 'MEDIA_URL = 'dosya: ///' + MEDIA_ROOT') . Aksi halde bu genel bir çözümdür! – ecoe

0

gelen liberal alındı.

2

Google'da bulabildiğim her çözümü denemesine rağmen, resimlerin görünmesini sağlayamadım.

from tempfile import mkstemp 

    # write html to a temporary file 
    # can used NamedTemporaryFile if using python 2.6+ 
    fid, fname = mkstemp(dir='/tmp') 
    f = open(fname, 'w+b') 
    f.write(html) 
    f.close() 


    # now create pdf from the html 
    cmd = 'xhtml2pdf "%s"' % fname 
    os.system(cmd) 
    os.unlink(fname) 

    # get the content of the pdf 
    filename = fname+'.pdf' 
    pdf = open(filename, 'r') 
    content = pdf.read() 

    pdf.close() 
    os.unlink(pdf.name) 

    # return content 
    response = HttpResponse(content, mimetype='application/pdf') 
    response['Content-Disposition'] = 'attachment; filename=draft.pdf' 

görüntüleri url veya tam yol adını, mesela ya vardı burada çalıştı: pisa komut satırı sürümü Tamam görüntüler Ama bu geçiştirmek benim için çalıştı.

<img src="/home/django/project/site_media/css/output/images/logo.jpg" /> 

<img src="http://www.mysite.com/css/output/images/logo.jpg" /> 
2
def render_to_pdf(template_src, context_dict): 

    template = get_template(template_src) 
    context = Context(context_dict) 
    html = template.render(context) 
    result = StringIO.StringIO() 

    if page has an image.something: 
     pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources) 
    else no image.something : 
     pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")),result) 

    if not pdf.err: 
     return HttpResponse(result.getvalue(), mimetype='examination_report/pdf') 
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html)) 



def fetch_resources(uri, rel): 
    if os.sep == '\\': # deal with windows and wrong slashes 
     uri2 = os.sep.join(uri.split('/')) 
    else:# else, just add the untouched path. 
     uri2 = uri 

    path = '%s%s' % (settings.SITE_ROOT, uri2) 
    return path 
1

Yukarıdaki tüm kod benim için çalıştı olmadı. Sonunda get_full_path prosedürünü koyarak çalıştım. Bu nedenle, son kod şu şekilde görünüyor:

0

Görüntüyü base64'e de dönüştürebilirsiniz. Eğer resim bağlantılarıyla ilgili sorunlar asla Base64 dönüştürülüyor

http://www.motobit.com/util/base64-decoder-encoder.asp

.

+0

Evet, resimler base64'e dönüştürülebilir. Lütfen bunları pdf'ye nasıl ekleyeceğinize ekleyiniz. – ebob

İlgili konular