Skip to content

Instantly share code, notes, and snippets.

@mtalcott
Last active December 9, 2016 04:39
Show Gist options
  • Save mtalcott/447c982e59bcdc91b25d to your computer and use it in GitHub Desktop.
Save mtalcott/447c982e59bcdc91b25d to your computer and use it in GitHub Desktop.
wkhtmltopdf ContentNotFoundError

wkhtmltopdf has very different behavior when certain resources fail to load, which depends on the URL of the resource.

Simply changing the URL of the failed resource changes the exit code of wkhtmltopdf. Certain URLs cause a ContentNotFoundError with an exit code of 1, and others are ignored with an exit code of 0.

All of these tests seem like they should have at least the same exit code.

Tested with wkhtmltopdf 0.12.2.1 (with patched qt).

test1

wkhtmltopdf test1.html test1.pdf

Loading pages (1/6)
Warning: Failed to load file:///not_here.css (ignore)
Warning: Failed to load file:///not_here.js (ignore)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

Exit code: 0

test2 (link href with params)

wkhtmltopdf test2.html test2.pdf

Loading pages (1/6)
Warning: Failed to load file:///not_here.js (ignore)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
Exit with code 1 due to network error: ContentNotFoundError

wkhtmltopdf --load-error-handling ignore test2.html test2.pdf

Exact same output as above: --load-error-handling ignore has no effect

test3 (script src with params)

wkhtmltopdf test3.html test3.pdf

Loading pages (1/6)
Warning: Failed to load file:///not_here.css (ignore)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
Exit with code 1 due to network error: ContentNotFoundError

wkhtmltopdf --load-error-handling ignore test3.html test3.pdf

Exact same output as above: --load-error-handling ignore has no effect

<html>
<head>
<title>wkhtmltopdf - test1</title>
<link href="/not_here.css" rel="stylesheet" type="text/css" />
</head>
<body>
<p>test1</p>
<script src="/not_here.js"></script>
</body>
</html>
<html>
<head>
<title>wkhtmltopdf - test2 - link href with params</title>
<link href="/not_here.css?body=1" rel="stylesheet" type="text/css" />
</head>
<body>
<p>test2</p>
<script src="/not_here.js"></script>
</body>
</html>
<html>
<head>
<title>wkhtmltopdf - test3 - script src with params</title>
<link href="/not_here.css" rel="stylesheet" type="text/css" />
</head>
<body>
<p>test3</p>
<script src="/not_here.js?body=1"></script>
</body>
</html>
@WaleedAshraf
Copy link

Quite helpful! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment