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"),