2010-02-01 11 views
30

में छवियों को जोड़ना मैं एक डीजेंगो व्यू/टेम्पलेट को पीडीएफ में परिवर्तित करने के लिए वेब (http://www.20seven.org/journal/2008/11/pdf-generation-with-pisa-in-django.html) से मानक उदाहरण का उपयोग कर रहा हूं।django - pisa: पीडीएफ आउटपुट

क्या टेम्पलेट में छवियों (या तो यूआरएल या सर्वर पर संदर्भ से) शामिल करने का एक "आसान" तरीका है ताकि वे पीडीएफ पर दिखाए जाएंगे?

+2

लिंक मृत - यह है: http://20seven.org/journal/2008/11/11/pdf- जनरेशन-with-pisa-in-django/ – Gady

उत्तर

32

मुझे छवियां काम कर रही हैं। कोड इस प्रकार है:

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

+0

गैर-Django उपयोगकर्ताओं के लिए fyi, 'MEDIA_ROOT' है निर्देशिका जहां आप जिस HTML दस्तावेज़ को परिवर्तित कर रहे हैं वह भौतिक रूप से स्थित है और 'MEDIA_URL' उस स्थान का * http- अनुकूल * संस्करण है (उदाहरण के लिए स्थानीय HTML फ़ाइल के लिए: 'MEDIA_URL =' फ़ाइल: /// '+ MEDIA_ROOT') । अन्यथा यह एक ध्वनि सामान्य समाधान है! – ecoe

0

उदारतापूर्वक लिया गया था तुम हमेशा iText/ISharp साथ बाद में छवियों को जोड़ सकता है।

2

मुझे Google पर मिलने वाले हर समाधान की कोशिश करने के बावजूद छवियों को प्रदर्शित नहीं किया जा सका। लेकिन इस फ़ज मेरे लिए काम किया पीसा की कमांड लाइन संस्करण के रूप में छवियों को प्रदर्शित करता ठीक:

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' 

यह काम किया, जहां छवियों कोई URL या पूर्ण पथ नाम, उदाहरण के लिए किया था।

<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

उपरोक्त सभी कोड मेरे लिए काम किया नहीं था। अंत में मुझे get_full_path प्रक्रिया डालने से काम मिल गया। तो अंतिम कोड इस

def render_to_pdf(template_src, context_dict): 
    now = datetime.now() 
    filename = now.strftime('%Y-%m-%d') + '.pdf' 
    template = get_template(template_src) 
    context = Context(context_dict) 
    html = template.render(context) 
    result = StringIO.StringIO() 

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")),result, path=path) 

    if not pdf.err: 
     response = HttpResponse(result.getvalue(), mimetype='application/pdf') 
     response['Content-Disposition'] = 'attachment; filename="'+filename+'"' 
     return response 
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html)) 

def get_full_path_x(request): 
    full_path = ('http', ('', 's')[request.is_secure()], '://', 
    request.META['HTTP_HOST'], request.path) 
    return ''.join(full_path) 
0

जैसा कि आप छवि को बेस 64 में भी परिवर्तित कर सकते हैं।

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

base64 करने के लिए आप छवि लिंक के साथ समस्या है कभी नहीं होगा परिवर्तित।

+0

हां, छवियों को बेस 64 में परिवर्तित किया जा सकता है। पीडीएफ में उन्हें कैसे रखा जाए, इस पर एक उदाहरण जोड़ें। – ebob

संबंधित मुद्दे