修复weasyprint生成的PDF中的引导表

时间:2019-10-03 15:53:25

标签: django twitter-bootstrap weasyprint

我正在尝试在Django网站中使用Weasyprint创建PDF表。相同的表格显示在html页面中,并带有Weasyprint。当我在html版本中使用引导程序颜色时,它可以正确显示,但是在Weasyprint版本中这些颜色会丢失。这是Weasyprint版本唯一缺少的功能,否则可以正常工作。

views.py

@login_required
def scale_answer_pdf(request, scale_id):
    scale = get_object_or_404(UserScale, pk=scale_id)
    if scale.user == request.user or request.user.is_superuser:
        if scale.scale_meta.title == "Now":
            choices = NowLegend
            class ScaleForm(forms.ModelForm):
                class Meta:
                    model = NowModel
                    fields = labellist(NowLabels)
                    labels = NowLabels
        if scale.scale_meta.title == "Child":
            choices = NowLegend
            class ScaleForm(forms.ModelForm):
                class Meta:
                    model = ChildModel
                    fields = labellist(ChildLabels)
                    labels = ChildLabels
        form = ScaleForm(instance=scale)
        **html = render_to_string('diagnosis/scale_answer_pdf.html', 
{'form': form, 'scale':scale, 'choices':choices})
        response = HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'filename="{} - {} - 
{}.pdf"'.format(scale.scale_meta.title, scale.user.username, 
scale.created.strftime('%Y-%m-%d'))
       weasyprint.HTML(string=html, 
base_url=request.build_absolute_uri()).write_pdf(response, 
presentational_hints=True, stylesheets= 
[weasyprint.CSS(settings.STATIC_ROOT + '/css/sb-admin-2.min.css'), 
weasyprint.CSS(settings.STATIC_ROOT +'/css/all.min.css')])
        return response**
    else:
        raise Http404
PDF的

html-boostrap CSS起作用,并且该表也显示出来。但是桌子上的颜色没有。我突出显示了不显示颜色的桌子零件

    {% extends "base_pdf.html" %}
{% load i18n %}
{% load staticfiles %}
{% load get_item %}
{% block content %}
<div class="container-fluid">
     <div class="row">
<div class="col-sm-6 mx-auto" >
    <p class="text-center"><img height="120" width="120" src="{{scale.scale_meta.icon.url}}" alt="Test icon" class="img-fluid"></p>
    </br>
    <h1 class="text-center">{{scale.meta.title}}</h1>
        {% with scale.created as created %}
        {% with scale.user.username as username %}
    <h4 class="text-center">{% blocktrans %}Created the {{created}} by {{username}}{% endblocktrans %}</h4>
    </br>
   <p class="text-center">{% trans "A test by" %}<img src="{% static 'icon.png' %}" height="30" width="30" style="margin-right:1px;"/><a href="Atrapamente.com">Atrapamente</a></p>

  <table class="table table-striped">
       <tbody>
        **<tr class="table-info">**
    <td>{% trans "Question" %}<td>
    <td>{% trans "Answer type" %}<td>
    <td>{% trans "Answer" %}<td>
        </tr>
    {% for field in form %}
    {% with name=field.name %}
    {% if choices|get_item:name == "0 to 6" %}
        {% if field.value > "4" %}
        **<tr class="table-danger">**
        {% endif %}
         {% if field.value > "2" and field.value < "5" %}
        **<tr class="table-warning">**
        {% endif %}
        {% if field.value <= "2" %}
        **<tr class="table-success">**
        {% endif %}
    {% elif choices|get_item:name == "0 to 10" %}
        {% if field.value <= "4" %}
        **<tr class="table-danger">**
        {% endif %}
         {% if field.value > "4" and field.value < "7" %}
        **<tr class="table-warning">**
        {% endif %}
        {% if field.value >= "7" %}
        **<tr class="table-success">**
        {% endif %}
    {% endif %}
    <td>{{field.label}}<td>
    <td>{{choices|get_item:name}}<td>
    <td >{{field.value}}<td>
        </tr>
    {% endwith %}
    {% endfor %}
    </tbody>
</table>
</div>
     </div>
</div>
 <br/>
 <br/>
{% endwith %}
{% endwith %}
{% endblock %}

1 个答案:

答案 0 :(得分:0)

BootStrap和文件。CSS在weasyprint中无法正常工作,

最好在HTML文件中使用CSS <style>

喜欢:

<style> /* use CSS into style */
      @page {
        size: A4; /* Change from the default size of A4 */
        margin: 3.5mm; /* Set margin on each page */
      }
</style>

再见。