Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Django PDFKIT - [Errno 9] Bad file descriptor

I use pdfkit and wkhtmltopdf to generate pdf documents. When i generate the first pdf all is well. When i quickly (within 5 seconds) generate an other i get the error [Errno 9] Bad file descriptor. If i close the error (step back in browser) and open again, it will create the pdf.

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

Maybe important note: i run this site on IIS8, when running from commandline (python manage.py runserver) the error is not present.

Any guidelines on how to handle this error would be great.

like image 711
phicon Avatar asked Jun 21 '15 15:06

phicon


People also ask

What is bad file descriptor in Python socket?

[Errno 9] Bad File Descriptor in Python Socket Module Another main area in which this error is seen is in the Python socket – Socket error Bad file descriptor. When dealing with this kind of program, you can notice that you will find a Bad file descriptor error message is seen along with some issues in opening/closing or accessing the socket.

How do I make a PDF from a Django template?

# urls.py from django_pdfkit import PDFView ... url(r'^my-pdf/$', PDFView.as_view(template_name='my-pdf.html'), name='my-pdf'), ... Then in your browser goto http://localhost:8000/my-pdf/ and it will magically render as a PDF.

What is a negative file descriptor in Python?

In Python, file descriptors are integers (positive) that identify the kernel’s open files kept in a table of files. They are generally non-negative values. If found to be negative, that indicates error or a “no value” condition.

What are file descriptors in Python?

What are File Descriptors in Python? In Python, file descriptors are integers (positive) that identify the kernel’s open files kept in a table of files. They are generally non-negative values. If found to be negative, that indicates error or a “no value” condition. They assist in performing various functions related to files.


1 Answers

When i quickly (within 5 seconds) generate an other

This point suggests that your code is flawless and the problem lies with your browser rejecting the URL as Peter suggests.

Most probably the cause of the error lies with file buffer flush. Consider flushing buffer at appropriate places.

like image 144
Himanshu Mishra Avatar answered Sep 19 '22 19:09

Himanshu Mishra