My code now:
serializer_class = InputSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (AllowAny,)
@action(detail=False, methods=['GET'])
def generate(self, request, **kwargs):
template = get_template('receipts/template1.html')
context = {
"invoice_id": 123,
"customer_name": "John Cooper",
"amount": 1399.99,
"today": "Today",
}
html = template.render(context)
with Display():
pdf = pdfkit.from_string(html, False)
response = HttpResponse(pdf, content_type='application/pdf')
return response
The error:
Traceback (most recent call last):
File "/home/varand/.virtualenvs/intrudevenv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/varand/.virtualenvs/intrudevenv/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/varand/.virtualenvs/intrudevenv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/varand/.virtualenvs/intrudevenv/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "/home/varand/.virtualenvs/intrudevenv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/varand/.virtualenvs/intrudevenv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/varand/.virtualenvs/intrudevenv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/varand/.virtualenvs/intrudevenv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/varand/Intrude/tools_api/views.py", line 418, in generate
pdf = pdfkit.from_string(html, False)
File "/home/varand/.virtualenvs/intrudevenv/lib/python3.8/site-packages/pdfkit/api.py", line 72, in from_string
return r.to_pdf(output_path)
File "/home/varand/.virtualenvs/intrudevenv/lib/python3.8/site-packages/pdfkit/pdfkit.py", line 159, in to_pdf
raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, stderr))
OSError: wkhtmltopdf exited with non-zero code -11. error:
Loading page (1/2)
[> ] 0%#015[======> ] 10%#015[==============================> ] 50%#015[============================================================] 100%#015