1

I used django and weasyprint to make an application which print a pdf file with picture and css file which design my pdf file. I used nginx, gunicorn and supervisor to deploy my application. In my intranet all is ok. When i used INTERNET PUBLIC IP ADDRESS to publish it on internet, my pdf file don't show anymore picture and css design. but all the application's static files work well I see my gunicorn log but nothing. I use Nginx to serve my static file. this the configuration

upstream app_server { server unix:/webapps/myapp/run/gunicorn.sock fail_timeout=0; }

server {

listen 80;

server_name 127.0.0.1;

client_max_body_size 4G;

access_log /webapps/myapp/logs/nginx-access.log;

error_log /webapps/myapp/logs/nginx-error.log;

location /static/ { alias /webapps/myapp/basegenecirdes/public/static/; }

location /media/ { alias /webapps/myapp/media/; }

location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off;

   if (!-f $request_filename) {
       proxy_pass http://app_server;
       break;
   }

}

in my views.py to call a pdf file, i use this

html = render_to_string('pdf/print.html', {'pagesize':'A4'}) response = HttpResponse(content_type="application/pdf",) response['Content-Disposition'] = 'inline; filename="print.pdf"' weasyprint.HTML(string=html,base_url=request.build_absolute_uri()).write_pdf(response)
return response

static image file:

<img style="background-color: white" src="{% static "image/photo_50x48.png" %}">

media image file

<img alt="{{ a.nom }}" src="{{ a.photo.thumbnail.url }}" >

is somebody have the same problem?

Hervé
  • 149
  • 5
  • 14

1 Answers1

0

After reading your edits, this question appears to be a duplicate of PDF output using Weasyprint not showing images (Django)

You have the first part of the answer already in your code. But I don't see this part in your write_pdf() call:

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

Community
  • 1
  • 1
ss7
  • 2,902
  • 7
  • 41
  • 90
  • thank you it is working, just picture d'ont appear, i update my post to show you how i put img html code in my pdf – Hervé Aug 05 '18 at 00:20
  • any help? Since 3 days, i am on this problem. – Hervé Aug 06 '18 at 00:51
  • how can i enable looging? i will put it after "return response"? – Hervé Aug 06 '18 at 11:45
  • 1
    To enable logging you’ll have to set it up in settings and then call getLogger() in the file before writing to it. This explains how to do it: https://docs.djangoproject.com/en/2.1/topics/logging/#configuring-logging – ss7 Aug 09 '18 at 23:31
  • i got 2 logs Failed to load stylesheet at http://IP_ADDR/static/css/default.css : HTTPError: HTTP Error 401: Failed to load image at "http://IP_ADDR/static/image/picture_50x48.png" (HTTPError: HTTP Error 401: ) – Hervé Aug 13 '18 at 18:56