Exit with code 1 due to network error contentnotfounderror

What is the HTML you are trying to convert?

What is the HTML you are trying to convert?

You were using pipes for input — what is the command line you use for this?

Here’s the output I get:

Exit with code 1 due to network error: ContentNotFoundError
command failed (exitstatus=1): /usr/local/bin/wkhtmltopdf —quiet —page-size Letter —margin-top 0.75in —margin-right 0.75in —margin-bottom 0.75in —margin-left 0.75in —encoding UTF-8 —load-error-handling ignore — /tmp/5463d01c6f706554720b0100.pdf

It’s BASH if you mean which shell?

Any solution to this as I am having the same issue?

I’m facing the same. I’m using mac OS X 10.9.5. My command is ‘wkhtmltopdf’ inputfile outputfile.

Same issue, I get the error when wkhtmltopdf request some resources on a domain managed by the same server, solved adding a new row in the hosts file of the server:

127.0.0.1 domain_used

@TheShrike I have tried your solution as I am having the same problem. All content renders fine as long as its not on the same server that wkhtmltopdf is running on. I have added the domain to my hosts file in CentOS 6 and saved but I get the same result. Is there anymore info or help you can give?

@Joe90k Are you sure the new hosts file is used? If you try to ping your domain you should get this:
PING your_domain (127.0.0.1) etc.

@TheShrike yes I do, wkhtmltopdf still generates a redirect page which is the default page for any domain that is pointing at our server but has not been assigned a web root.

I wish there was a way of seeing where/how wkhtmltopdf looks for the domain!

What happens if you wget the URL you are trying to convert on the server itself? If it generates the redirect page, then that is a server setup issue.

@ashkulz Simple, hadn’t thought of that. It does indeed generate the redirect page. It seems there must be something amiss with the server config. Thanks for the help.

I had this same ContentNotFoundError with a simple wkhtmltopdf command.
Turned out that my html page had a script line in the header pointing to a .js file, with a relative url, something like ‘../blabla.js’
Looks like wkhtmltopdf cannot handle that.
So I took out that line — and the error disappeared :-)

We’ve also run into this problem on our development servers, and as it turns out, the Symfony profiler injected its debugging toolbar into the pages we were trying to render, which made wkhtmltopdf choke. The JavaScript of the toolbar must have changed with a recent Symfony update, making this error pop out of nowhere. Disabling the profiler in the affected controllers solved it.

Is there a general solution in cases where you don’t have control over the relative URLs? Potentially a flag to place that lets wkhtmltopdf ignore the error?

I’m running into the same thing. I’ve tried with «load-error-handling» as both «ignore» and «skip». Neither seems to ignore the content load errors for all the files that are failing. The output shows it’s skipping some of them but not all?

The same here when downloading:

http://stackoverflow.hewgill.com/questions/282/357/1.html

wkhtmltopdf v0.12.2.1 (with patched qt)

Related:

  • Issues/bugs in wkhtmltopdf 0.12 at stackoverflow SE

I’m seeing that slight changes in the URL of certain resources completely changes the load error handling behavior of wkhtmltopdf. Additionally, --load-error-handling has no effect on such behavior.

Here are some test cases with results: https://gist.github.com/mtalcott/447c982e59bcdc91b25d

I’ve run into this bug also.

We are generating some HTML and using the <base href="http://site.example.com"> tag to resolve URLs beginning with / to a full URL.

However, if a CSS reference like
<link rel="stylesheet" type="text/css" href="/path/to/mainsite.css?13452" />
is present, that causes the problem discussed above.

Changing this to remove the versioning number, i.e.
<link rel="stylesheet" type="text/css" href="/path/to/mainsite.css" />
causes the program to complete cleanly with a success code.

The file has similar query strings with javascript files and general URL links, but they do not trigger the problem. Removing just the query string in the CSS reference avoids the bug being triggered.

This issue bite me too.
In my case, i have the follow code in my template

    <script>if (!window.jQuery) {
        document.write('<script src="/static/core/js/jquery/1.11.3/jquery.min.js"></script>');
        document.write('<script src="/static/core/js/jquery/1.11.3/jquery-migrate.min.js"></script>');
    }
    </script>

after removing this code, all goes as expected

