PDF output using Weasyprint not showing images (Django)

旧街凉风 提交于 2019-12-10 15:22:39

问题


I am trying to output PDF on Django using the Weasyprint library, but the images don't appear on the generated PDF. I have tried both relative and static URLs for the images, but even the static URL doesn't show the image. When opening the HTML itself on chrome, the images do show.

Here is my pdf generation view in the views.py file:

def pdf_generation(request, some_slug)
    stud = Student.objects.get(some_slug=some_slug)
    studid = stud.some_slug
    context = {'studid':studid}
    html_string = render_to_string('templates/pdf_gen.html', context)
    html = HTML(string=html_string)
    pdf = html.write_pdf(stylesheets=[CSS(settings.STATIC_ROOT +  '/css/detail_pdf_gen.css')]);
    response = HttpResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = 'inline; filename="mypdf.pdf"'
    return response

Here is the part of the HTML of the image:

<DIV id="p1dimg1">
<IMG src="{% static 'img/image.jpg' %}" alt="">
</DIV>

And the CSS:

#page_1 #p1dimg1 {position:absolute;top:0px;left:0px;z-
index:-1;width:792px;height:1111px;}
#page_1 #p1dimg1 #p1img1 {width:792px;height:1111px;}

Thank you very much


回答1:


Fixed by:

Add base_url=request.build_absolute_uri() so that

html = HTML(string=html_string)

becomes

html = HTML(string=html_string, base_url=request.build_absolute_uri())

That will allow for relative URLs in the HTML file.

For the images, only PNG images seems to work for some reason.

For the HTML styles to show on the PDF, add presentational_hints=True as per the Weasyprint docs:

    pdf = html.write_pdf(stylesheets=[CSS(settings.STATIC_ROOT +  '/css/detail_pdf_gen.css')], presentational_hints=True);



回答2:


Setup static for the path of your image as:

{% load static %}
<img src="{% static 'images/your_image.png %}" alt="" />

and then you have to pass the base_url in HTML class of Weasyprint as:

HTML(string=html_string, base_url=request.build_absolute_uri())



回答3:


I don't know Weasyprint, but I'm using Pisa and it works very well with pictures into PDF output.

For example :

def PDFGeneration(request) :

    var1 = Table1.objects.last()
    var2 = Table2.objects.last()

    data = {"key1" : variable1, "key2" : variable2}

    html = get_template('My_template_raw.html').render(data)
    result = StringIO()

    with open(path, "w+b") as file :
      pdf = pisa.pisaDocument(StringIO(html), file, link_callback=link_callback)
      file.close()

      image_data = open(path, "rb").read()
      return HttpResponse(image_data, content_type="application/pdf")

    return render(request, 'HTML template', context)

and

def link_callback(uri, rel):
    if uri.find('chart.apis.google.com') != -1:
        return uri
    return os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))

My PDF is generated from an .html file and I have my picture like this :

<html>
    <head>
    {% load staticfiles %}
    {% load static %}

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
    <link rel="stylesheet" type="text/css" href="{% static 'css/MyFile.css' %}"/>

    <style>

            body {
                font-family: Courier New, Courier, monospace;
                text-align: justify;
                list-style-type: none;
            }
    </style>

    </head>

    <body>

        <img src="{{MEDIA_ROOT}}Logo/logo.jpeg" width="250" height="66"/>
        <br></br>
        ...


来源:https://stackoverflow.com/questions/48988707/pdf-output-using-weasyprint-not-showing-images-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!