Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Django convert current page to PDF on button click

I am using Django 1.10 and python 3.4 on windows. I set up pdfkit and it works perfectly from command line. I want to use it in the site on a button to return the current page as a pdf attachment without redirecting anywhere(if possible). The page to convert is a report which uses a user specified date as url.

Currently the button redirects by appending the current url with "?mybtn=Generate+PDF#"

I have seen some solutions using jQuery or javascript, but I only really know python well. I will learn those eventually, but for now I would like a quicker solution where I understand what's going on.

views.py

class FlashReport(generic.View):
template_name = "reports_flash_report.html" 

def get(self, request, year, month, day):
    report_data = get_flash_report_data(int(year),int(month),int(day))
    return render(request, 'reports_flash_report.html', report_data)

def generate_PDF(request):
    path = request.get_full_path()
    pdf = pdfkit.from_url(path, False)
    response = HttpResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="flash_report.pdf"'
    return response

reports_flash_report.html

<div>
    <form action="" method="get">
        <input type="submit" class="btn" value="Generate PDF" name="mybtn" />
    </form>
</div>

urls.py

url(r'^flash_report/(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})/$', views.FlashReport.as_view(), name="flash_report"),
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!