I have solved this issue by put full path of your js and css files. It will be like
<link rel="stylesheet" href="http://blabla.com/css/cssFilename.css"/>
< script type="text/javascript" src="http://blabla.com/js/jsFileName.js"/></script>

CMIIW

Isn’t there a separate bug in that this error message sucks? I’m running across this intermittently in our logs, yet whenever I debug I cannot replicate it. I have no way to know which script is intermittently failing because the error message doesn’t give that info. This should be fixed, the bad error message is a bug.

I’m facing a similar issue. My html has no request to an external resource. The file is created, but I get this error.

Loading pages (1/6)
Warning: Failed loading page http:// (ignored)
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: HostNotFoundError

And the command I’m using is this,
cat test.html | wkhtmltopdf —margin-bottom «1» —margin-left «1» —margin-right «1»
—margin-top «5» —orientation «Landscape» —page-size «Legal»
—load-error-handling «ignore» —load-media-error handling «ignore»
—disable-smart-shrinking «» — «out.pdf»

This is particularly problematic when I’m using the JS library. On getting this error, the error handler in the callback function is triggered, while the file is generated.

I’m using wkhtmltopdf 0.12.2.1 (with patched qt) on Ubuntu 14.04.

Same bug over here when trying to pdf a locally URL and I’m even using ‘abolute paths’ everywhere and no JavaScript.
When giving an external URL everything works as expected.

