snappy: ContentNotFoundError on pdf generation

Hello,

I’m currently having the following error when trying to generate a pdf from html:

The exit status code '1' says something went wrong:
stderr: "Loading pages (1/6)
[> ] 0%
[======> ] 10%
[=======> ] 12%
Warning: Failed to load file:///assets/admin/scripts/bootstrap.js (ignore)
Warning: SSL error ignored
[=================> ] 29%
[=====================> ] 35%
[============================================================] 100%
Counting pages (2/6) 
[============================================================] Object 1 of 1
Resolving links (4/6) 
[============================================================] Object 1 of 1
Loading headers and footers (5/6) 
Printing pages (6/6)
[> ] Preparing
[============================================================] Page 1 of 1
Done 
Exit with code 1 due to network error: ContentNotFoundError
"
stdout: ""
command: /usr/local/bin/wkhtmltopdf --lowquality '/var/tmp/knp_snappy55c0b954e4cf05.58613037.html' '/var/tmp/knp_snappy55c0b954e4dfd4.48017554.pdf'.

Did I miss something ?

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 19 (5 by maintainers)

Most upvoted comments

It would be very convenient to choose which exit status codes throw an exception.

For example an option could be added to the AbstractGenerator class. This option may contain the list of codes not throwing an exception.

Find the list of wkhtmltopdf exit codes found in the manual below:

Exit Code Explanation
0 All OK
1 PDF generated OK, but some request(s) did not return HTTP 200
2 Could not something something
X Could not write PDF: File in use
Y Could not write PDF: No write permission
Z PDF generated OK, but some JavaScript requests(s) timeouted
A Invalid arguments provided
B Could not find input file(s)
C Process timeout

The thing is codes 1 & Z should not throw an exception systematically because the PDF is generated.

I made a workaround, you can find it below:

try {
    $pdf = $generator->getOutputFromHtml(
        $html,
        $params
    );
} catch (\RuntimeException $e) {
    $matches = [];
    if (preg_match('/([^\']+)\'.$/', $e->getMessage(), $matches)) {
        $pdf = file_get_contents($matches[1]);
        unlink($matches[1]);
    } else  {
        throw $e;
    }
}

It is not very clean code but it does the job, thus the issue should remain opened until it is fixed 😃

Ive set load-error-handling and load-media-error-handling to ‘ignore’

'pdf' => [
        'enabled' => true,
        'binary'  => env('WKHTML_PDF_BINARY', '/usr/local/bin/wkhtmltopdf'),
        'timeout' => 3600,
        'options' => [
            'print-media-type' => true,
            'image-dpi' => '300',
            'load-error-handling' => 'ignore',
            'load-media-error-handling' => 'ignore',
        ],
        'env'     => [],
    ],

@nWidart you are trying to convert local file, right? In this case, bootstrap.js has relative path and it is interpreted as invalid. You should either use absolute file path in your html or if this JS file is not important for you, you can try https://github.com/wkhtmltopdf/wkhtmltopdf/blob/gh-pages/usage/wkhtmltopdf.txt#L138-L143

Anyway, it is not Snappy issue.