downloading a functioning pdf in django using wkhtmltopdf

I have a django project, where i download the template as a pdf using wkhtmltopdf.The pdf is downloaded when i click the download button, However when i open the pdf it says, “Error Failed to load PDF document.” and i cant open the downloaded pdf. Upon looking at the terminal i see this error: UserWarning: A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext. What is causing this issue and how can it be solved?

this is my views.py:

@login_required def download_resume(request): # Retrieve data from your Django models personal_info = personalinfo.objects.filter(user=request.user).last() summaries = summary.objects.filter(user=request.user).last() experiences = experience.objects.filter(user=request.user) educations = education.objects.filter(user=request.user) certs = certificates.objects.filter(user=request.user) skillset = skills.objects.filter(user=request.user)

# Generate the PDF content using a template
template = get_template('template0.html')
context = {
    'personal_info': personal_info,
    'summaries': summaries,
    'experiences': experiences,
    'educations': educations,
    'certs': certs,
    'skillset': skillset
}

# Render the template with the context data
html_content = template.render(context)

# Configure pdfkit with the path to wkhtmltopdf
pdfkit_config = pdfkit.configuration(wkhtmltopdf='C:/Users/lulu/PProjects/resumebuilderproject/wkhtmltopdf/bin/wkhtmltopdf.exe')

# Convert HTML to PDF using wkhtmltopdf
pdf = pdfkit.from_string(html_content, False, configuration=pdfkit_config)

# Create a response with the PDF content
response = FileResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="resume.pdf"'

return response

this is my html download button:

  <form method="post" action="{% url 'download_resume' %}">   {%
  csrf_token %}   <button type="submit" class="download-button">
   <i class="fas fa-download"></i> DOWNLOAD   </button>   </form>

I’m not sure why you’re using a form and a post for this - unless there’s a lot more to this form than the segment you’re displaying here.

Either way, that warning message you’re seeing is something you can safely ignore. (To resolve it, you would need to fix it in the view that is producing the page containing the form, not this page that is handling the post.)

Given that wkhtmltopdf is now effectively a dead project, and pdfkit isn’t far behind, I’m not sure I’d want to be relying upon it.

Now, having said that, if you really want to try and debug/diagnose this, I’d try writing the pdf variable out to a file to verify that it is a valid pdf - or at least to see what you’re getting.

Hi Ken, Thank you for your help and suggestions. Honestly, I was just using wkhtmltopdf because upon doing research, it seemed like it was the best html to pdf converter compatible with Django. I tried using the wkhtmltopdf wrapper instead of downloading the files, but that didn’t work, so I ended up installing it and using the binary file but it’s not working so great as well. Do you happen to have any recommendations for a PDF converter that can work well with Django?

Sorry, no. We use ReportLab when we generate PDFs, because we need more precise control over the format and layout of the pages being generated. We’ve never found an HTML → PDF converter that provided sufficient control. (Exception: We produce a lot of tabular data using the jQuery Datatables library, and it provides a “PDF export” facility that we also use. It’s possible that the PDF-creation library that it uses could be adapted.)