2012-02-13 123 views
0

我在導出html文件中的圖像時遇到了問題,因爲pdf文件存在類似的解決方案heredjango將圖像和文本導出爲html格式爲pdf

html正在服務器上正確渲染。我通過在網址上進行交叉檢查來驗證它。

但嘗試下載/渲染PDF **我得到的PDF,但它是空白的,也是它說的下載功能在views.py

這裏第三行的錯誤是我的嘗試:

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'}) 

該網址已妥善保管。

+0

對此有幫助嗎? http://stackoverflow.com/questions/4678723/django-pdf-question-with-pisa – jperelli 2012-02-13 18:38:19

回答

0

我後來發現,使用上述技術堆棧無法實現,因此我嘗試獲取模板圖像並使用PIL根據上下文對其進行修改。這工作。