简体   繁体   中英

Django : Display image with Pisa library

I'm looking for add Pictures in my PDF generated by html2pdf library . Up to now, I don't find a way to do that.

My function looks like :

@login_required
def Generate_PDF(request, id) :

    personne = get_object_or_404(Individu, pk=id)

    data = {"personne" :personne}

    template = get_template('raw.html') 
    html  = template.render(Context(data))
    result = StringIO.StringIO()

    path = Global_variables.Individu_path.path + filename


    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("utf-8")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

In my RawFile.html , I add :

<body>

        <img class="logo" src="{% get_static_prefix %}{{ mytheme }}/images/logo.jpeg" />

        <h2 align="center"> <font size="6"> Fiche d'Identification - INDIVIDU</font></align> </h2>

        <br></br>
        <br></br>

        {% block content %} 

        {% if personne.Image %}
            <img src='./19312STRASBOURG-855525.jpg'/>
        {% endif %}

        <h3 align="left"> N° Identification {{personne.NumeroIdentification}} </align> </h3>
        <h3 align="left"> Au nom de {{personne.Prenom}} {{personne.Nom}}</align> </h3>
        ...
        </body>

How I can add my logo and another picture in my PDF ? I searched different way to do that. None is working.

Thank you

I found the solution ;)

This is my script :

@login_required
def Identity_Individu_PDF(request, id) :

    folderId = None

    personne = get_object_or_404(Individu, pk=id)

    data = {"personne" :personne}

    template = get_template('Identity_Individu_raw.html') # A modifier : template pdf généré
    html  = template.render(Context(data))
    result = StringIO.StringIO()

    NumeroIdentification = str(Individu.objects.get(pk=id).NumeroIdentification.encode('utf-8'))
    NumeroIdentification_2 = NumeroIdentification.replace(" ", "")

    filename_directory = str(Individu.objects.get(pk=id).Nom.encode('utf-8')) + "_" + str(Individu.objects.get(pk=id).Prenom.encode('utf-8')) + "_" + NumeroIdentification_2
    filename_init = 'Fiche_Identification_' + filename_directory 
    filename = filename_init + '.pdf'

    path = Global_variables.Individu_path.path + filename

    file = open(path, "w+b")
    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("utf-8")), file)
    file.close()

And my HTML file :

<h2 align="center"> <font size="6"> Fiche d'Identification - INDIVIDU</font></align> </h2>

        <br></br>
        <br></br>

        {% block content %} 

        {% if personne.Image %}
            <img src='/Users/Desktop/Django/DatasystemsCORE/Photos_Identifications/pictures/19312STRASBOURG-614245.jpg' class="img-responsive"/>
        {% endif %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM