2015-09-26 64 views
1

所以我有一個非常簡單的模板,我正在使用WeasyPrint進行測試。如果將模板呈現爲常規網頁,則呈現效果很好。如果我嘗試生成PDF,造型消失。只有當我刪除bootstrap引用時,PDF才能正常生成。有沒有人有任何想法,爲什麼樣式不會盡快引入引導CSS文件?我試過一些Bootstrap3和Bootstrap2文件。提供本地和CDN服務。WeasyPrint和Bootstrap

模板:

<!doctype html> 
<html lang="en"> 
{% load static from staticfiles %} 
{% block head %} 
    <head> 
    {% block css %} 
     <link rel="stylesheet" href="{% static "css/bootstrap.min.css" %}"> 
     <link rel="stylesheet" href="{% static "css/main.css" %}"> 
    {% endblock css %} 
    </head> 
{% endblock head %} 
<body> 
{% block content %} 
    <div id="content" class="container"> 
     <div id="logo" class="col-md-3"> 
      <img src="{% static "images/logo_small.png" %}"> 
     </div> 
     <div id="heading" class="col-md-6"> 
      <h1>Packing Slip</h1> 
     </div> 

     <div class="col-md-3"> 
      <h2>{{ packslip_id }}</h2> 
     </div> 
    </div> 
{% endblock %} 
</body> 
</html> 

筆者認爲:

class WeasyPDF(TemplateView): 
template_name = 'jinja2/Shipping/test_pdf.html' 


def get(self, request, *args, **kwargs): 
    packslip_id = kwargs.get('packslip_id') 
    context= {'packslip_id': packslip_id } 
    template_string = render_to_string(self.template_name, context) 
    html = HTML(string=template_string, base_url=request.build_absolute_uri()) 
    main_doc = html.render() 
    pdf = main_doc.write_pdf() 
    response = HttpResponse(pdf, content_type='application/pdf') 
    #Download as attachment 
    # response['Content-Disposition'] = 'attachment; filename=packslip-{0}.pdf'.format(packslip_id) 
    # Display in browser 
    response['Content-Disposition'] = 'filename=packslip-{0}.pdf'.format(packslip_id) 
    return response 

最後urls.py進入

url(r'^weasypdf/(?P<packslip_id>\d+)$', WeasyPDF.as_view(), name='weasypdf'), 
+0

你應該使用樣式表關鍵字參數與write_pdf方法,並通過你的樣式表,而不是存在。 – xtranophilist

回答

0

我相信你需要渲染CSS到HTML對象: html = HTML(string = template_string,base_url = request.build_absolute_uri(),stylesheet =「filename.cs S「)

一旦做到這一點,那麼Weasyprint會使其進入HTML對象爲PDF和HTML顯示