2012-02-13 22 views
0

htmlファイルの画像をpdf形式でエクスポートする際に問題が発生しましたが、同様の解決策が存在しますheredjangoがhtmlの画像とテキストをpdfとしてエクスポート

htmlがサーバー上で正しくレンダリングされています。私はそれをURLでチェックして確認しました。

が、ダウンロードしようとしたときに、私はPDFファイルを取得しますが空白になっており、また、それはここで

views.py

でのダウンロード機能の3行目のエラーは、私が試したものですと言い**/PDFをレンダリングします:

htmlファイル:

<html> 
<head> 
<link href="{{ STATIC_URL }}css/certificate.css" rel="stylesheet" 
    type="text/css" /> 
</head> 
<body> 
<div class="certificate_container"> 
    <div class="statictext"> 
     <p>{{ name }}</p> 
    </div> 
</div> 
</body> 
<html> 

CSSファイル:

body{margin:0px; padding:0px;} 
.certificate_container{ width:792px; height:612px; background:url("../images/certificate.gif") no-repeat;} 
.statictext{width:400px; margin:0px auto; padding-top:240px; height:30px; text-align:center; font:bold 14px Arial, Helvetica, sans-serif; color:#333;} 

views.py:

#relevant imports 
from reportlab.pdfgen import canvas 
import xhtml2pdf.pisa as pisa 
import cStringIO as StringIO 

def download(request): 
    html = render_to_string("certificate.html", { 'pagesize' : 'A4', }, context_instance=RequestContext(request)) 
    result = StringIO.StringIO() 
    pdf = pisa.pisaDocument(StringIO.StringIO(), 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.STATIC_ROOT, uri.replace(settings.STATIC_URL, "")) 
    return path 


def home(request): 
    return render(request, 'certificate.html', {'name':'user1'}) 

URLは正しく処理されています。

+0

これは役に立ちますか? http://stackoverflow.com/questions/4678723/django-pdf-question-with-pisa – jperelli

答えて

0

私は後で見つけました、これは上記の技術スタックを使用して達成できなかったので、私はテンプレートイメージを取得しようとし、コンテキストに基づいてそれを変更するためにPILを使用しました。それはうまくいった。

関連する問題