I just got this error (despite passing both --load-error-handling "ignore" and --load-media-error-handling "ignore".

Is it possible that Webkit was updated recently? I don’t know how my server would have gotten it, but because I have <img src="REPLACE_ME"> somewhere in a JSON object, the rendering engine seems to attempt to preload the resource despite it not existing?

@brandonros not quite sure that makes sense. Somewhere in a JSON object? So you have a json (string), or an object (not json)? «Where» is this value? stored in a javascript variable? or is the value written to the DOM. The browser will try to load any src tags though. That’s why frameworks like angular have ng-src. Try renaming src to brandon-src, and then only write the URL into src with the real value rather than REPLACE_ME.

Adding the <base> tag, as per https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2051#issuecomment-137267732 above, fixed this problem for me, despite my Drupal-generated page having query strings on all the CSS files (in contradiction to the latter part of the same comment).

Weirdly, none of my paths were even relative, yet the base tag made the problem go away anyway. I _did_ have a <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> but didn’t try removing that to see if it was the culprit.

Seems you also get this error if you have relative paths in your CSS.

For example, this styling from semantic-ui will cause this error:

@font-face {
  font-family: 'Icons';
  src: url("themes/default/assets/fonts/icons.eot");
  src: url("themes/default/assets/fonts/icons.eot?#iefix") format('embedded-opentype'), url("themes/default/assets/fonts/icons.woff2") format('woff2'), url("themes/default/assets/fonts/icons.woff") format('woff'), url("themes/default/assets/fonts/icons.ttf")     format('truetype'), url("themes/default/assets/fonts/icons.svg#icons") format('svg');
  font-style: normal;
  font-weight: normal;
  font-variant: normal;
  text-decoration: inherit;
  text-transform: none;
}

WT… this started happening to me right now, nothing has changed that I know and I think I’ve always had relative links. I am using the prebuilt binary as well…

Ah nvm, it was a hosts issue (I had suppressed fonts.google.com but didn’t remember it). However the «ignore» options described above didn’t work

I just did some debugging because I have the same issue with Google fonts imports.

The HTML contains the following in the header (amongst other things) :
<style>
@import url(https://fonts.googleapis.com/css?family=Roboto:400,500,700,300);
</style>

When I try to create the PDF without an internet connection this always fails with ‘Exit with code 1 due to network error: HostNotFoundError’, even though I used —load-error-handling ‘ignore’ and —load-media-error-handling ‘ignore’. If I comment the imports in the template everything works as expected, and the PDF gets generated.

Hope this helps in finding the root cause.

As @joshribakoff says, the message makes this far more difficult to debug than it could be. That is a separate issue, #2228.

Yeah, this still is a serious bug. I do not really give a s*t about not having some fonts there. I just want my output.

This bug has made me switch to another PDF generator. Which is what nobody wants…

I had this issue too. Checked various option and found the root of this problem and most of the issues reported here on various network and content not found error.

The issue is mainly due to a bug in qtwebkit qt4 patched version source code used in wkhtmltopdf. qtwebkit is not able to correctly resolve browser dns redirection and multi dns calls to resource located on one or more resources within the page. An upgrade to newer qtwebkit Qt5 and a revised batch will likely fix all theses bugs.

Until maintainers have time to work on this or someone with the expertise lends a hand (sadly not me of course :- ( ) a workaround I found fixes the issue.

calling your page using server IP without domain names fixed the problem. so instead of using http://mydoamin.com/page_url

in your wkhtmltopdf argument call
http://xx.xx.xx.xx/page_url

And if you have linked files in your webpage not loading try calling them through server IP and it will likely work.

hope you find this helpful.

Yeah but this still does not explain _why_ by default one 404 needs to _abort_ the whole rendering process. There ought to be a way to configure it.

There is away to configure behavior on 404 but it does not work, the —ignore flag should allow bypassing unloaded content, unfortunately it does not!!!

I ran into this issue as well. I tried with these flags: --load-error-handling ignore and --load-media-error-handling ignore but the error kept happening. In my case, the content that wasn’t found were two images.

I ended up --no-images which is not ideal.

I am experiencing exactly the same problem.

Hey guys, I faced the same issue today in laravel.
_Exit with code 1 due to network error: ContentNotFoundError_
I tried to wrap my all css and script link with url() function.
example:
url(‘/css/bootstrap.min.css’)
url(‘/js/jquery-2.1.1.js’)

and my code working. Now every thing is fine. :)

I get that this may not be fixed in the short term, but it there a legit way to suppress this specific error? I know certain things wont load in my page but I need the output without wkhtmltopdf crashing my node server because it exited with an error code.

While reading one of the answers above I realized that the output image file is generated despite the fact that the error code is returned.
I used to verify result by testing exit code, but I switched my code to check if the output file exists.
For sure it’s not a best practice, but it works brilliant as long as server errors can’t be suppressed.

I’m using WKHTMLTOPDF with cakephp (cakepdf), and I’m able to generate a pdf successfully once every 1-2 minutes. the input/output is a blank page, and without changing anything and running it more often than every 1-2 minutes generates this error.

Using 0.12.3 on ubuntu 16.04 x64

Hello All,

Have you check special characters in URL or not?If there is special characters than you should encode it .Only encode special characters not any other.

Same error here. Happening with an image.
Tried with IP versus Hostname, didn’t change anything 😢

Any news? Still the same error here!

I don’t think this will be fixed so soon, given how long it has been opened. But the issue is due to a relative path. You have to include the full file path. It might be occurring with your JavaScript/CSS files. Instead of using /path/to/file use http://domain.com/path/to/file.

@hellvix: Both your examples use absolute path. You’ve described a difference between a absolute filesystem path (/path/to/file) and a URL containing an absolute path (http://domain.com/path/to/file).

Are you saying we should expect the program to fail when specifying an absolute path of a file? That only a URL will work?

@benf-wspdigital That is not the absolute system file location, it is path for the file file in the URL after the domain. Basically the difference between HttpRequest.get_full_path() and HttpRequest.build_absolute_uri described in here.

If you hard code your HTML (that will be transformed in PDF) with the full URL of the file (including the domain), it will work. Instead of using {% static 'path/to/file' %} use http://domæne.com/path/to/file. A way to know which resource file is missing, is going to /tmp/ (assuming you are on Linux) and check the temporary files wkhtmltopdf generates and tries to transform into PDF. If you open it with your browser and inspect the source of the page, you’ll see the missing resources there.

I am struggling with this issue for some time. The problem is that I have a lot of resources that are dynamically generated. I wanted this PDF class to be as generic as possible without much patching — so it could fit other parts of my code -, but this URL thing is giving me a huge headache.

Edit

Javascript files make this exception, however .css files are not accessible either, leading the page to be rendered without styles. A workaround this to read the CSS file with Python and dump its context in a context variable inside your template.

As mentioned, open the temporary file inside /tmp/ and read the missing resources from there. You’ll have an idea what the problem is.

If you hard code your HTML (that will be transformed in PDF) with the full URL of the file (including the domain), it will work.

Thanks, that’s what I thought you meant.

I don’t think that counts as a work-around; what’s needed is to specify static files on the local filesystem.

I had this issue several times and the root cause was same — the renderer could not load a/some javascript/image/font file. After resolving missing resource, the issue was fixed.
BTW, to quickly find the missing resource you may open the page URL from the browser with keeping console > Network panel open.

This error occurs when url are defined as //url… in place of https://url….
Keep the protocol.
wkhtmltopdf try find double slash as local path…

Solved for me.

I have added these flags based on the suggestions above: --load-error-handling ignore and --load-media-error-handling ignore

And I am using the version: wkhtmltopdf 0.12.1 (with patched qt).

However still getting this error for an HTML page with video:
Exit with code 1 due to network error: ContentNotFoundError

Please suggest and any help on this will be appreciated.

For those having issues with fonts ALTHOUGH they are available locally (which fixed the issue in previous versions such as 0.9.6) — it seems there is still a problem with the relative path in font-face. So if you use the absolute path (e.g. with $_SERVER[‘DOCUMENT_ROOT’]) it now works for me!

I got same error message «Exit with code 1 due to network error: ContentNotFoundError».
The problem was that source HTML referenced resources which had only path in them. For example:

<svg role="presentation">
<use xlink:href="/svg/sprite.svg" xmlns:xlink="http://www.w3.org/1999/xlink" />
</svg>

It won’t be fixed. Instead, read what I posted above. There is a work-around for this problem.

I don’t think this will be fixed so soon, given how long it has been opened. But the issue is due to a relative path. You have to include the full file path. It might be occurring with your JavaScript/CSS files. Instead of using /path/to/file use http://domain.com/path/to/file.

@ragnarkurmwunder

<use xlink:href="https://YOUR_DOMAIN/PATH_TO_STATIC/svg/sprite.svg" xmlns:xlink="http://www.w3.org/1999/xlink" />

@hellvix the problem is that we cannot fix source content. why you think its not gonna be fixed. i think many people using wkhtmltopdf and having this issue.

@to-kra Yeah, I know we can’t edit the source and that’s why I am saying it won’t be fixed. Look when the issue in this topic was originally reported. It has been opened for years.

@hellvix who is then responsible for fixing wkhtmltopdf ? :(

@to-kra I would guess the guy owning this project on GitHub.

who is then responsible for fixing wkhtmltopdf

It’s an open source, community project. If no-one kindly volunteers their time, or finds funds to have someone to pay to fix it, then it won’t get fixed. Personally, I’m very grateful to all those who have worked on the software so far.

I have this issue too just trying to render a pdf from html. It really sucks. Sorry for being negative but this defect really makes wkhtmltopdf rather unusable.

I found a workaround:

// Replace all ?sadasd query strings from static file includes
$html = preg_replace('#(.css|.js)?[^"]+#',  '$1', $html);

before passing html to wkhtmltopdf

In case of Ubuntu, try to update your libssl with:

sudo apt install libssl-dev=1.0.2g-1ubuntu4.10

You too?

For anyone that has stumbled onto this thread via a search engine, dismayed that the issue was ‘closed without reason’, read on.

This is a generic error, linked to specific cases

This is a generic error that wkhtmltopdf generates when it cannot find a specific resource.
This means that your html / code passed to wkhtmltopdf contains links to resources that wkhtmltopdf cannot find.

The reasons it cannot find them are several and varied.

The most simple:

  • Perhaps you made a typo in the resource
  • Perhaps the resource doesn’t actually exist

An easy way to check if you fall into this category, tail your web server logs.
You may notice 404 / 403 errors being generated, ie

Request URL: /bower_components/fontawesome/webfonts/fa-regular-400.svg
Result: 404

The more complicated

If you’re using absolute URLs for your content, there could be other reasons:

  • Your webserver can’t reach itself
  • You have multiple web-servers and the resource in question isn’t on all of them
  • Your server has outdated ca-certs / ssl libraries

Solution
Go through the source passed to your PDF, and check every single absolute URL from the CLI of the machine that you’re attempting to generate the PDF, ie:

curl -s -o /dev/null -w "%{http_code}" http://your.com/url/path

If it doesn’t return ‘200’ — this may be where your problem lies, and will merit further investigation.

Or, you may be incredibly unlucky and have actually discovered a bug — lodge an issue and good luck!

For me, the problem was that one of the «src» tags in the HTML file was pointing to a wrong directory (because forgot to change it after switching to another machine)

Same error here. Happening with an image.
Tried with IP versus Hostname, didn’t change anything 😢

i transform src to base_64 format for img

Does anyone have the solution now in 2019?

wkhtmltopdf --lowquality --header-html '/tmp/knp_snappy5d5d2fce6625d0.53804995.htm
> l' '/tmp/knp_snappy5d5d2fce660806.00715860.html' '/tmp/knp_snappy5d5d2fce661d57.43165999.pdf'

Loading pages (1/6)
Error: Failed loading page http:// (sometimes it will work just to ignore this error with —load-error-handling ignore)
Exit with code 1 due to network error: HostNotFoundError

RUN apk add —update —no-cache
libgcc libstdc++ libx11 glib libxrender libxext libintl
ttf-dejavu ttf-droid ttf-freefont ttf-liberation ttf-ubuntu-font-family

binary is static compiled against libssl1.0 libcrypto1.0 see issue #3

RUN echo ‘http://dl-cdn.alpinelinux.org/alpine/v3.8/main’ >>
/etc/apk/repositories &&
apk —update —no-cache add libssl1.0 libcrypto1.0

on alpine static compiled patched qt headless wkhtmltopdf (47.2 MB)

compilation takes 4 hours on EC2 m1.large in 2016 thats why binary

COPY wkhtmltopdf /bin

I had this problem before. and solve that like below.

wkhtmltopdf

In the above example i had some «src» files and «url»s that they refer to the static directory but the static directory didn’t exist so wkhtmltopdf thrown me that error. for example:

src: url(«file:///home/ehsan/Projects/Example/main/sib/static/WebYekan.eot»);

and one more important thing i have to say is all the file paths in html files have to be an absolute path. do not use relative path at all.

I hope this help you.

OSError: wkhtmltoimage reported an error: 
Loading page (1/2) 
[> ] 0% 
[======> ] 10% 
[==========================> ] 44% 
Warning: Failed to load file:///assets/frappe/css/bootstrap.css (ignore) 
Warning: Failed to load file:///assets/frappe/css/font-awesome.css (ignore) 

Error: Failed to load file:///assets/frappe/css/fonts/fontawesome/fontawesome-webfont.woff?v=4.7.0, with network status code 203 and http status code 0 - 
Error opening /assets/frappe/css/fonts/fontawesome/fontawesome-webfont.woff: No such file or directory 

[============================================================] 100% 

Error: Failed to load file:///assets/frappe/css/fonts/fontawesome/fontawesome-webfont.ttf?v=4.7.0, with network status code 203 and http status code 0 - 
Error opening /assets/frappe/css/fonts/fontawesome/fontawesome-webfont.ttf: No such file or directory 

Error: Failed to load file:///assets/frappe/css/fonts/fontawesome/fontawesome-webfont.svg?v=4.7.0#fontawesomeregular, with network status code 203 and http status code 0 - 
Error opening /assets/frappe/css/fonts/fontawesome/fontawesome-webfont.svg: No such file or directory 

Rendering (2/2) 
[> ] 0% 
[===============> ] 25% 

Warning: Received createRequest signal on a disposed ResourceObject's NetworkAccessManager. This might be an indication of an iframe taking too long to load.
 [============================================================] 100% Done 

Exit with code 1 due to network error: ContentNotFoundError

In my case, I am getting above error, I think this is related with the query string v=4.7.0

Any pointers please?

wkhtmltopdf index.html 7.pdf

Loading pages (1/6)
Warning: Blocked access to file E:/David/company/projects/html/cart/first_test.2/static/js/jquery.js
Warning: Blocked access to file E:/David/company/projects/html/cart/first_test.2/static/js/vue.js
Warning: Blocked access to file E:/David/company/projects/html/cart/first_test.2/static/js/first_part.js
Warning: Blocked access to file E:/David/company/projects/html/cart/first_test.2/static/css/index.css
Error: Failed to load about:blank, with network status code 301 and http status code 0 — Protocol «about» is unknown
Error: Failed to load about:blank, with network status code 301 and http status code 0 — Protocol «about» is unknown
Error: Failed to load about:blank, with network status code 301 and http status code 0 — Protocol «about» is unknown
Warning: Blocked access to file E:/David/company/projects/html/cart/first_test.2/images/logo.png
Error: Failed to load about:blank, with network status code 301 and http status code 0 — Protocol «about» is unknown
Error: Failed to load about:blank, with network status code 301 and http status code 0 — Protocol «about» is unknown
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: ProtocolUnknownError

Hey.
I found a solution for this. i had the same problem and tried different many versions of wkhtmltopdf.
I have a server with nginx + others packages.
Right now for me is working even the last version, 100%.

The solution is super simple, just go to your host file on server and add there the ip of the machine you running also the domain ( 127.0.0.1 example.com ), save the host file and job done.

Is the same problem when you try to run a curl on a page o the same server. the call reach the server, and return a 401 code, since he have problem to identify corectly what he need on server.

This solution work for me perfect 💯

Hope your problems are solved also.
Cheers !

Another thing to check if you’re getting Blocked access to file or other problems with local files (e.g., .js, .css, images), is to explicitly _enable_ local file access (--enable-local-file-access). The default changed as of 0.12.6 to refuse to access local files.

Can someone tell me how to resolve following issues?

  1. wkhtmltopdf don’t have option to pass proxy info (-p or –proxy) unlike in previous versions and its not using system $http_proxy and $https_proxy env variable too.

  2. wkhtmltopdf not working with HTTPS/SSL even though i set LD_LIBRARY_PATH for libssl.so and libcrypto.so

    [deploy@localhost ~]$ wkhtmltopdf https://www.google.co.in google.pdf
    loaded the Generic plugin 
    Loading page (1/2)
    Error: Failed loading page https://www.google.co.in (sometimes it will work just to ignore this error with --load-error-handling ignore)
    Exit with code 1 due to network error: UnknownNetworkError
    

    and

    [deploy@localhost ~]$ wkhtmltoimage https://www.google.co.in sample.jpg
    loaded the Generic plugin 
    Loading page (1/2)
    Error: Failed loading page https://www.google.co.in (sometimes it will work just to ignore this error with --load-error-handling ignore)
    Exit with code 1 due to network error: UnknownNetworkError
    
  3. wkhtmltopdf working partially with HTTP. The output pdf files missing some content/background/positions.

    [deploy@localhost ~]$ wkhtmltopdf http://localhost:8880/ sample.pdf
    loaded the Generic plugin 
    Loading page (1/2)
    Printing pages (2/2)                                               
    Done                                                           
    Exit with code 1 due to network error: ContentNotFoundError
    
    [deploy@localhost ~]$ wkhtmltoimage http://localhost:8880/ sample.jpg
    loaded the Generic plugin 
    Loading page (1/2)
    Rendering (2/2)                                                    
    Done                                                               
    Exit with code 1 due to network error: ContentNotFoundError
    

Note: Im using wkhtmltopdf-0.12.1-1.fc20.x86_64 and qt-4.8.6-10.fc20.x86_64

Answer

Unfortunately wkhtmltopdf doesn’t handle downloading of complex websites, because it’s uses Qt/QtWebKit library which seems to have some issues.

One problem is that wkhtmltopdf doesn’t support relative addresses (GitHub: #1634, #1886, #2359, QTBUG-46240) such as:

<img src="/images/filetypes/txt.png">
<script src="//cdn.optimizely.com/js/653710485.js">

and it loads them as local. One solution which I’ve found to this is the correcting html file in-place by ex in-place editor:

ex -V1 page.html <<-EOF
  %s,'//,'http://,ge 
  %s,"//,"http://,ge 
  %s,'/,'http://www.example.com/,ge
  %s,"/,"http://www.example.com/,ge
  wq " Update changes and quit.
EOF

However it won’t work for files which have these type of URLs on the remote.

Another problem is that it doesn’t handle missing resources. You can try to specify --load-error-handling ignore, but in most cases it doesn’t work (see #2051), so this is still outstanding. Workaround is to simply remove these invalid resources, before conversion.

Alternatively to wkhtmltopdf, you can use either htmldoc, PhantomJS with some additional script, for example using rasterize.js:

phantomjs rasterize.js http://example.com/

or dompdf (HTML to PDF converter for PHP, you can install by composer) with sample code below:

<?php
// somewhere early in your project's loading, require the Composer autoloader
// see: http://getcomposer.org/doc/00-intro.md
$HOMEDIR = "/Users/foo";
require $HOMEDIR . '/.composer/vendor/autoload.php';

// disable DOMPDF's internal autoloader if you are using Composer
define('DOMPDF_ENABLE_AUTOLOAD', FALSE);
define('DOMPDF_ENABLE_REMOTE', TRUE);

// include DOMPDF's default configuration
require_once $HOMEDIR . '/.composer/vendor/dompdf/dompdf/dompdf_config.inc.php';

$htmlString = file_get_contents("https://example.com/foo.pdf");

$dompdf = new DOMPDF();
$dompdf->load_html($htmlString);
$dompdf->render();
$dompdf->stream("sample.pdf");

Attribution
Source : Link , Question Author : Murali Mopuru , Answer Author : kenorb

Cover image for Solve network errors with PDFKit and wkhtmltopdf

Mattia Orfano

In today’s #10stips (where you learn how to solve coding issues within 10 seconds) we talk about the lovely PDFKit gem.

I’ve been using it since I was a child (ehm no.. since the gem was born around 12 years ago). It creates PDFs using plain old HTML+CSS.

PDFKit requires wkhtmltopdf library which renders HTML using Webkit.

Now, Webkit is a rendering engine and depending on your system configuration you may stumble upon compatibility issues loading resources or unsolicited network errors that might drive you crazy, especially when you have HTML pages with lots of images.

PDFKit::ImproperWkhtmltopdfExitStatus UnknownNetworkError
Exit with code 1 due to network error: UnknownNetworkError
Exit with code 1 due to network error: ContentNotFoundError
Warning Failed to load

Enter fullscreen mode

Exit fullscreen mode

Basically, wkhtmltopdf fails to load CSS, JS or IMG files.

There are several factors playing a role…

  • PDFkit 0.13 seems to work, while 0.12.5 does not.
  • loading images from a web server with TLSv1, it works.
  • on a modern server with TLS 1.2, it breaks but not for all resources.
  • better to use absolute paths instead of relative ones
  • who knows, who cares!?!?!?!

So, I give you a quick and final solution.

Ready?

Turn off the computer and go on a hike. Seriously, it helps your mental and physical health.

But if you’re still interested to solve the problem, this is what I suggest…

  1. If possible, update your wkhtmltopdf to the latest version
  2. Check wether external resources in your HTML comes from a TLSv1 or TLSv2 server (newer versions of wkhtmltopdf work best with TLSv2).
  3. Try to switch URLs from HTTPS to HTTP (and viceversa)
  4. Set config.default_options[:ignore_load_errors] = true in your PDFKit.configure block

Obviously, using HTTP versus HTTPS presents a security concern. Unfortunately, we have very few options and I’m a guy who prioritize sanity over bugfixing.

And remember, sometimes it’s OK to pretend bugs are features.

Bugfixing

Leave a thumbs up and comment if you wish.

Recommend Projects

  • React photo

    React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo

    Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo

    Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo

    TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo

    Django

    The Web framework for perfectionists with deadlines.

  • Laravel photo

    Laravel

    A PHP framework for web artisans

  • D3 photo

    D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Visualization

    Some thing interesting about visualization, use data art

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo

    Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo

    Microsoft

    Open source projects and samples from Microsoft.

  • Google photo

    Google

    Google ❤️ Open Source for everyone.

  • Alibaba photo

    Alibaba

    Alibaba Open Source for everyone

  • D3 photo

    D3

    Data-Driven Documents codes.

  • Tencent photo

    Tencent

    China tencent open source team.

Понравилась статья? Поделить с друзьями:
  • Exit status 1 ошибка компиляции для платы arduino mega or mega 2560
  • Exit status 1 ошибка компиляции для платы arduino genuino uno
  • Exit status 0xc0000135 compilation error exit status 0xc0000135
  • Exit fatal error cannot initialize renderer dead island
  • Exit code decimal 2147467259 error sql