I'm trying to output a pdf from django. I am using weasyprint. Here is my view:
def fleet_report_pdf(request):
template = loader.get_template("Reports/fleetreport.html")
context = {
'crews': models.Unit.objects.all().annotate(c=Count('memberunit__Member')),
's31': models.Member.objects.filter(memberunit__isnull=True)
}
html = template.render(RequestContext(request, context))
response = HttpResponse(content_type="application/pdf")
weasyprint.HTML(string=html, base_url=request.build_absolute_uri(), url_fetcher=request).write_pdf(response)
return response
The pdf loads however, the page is not styled. when digging further, I noticed errors like these in the console:
Failed to load stylesheet at http://127.0.0.1:8000/static/css/layout.css : TypeError: 'WSGIRequest' object is not callable
Failed to load stylesheet at http://127.0.0.1:8000/static/css/darkblue.css : TypeError: 'WSGIRequest' object is not callable
Failed to load stylesheet at http://127.0.0.1:8000/static/css/custom.css : TypeError: 'WSGIRequest' object is not callable
This tells me that I'm missing something. How to make the stylesheets callable? Thanks.
You are using request
object as a url fetcher in your weasyprint.HTML
call (url_fetcher=request
). You shouldn't need to provide this argument for fetching simple resources that does not require authentication or so.
See this for details on URL fetchers.