Python Django PDFKIT - [Errno 9] Bad file descriptor

I use pdfkit and wkhtmltopdf to create pdf documents. When I create the first pdf, everything is fine. When I quickly generate another one (within 5 seconds), I get an error [Errno 9] Bad file descriptor. If I close the error (return to the browser) and open again, it will create a pdf file.

my views.py

config = pdfkit.configuration(wkhtmltopdf='C:/wkhtmltopdf/bin/wkhtmltopdf.exe') pdfgen = pdfkit.from_url(url, printname, configuration=config) pdf = open(printname, 'rb') response = HttpResponse(pdf.read()) response['Content-Type'] = 'application/pdf' response['Content-disposition'] = 'attachment ; filename =' + filename pdf.close() return response 

Perhaps it is important to note: I am running this site on IIS8, there is no error when starting from the command line (python manage.py runningerver).

Any advice on how to deal with this error would be great.

+7
python django iis pdfkit
source share
2 answers

When I quickly (within 5 seconds) create another

This point assumes your code is flawless, and the problem is that your browser rejects the URL, as Peter suggests.

Most likely, the cause of the error is to clear the file buffer . Examine the wash buffer at appropriate locations.

+6
source share

Without further information, I will return my comment on the answer ...

Most likely, the problem is that your URL is rejected by the web server when you try to quickly reboot (via from_url) or you have problems accessing the local file that you are trying to create.

You can try to eliminate the latter by simply pointing directly to the variable, passing False as the name of the output file - for example. pdf = pdfkit.from_url('google.com', False) .

If this does not solve the problem, your problem will almost certainly be with the server rejecting the URL, and therefore you need to look at the diagnostics on this server.

+3
source share

All Articles