SebastiaanKlippert / go-wkhtmltopdf

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

Create() returning not nil error on valid html #120

Closed Skillozone closed 11 months ago

Skillozone commented 1 year ago

Hi, I'm facing this issue once in a while and I don't know why. When I'm using the Create() function it returns an error like this:

Loading pages (1/6) [> ] 0% [======> ] 10% [=========> ] 15% [=========> ] 15% [=========> ] 16% [=========> ] 16% [=========> ] 16% [==========> ] 17% [==========> ] 17% [==========> ] 17% [==========> ] 18% [===========> ] 19% [============> ] 20% [============> ] 21% [=============> ] 23% [===============> ] 26% [================> ] 28% [==================> ] 30% [===================> ] 32% [============================================================] 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 30 [===> ] Page 2 of 30 [======> ] Page 3 of 30 [=======> ] Page 4 of 30 [=========> ] Page 5 of 30 [============> ] Page 6 of 30 [=============> ] Page 7 of 30 [===============> ] Page 8 of 30 [==================> ] Page 9 of 30 [===================> ] Page 10 of 30 [=====================> ] Page 11 of 30 [========================> ] Page 12 of 30 [=========================> ] Page 13 of 30 [===========================> ] Page 14 of 30 [==============================> ] Page 15 of 30 [===============================> ] Page 16 of 30 [=================================> ] Page 17 of 30 [====================================> ] Page 18 of 30 [=====================================> ] Page 19 of 30 [=======================================> ] Page 20 of 30 [==========================================> ] Page 21 of 30 [===========================================> ] Page 22 of 30 [=============================================> ] Page 23 of 30 [================================================> ] Page 24 of 30 [=================================================> ] Page 25 of 30 [===================================================> ] Page 26 of 30 [======================================================> ] Page 27 of 30 [=======================================================> ] Page 28 of 30 [=========================================================> ] Page 29 of 30 [============================================================] Page 30 of 30 Done

However I don't see any indications on what caused the error here. And I am not able to reproduce the error, when I take the same html that caused the error and try to convert it to pdf it works. I feel like this error should not happen.

Thank you for your work

SebastiaanKlippert commented 1 year ago

Hi,

Do you use method SetStderr? See https://pkg.go.dev/github.com/SebastiaanKlippert/go-wkhtmltopdf#PDFGenerator.SetStderr Can you try use SetStderr if you don't? You can create a buffer to write to.

It does not really look like there was an error from this log, it is just the content of Stderr, but that means that somehow the underlying Command did not return correctly. But I think it would be hard to figure out why if it only happens once in a while. I have never seen it myself.

To provide some more context, can you give me the version of wkhtmltopdf you are using and which OS this is on? And does the PDF look correct when this happens?

SebastiaanKlippert commented 1 year ago

Upon further inspection I think that you might have found a bug here where the actual error is discarded and only the contents of stderr are displayed. In those cases it is probably not an error from wkhtmltopdf, but from the command itself...

I will create a fix in a couple of hours that will include the actual error, that should give you more information of the actual error.

SebastiaanKlippert commented 1 year ago

I have created a pre-release, can you please manually point your go.mod to use v1.9.2? https://github.com/SebastiaanKlippert/go-wkhtmltopdf/releases/tag/v1.9.2

This should append the error to the end of the output. It might be ideal, but it was the only way I could think of to not include a breaking change and keep all the error details. I marked it as a pre-release for now because I need to think about a way to test this better, but it should not break anything.

Skillozone commented 1 year ago

I was not using method SetStderr. I was using wkhtmltopdf v1.9.0 on a docker image on Alpine OS. I don't know if the pdf looked correct when the error happened because I was returning an error with no PDF if an error happened in the PDF creation. Only thing I know is that with the same HTML that caused this error I was able to convert it successfully to PDF when I tried manually.

I just pulled your pre-released version 1.9.2 and I will let you know as soon as the error appears again, I have to wait for someone causing the error again because I'm not able to reproduce the error manually.

Thank you for being so reactive !

SebastiaanKlippert commented 1 year ago

No problem, thanks for reporting it.

Skillozone commented 1 year ago

The error happened again and the log is now a bit more extensive but I'm not sure to understand what's the problem here:

Loading pages (1/6) [> ] 0% [======> ] 10% [=======> ] 13% [========> ] 14% [========> ] 14% [========> ] 14% [========> ] 14% [========> ] 14% [========> ] 14% [========> ] 14% [========> ] 14% [=========> ] 15% [=========> ] 15% [=========> ] 15% [=========> ] 15% [=========> ] 15% [=========> ] 15% [=========> ] 15% [=========> ] 16% [=========> ] 16% [=========> ] 16% [=========> ] 16% [=========> ] 16% [=========> ] 16% [=========> ] 16% [==========> ] 17% [==========> ] 17% [==========> ] 17% [==========> ] 17% [==========> ] 18% [==========> ] 18% [==========> ] 18% [===========> ] 19% [===========> ] 19% [===========> ] 19% [===========> ] 19% [============> ] 20% [============> ] 20% [============> ] 21% [============> ] 21% [============> ] 21% [=============> ] 22% [=============> ] 23% [=============> ] 23% [==============> ] 24% [==============> ] 24% [===============> ] 25% [===============> ] 26% [================> ] 28% [==================> ] 31% [====================> ] 34% [======================> ] 37% [============================================================] 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 11 [==========> ] Page 2 of 11 [================> ] Page 3 of 11 [=====================> ] Page 4 of 11 [===========================> ] Page 5 of 11 [================================> ] Page 6 of 11 [=====================================> ] Page 7 of 11 [===========================================> ] Page 8 of 11 [================================================> ] Page 9 of 11 [======================================================> ] Page 10 of 11 [============================================================] Page 11 of 11 Done

write tcp 10.12.1.228:8080->10.12.9.11:38386: write: broken pipe

SebastiaanKlippert commented 1 year ago

Interesting.... At least the error appending works, but no idea what this error is. A broken pipe error is usually an indication of the server closing the connection. Do these 10.12 IPs mean anything or are they just internal Docker networks? The error does look a lot like a Go error and not so much from from wkhtmltopdf itself, but I might be wrong there.

It definitely is a TCP error, but no idea why and where that happens.

Two questions: 1) Would it be possible to share the contents of the HTML even if the error happens? It looks like wkhtmltopdf has completely rendered the PDF so I wonder what the output of Bytes() is. 2) Do you mind sharing your code?

Skillozone commented 1 year ago
  1. I have no idea why a TCP error happens here. I added a more detailled log to retrieve the entire HTML when the error happens, I'll let you know as soon as the error happen again.

  2. The code block where I use wkhtmltopdf v1.9.2 is the following:


import (...
wkhtml "github.com/SebastiaanKlippert/go-wkhtmltopdf"
...) 

func GeneratePdf(id int, db *sqlx.DB, writer io.Writer) (string, error) {

    fileName := fmt.Sprintf("%d_mypdfname.pdf", id)
    pdfg, err := wkhtml.NewPDFGenerator()
    if err != nil {
        return fileName, fmt.Errorf("an error occured while creating pdf generator : %w", err)
    }
    htmlContent, err := GetDataPDFTemplateAsString(id, db)
    if err != nil {
        return fileName, fmt.Errorf("an error occured while getting html template : %w", err)
    }
    pdfg.AddPage(wkhtml.NewPageReader(strings.NewReader(htmlContent)))
    pdfg.SetOutput(writer)
    err = pdfg.Create()
    if err != nil {
        return "", fmt.Errorf("an error occured while creating pdf : %w with html : %s", err, htmlContent)
    }
    return fileName, nil
}
SebastiaanKlippert commented 1 year ago

Thanks for your updates. The Go code looks fine. But what do you pass as writer into this function? Is it local, like a buffer or something that goes over network? If that is on your network it might be that that error is related to that.

Skillozone commented 1 year ago

It is something that goes over the network as an answer to an API call. You might be right, the error might not come from wkhtmltopdf itself but I don't really understand why it's happening. However the error didn't happen since 2 weeks now but I've changed nothing except the error log. I had it one time or two times a day before that, I feel confused.

SebastiaanKlippert commented 1 year ago

I understand the confusion. I am pretty sure that that what we are seeing now as error, is an error when writing to the contents of the PDF to your network. I don;t know why that happens as you will understand. But we did find a bug where the error was not printed correctly, so for my package that helps. I will mark 1.9.2 as latest release, I am sure that fix will help others to debug errors.

Let me know if there is anything else.

Skillozone commented 12 months ago

Thank you !