SebastiaanKlippert / go-wkhtmltopdf

Golang commandline wrapper for wkhtmltopdf
MIT License
1.06k stars 146 forks source link

Exit with code 1 due to network error: ContentNotFoundError #69

Closed sumankr1 closed 3 years ago

sumankr1 commented 3 years ago

I'm trying to run some javascript code inside the script tag. Here is the console error output.

Loading pages (1/6)
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

As soon as I remove JS from the HTML file, the PDF is generated properly.

Here is the JS snippet:

 <script>
      function includeHTML() {
        var z, i, elmnt, file, xhttp;
        /*loop through a collection of all HTML elements:*/
        z = document.getElementsByTagName("*");
        for (i = 0; i < z.length; i++) {
          elmnt = z[i];
          /*search for elements with a certain atrribute:*/
          file = elmnt.getAttribute("w3-include-html");
          if (file) {
            /*make an HTTP request using the attribute value as the file name:*/
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
              if (this.readyState == 4) {
                if (this.status == 200) {
                  elmnt.innerHTML = this.responseText;
                }
                if (this.status == 404) {
                  elmnt.innerHTML = "Page not found.";
                }
                /*remove the attribute, and call this function once more:*/
                elmnt.removeAttribute("w3-include-html");
                includeHTML();
              }
            };
            xhttp.open("GET", file, true);
            xhttp.send();
            /*exit the function:*/
            return;
          }
        }
      }
      includeHTML();
    </script>
SebastiaanKlippert commented 3 years ago

Hi. I think is not an issue in my Go library and this will also happen when you call wkhtmltopdf directly.

But to give you some more context, I think this error is actually correct and your script cannot access a resource you you are trying to load. This can have multiple reeasons, so I would suggestion adding some logging in your loop to see which of your XMLHttpRequests actuallly goes wrong. That is hard to see because you are looping over all files there. Make sure that all files in your HTML document, including javascript and CSS resources have to point to the full path, so use https://<domain>/script.js and not /script.js. But in your case it might just be that your trying to load a file which simply does not exist.

If all else fails you can try to ignore the error by setting page.LoadErrorHandling("ignore") and the ncheck which resource is actually missing in your PDF. https://github.com/SebastiaanKlippert/go-wkhtmltopdf/blob/master/options.go#L81

There are a lot more hints at https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2051but ultimately this seems to be a valid error and not a bug.

sumankr1 commented 3 years ago

Thanks! There was a problem from my side.

TaylorRayHoward commented 2 years ago

Just a note for anyone in the future, you might be able to ignore this error and the pdf still be generated correctly.

I was able to get the bytes even though this error was cropping up for me. In my use-case, I don't particularly care if specific content can't load and LoadErrorHandling("ignore") wasn't working for me. I was able to check the content of the error and ignore it.

err := pdfg.Create()
if !strings.Contains(fmt.Sprint(err), "ContentNotFoundError") {
  return err
}