django-wkhtmltopdf
django-wkhtmltopdf copied to clipboard
Added option to ignore ContentNotFoundError
My use case involved rendering lots of templates with user-uploaded images so bombing on a 404 was a deal breaker. Investigated other options, but wkhtmltopdf seemed like the best fit so...
A quick glance at the wkhtmltopdf source didn't reveal any way to do this with return codes, so I resorted to checking stderr. Had to rename subprocess.py since I'm no longer using the native implementation of check_source()
.
Added a few doctests to process.py
To use the new behaviour, set:
WKHTMLTOPDF_ENV = {
'ignore_404': 'True'
}
In your settings.py
.
@filipp do you mind splitting this f857631011e4ef0642647b44623ffc8bc138d2cc in 2 different commits please, one that renames the file and another that adds your changes. Will make the review easier on me ;-) Thanks !
Also as it seems to be related to the long standing issue about raising CalledProcessError
in an improper way (see issues: #122 #112 #107), and as you have already put some effort around it, it would be really nice to extend this PR to match the actual behavior of wkhtml2pdf and also improve the output in case of errors.
Again, this is only a proposal but that would greatly improve the lib, IMHO ;-)
I honestly don't know how to split the commits to pre/post file rename. I think we should actually just move check_output() to utils.py. :)
If someone could point me to the relevant wkhtmltopdf documentation or source on its different exit codes then I would be more than happy to add the proper handling. We should probably create exceptions for the different error scenarios withe corresponding options to ignore them in one's Django app.
You just need to go back in your git history with git rebase -i f857631^
, edit the commit, move the file, commit, do your changes commit again. The problem would be the same with moving the code to utils.py
btw, you'd have to first move the code without changing it, commit, do the changes, commit again.
I looked quickly and there doesn't seem to be a proper documentation... wkhtmltopdf... And from what I've read here, it seems to be difficult to really predict everything...
I guess we can start with what you did here and maybe at least improve error logging to give better feedback when something goes wrong.
Thanks for your time anyway.
@filipp do you think you could maybe split that commit up and I'll be very happy to merge this :-)
I got a TypeError: a bytes-like object is required, not 'str'
in line 73 of process.py if 'ContentNotFoundError' in error_message:
on Python3.5
Changing to if 'ContentNotFoundError' in str(error_message):
solved the issue. Didn't look deeper into it.