Skip to content

Provide help handling wkhtmltopdf error "ContentNotFoundError"? #122

Open
@hartwork

Description

@hartwork

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions