python-pdfkit icon indicating copy to clipboard operation
python-pdfkit copied to clipboard

Provide help handling wkhtmltopdf error "ContentNotFoundError"?

Open hartwork opened this issue 6 years ago • 5 comments
trafficstars

Hi!

wkhtmltopdf might fail with a line[..]ContentNotFoundError[..] despite patching in --load-media-error-handling ignore to the command line.

Here's the rather dirty (Python 3) hack that I used at some point. I think it illustrates, that PDFkit could provide more help on that front:

import subprocess
from unittest.mock import patch


class _WkhtmltopdfPopen(subprocess.Popen):
    """
    wkhtmltopdf likes to return non-zero return codes despite
    use of --load-media-error-handling ignore.
    Therefore we middleman here to make PDFKit think we're good.
    """

    def communicate(self, *args, **kwargs):
        stdout, stderr = super().communicate(*args, **kwargs)

        if not stderr and 'ContentNotFoundError'.encode('ASCII') in stdout:
            # Make PDFkit think we're good
            # To make that work, stderr must not contain "Error" but be non-empty
            # (so that PDFkit does not fall back to inspecting stdout
            stderr = 'Done'.encode('ASCII')
            self.returncode = 0

        return stdout, stderr


with patch('subprocess.Popen', _WkhtmltopdfPopen):
    ...........

Best, Sebastian

hartwork avatar Dec 14 '18 22:12 hartwork

You're right, we can handle this error, maybe use still can provive a test case? :)

Sorry that it took so long for me to get to it...

JazzCore avatar Mar 19 '21 20:03 JazzCore

@JazzCore I am not using python-pdfkit myself these days I and have plenty of other things on my plate, so I'm afraid I don't have for a pull request on this topic in in near future.

hartwork avatar Mar 19 '21 20:03 hartwork

Hi!! If pages is not present in template folder, I'm getting blank pages in my pdfkit so what we can remove blank pages automatically and how? (https://user-images.githubusercontent.com/66722328/144794589-ae0be685-cb70-4d6c-8599-b779cb404d7d.png) i'm facing this issue? i have also added ignore missing in our code for ignoring the pages if pages is not present but this is not working for me: {% for page in pages %}

{% include page+".html" ignore missing %}
{% endfor %}

Anant-Agarwal1298 avatar Dec 06 '21 05:12 Anant-Agarwal1298

@Anant-Agarwal1298 I'm almost sure that this does not fall under this issue, you can create separate one for this. But since this falls under wkhtmltopdf issues, not pdfkit one - you better try asking for support here and provide some form of test case

JazzCore avatar Dec 06 '21 10:12 JazzCore

Thanks @JazzCore, i have created new issue under wkhtmltopdf.

Anant-Agarwal1298 avatar Dec 07 '21 06:12 Anant-Agarwal1298