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.
[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.
# 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.
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? 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With