I want to generate pdf in my django project. I use pdfkit module to convert html page as a pdf file. I use this function but it has errors, like:
OSError
Exception Value:
No wkhtmltopdf executable found: "b''"
If this file exists please check that this process can read it or you can pass path to it manually in method call, check README. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
def customer_render_pdf_view(request, *args, **kwargs):
pk = kwargs.get('pk')
customer = get_object_or_404(Person, pk=pk)
enrolment = get_object_or_404(Enrollment,pk=pk)
template_path = 'test.html'
context = {'customer': customer, 'enrolment':enrolment}
template = get_template(template_path)
html = template.render(context)
pdf = pdfkit.from_string(html, False)
response = HttpResponse(pdf, content_type='application/pdf' )
response['Content-Disposition'] = 'filename= "report.pdf"'
return response
CodePudding user response:
you can use like this
import the report lab and pdf
CodePudding user response:
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.template.loader import get_template
import pdfkit
from .models import Buses
def pdf(request, id):
bus = Buses.objects.get(id=id)
template = get_template('buses/pdf.html')
html = template.render({'bus': bus})
options = {
'page-size': 'Letter',
'encoding': "UTF-8",
}
config =
pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
#use your actual location here
pdf = pdfkit.from_string(html, False, configuration=config,
options=options)
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="
{}_{}.pdf"'.format(bus.company, bus.name)
return response