1

I have a question related to generating PDF in a Django Project using Weazy Print

So the first thing I have done is that I made sure that the PDF is showing correctly as per the CSS file I have included in the function below in the views.py:

def admin_order_pdf(request, info_id):
    info = get_object_or_404(Info, id=info_id)
    html = render_to_string('businessplan/pdf.html', {'info': info})
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename="order_{}.pdf"'.format(
        Info.id)
    weasyprint.HTML(string=html,base_url=request.build_absolute_uri()).write_pdf(response,
                                           stylesheets=[weasyprint.CSS(settings.STATICFILES_DIRS[0] + '\css\\report.css')], presentational_hints=True)
    return response

Second I have included the {% load static %} in the report.css and I am trying to access the image file using the below:

@page :first {
  background: url('{% static "img/report-cover.jpg" %}') no-repeat center;
  background-size: cover;
  margin: 0; }

I have reviewed the following questions and their answers but they didn't help achieving my goal to show the image:

  1. PDF output using Weasyprint not showing images (Django)
  2. attach img file in pdf weasyprint

My trials:

I was able to get the image showing by adding @page :first in the html page between <style></style> but my issue is that there are other things that I need to link also like fonts and svgs so I wanted to know what I am doing wrong that doesn't allow the css file to show content from the static file.

My question to be more clear:

I want to be able to link files in the static folder to show from the report.css file

Etchosh
  • 111
  • 7

0 Answers0