generate pdf with Django

0

I am using a library called weasyPrint to generate a PDF with an HTML template and a CSS.

My problem is that it does not load {% load static %} and I can not load the images that carry that pdf. It also does not load {% load bootstrap %} and {% extends %} .

When I generate the template and pass it as an HTML template and not as a PDF, it loads all the properties I've said before and I can see it well.

My template

{% extends "frontend/base_frontend.html" %}
{% load static %}
{% block content %}
<div class="contenedor-email">
    <div class='header' style="padding: 25px 0px; text-align: center;">
        <img src="{% static '/img/chikka_logo.png' %}" alt="Chikka" class="logo-email" style="margin-bottom: 25px;" />
        <div class="separacion" style="background: rgba(5,171,168,1);background: -moz-linear-gradient(left, rgba(5,171,168,1) 0%, rgba(156,229,222,1) 26%, rgba(217,238,219,1) 42%, rgba(251,203,183,1) 59%, rgba(249,168,155,1) 75%, rgba(237,100,109,1) 100%);background: -webkit-gradient(left top, right top, color-stop(0%, rgba(5,171,168,1)), color-stop(26%, rgba(156,229,222,1)), color-stop(42%, rgba(217,238,219,1)), color-stop(59%, rgba(251,203,183,1)), color-stop(75%, rgba(249,168,155,1)), color-stop(100%, rgba(237,100,109,1)));background: -webkit-linear-gradient(left, rgba(5,171,168,1) 0%, rgba(156,229,222,1) 26%, rgba(217,238,219,1) 42%, rgba(251,203,183,1) 59%, rgba(249,168,155,1) 75%, rgba(237,100,109,1) 100%);background: -o-linear-gradient(left, rgba(5,171,168,1) 0%, rgba(156,229,222,1) 26%, rgba(217,238,219,1) 42%, rgba(251,203,183,1) 59%, rgba(249,168,155,1) 75%, rgba(237,100,109,1) 100%);background: -ms-linear-gradient(left, rgba(5,171,168,1) 0%, rgba(156,229,222,1) 26%, rgba(217,238,219,1) 42%, rgba(251,203,183,1) 59%, rgba(249,168,155,1) 75%, rgba(237,100,109,1) 100%);background: linear-gradient(to right, rgba(5,171,168,1) 0%, rgba(156,229,222,1) 26%, rgba(217,238,219,1) 42%, rgba(251,203,183,1) 59%, rgba(249,168,155,1) 75%, rgba(237,100,109,1) 100%);filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#05aba8', endColorstr='#ed646d', GradientType=1 );height: 4px;width: 100%;position: absolute;right: 0;"></div>
    </div>
    <div class="cuerpo" style="padding:50px 0px; text-align:center;">
        <div class="titulo" style="font-size:22px; margin-bottom:15px;">COMPROBANTE DE COMPRA</div>
        <div class="row">
            <div class="col-md-6">CÓDIGO DE PEDIDO</div>
            <div class="col-md-6">{{pedido.id}}</div>
        </div>
        <div>
            <div class="col-md-6">CÓDIGO DE TRANSACCIÓN</div>
            <div class="col-md-6">{{pedido.id_transaccion}}</div>
        </div>
        <div class="row">
            <div class="col-md-6">FECHA DE CREACIÓN</div>
            <div class="col-md-6">{{pedido.f_ped}}</div>
        </div>
        <div class="row">
            <div class="col-md-6">PRECIO PRODUCTO</div>
            <div class="col-md-6">$ {{pedido.cp_total}}</div>
        </div>
        <div class="row">
            <div class="col-md-6">NOMBRE COMPRADOR</div>
            <div class="col-md-6">{{pedido.cp_nombre_completo}}</div>
        </div>
        <div class="row">
            <div class="col-md-6">E-MAIL COMPRADOR</div>
            <div class="col-md-6">{{pedido.perfil.usuario.email}}</div>
        </div>
    </div>
</div>
{% endblock %}

My Function

def descargar_comprobante_compra(request, pedido):
    pedido = Pedido.objects.get(id=pedido)
    html_template = get_template('intranet/pdf/comprobante-compra.html')
    rendered_html = html_template.render().encode(encoding="UTF-8")
    pdf_file = HTML(string=rendered_html).write_pdf()
    response = HttpResponse(pdf_file, content_type='application/pdf')
    response['Content-Disposition'] = 'filename="comprobante.pdf"'
    return response
    
asked by F Delgado 24.04.2018 в 08:08
source

0 answers