【问题标题】:Django : Display image with Pisa libraryDjango:使用 Pisa 库显示图像
【发布时间】:2017-06-26 07:34:54
【问题描述】:

我正在寻找在我的由html2pdf library 生成的PDF 中添加图片。 到目前为止,我还没有找到办法。

我的函数看起来像:

@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))

在我的RawFile.html 中,我添加:

<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>

如何在我的 PDF 中添加我的徽标和另一张图片?我搜索了不同的方法来做到这一点。没有一个工作。

谢谢

【问题讨论】:

    标签: django html2pdf pisa


    【解决方案1】:

    我找到了解决方案;)

    这是我的脚本:

    @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()
    

    还有我的 HTML 文件:

    <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 %}
    

    【讨论】:

      猜你喜欢
      • 2011-11-09
      • 2023-04-01
      • 2011-01-11
      • 2011-04-29
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      相关资源
      最近更新 更多