Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error failed to load... network status 301 #117

Open
samuelhgf opened this issue Aug 10, 2023 · 8 comments
Open

Error failed to load... network status 301 #117

samuelhgf opened this issue Aug 10, 2023 · 8 comments

Comments

@samuelhgf
Copy link

Error probably when try to load assets in html

Please, I need help with this. I'm struggling to make this work with GoLang. The same link I was able to work using Lambda Docker with Python. But I would like to do not add this layer to my Go app. If I don't managed this, I'll have to send this saving process to this Lambda :'(

Error output

neofin-save-danfe  | 2023/08/10 23:38:08 Error: Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown
neofin-save-danfe  | Error: Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown
neofin-save-danfe  | Error: Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown
neofin-save-danfe  | Error: Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown
neofin-save-danfe  | Exit with code 1 due to network error: ProtocolUnknownError
neofin-save-danfe exited with code 1

Dockerfile

FROM surnet/alpine-wkhtmltopdf:3.16.2-0.12.6-full as wkhtmltopdf
FROM alpine

RUN apk update && apk add --no-cache \
    curl \
    jq \
    libstdc++ \
    libx11 \
    libxrender \
    libxext \
    libssl1.1 \
    ca-certificates \
    fontconfig \
    freetype \
    ttf-dejavu \
    ttf-droid \
    ttf-freefont \
    ttf-liberation \
    # more fonts
    && apk add --no-cache --virtual .build-deps \
    msttcorefonts-installer \
    # Install microsoft fonts
    && update-ms-fonts \
    && fc-cache -f \
    # Clean up when done
    && rm -rf /tmp/* \
    && apk del .build-deps

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /app/main /main

COPY --from=wkhtmltopdf /bin/wkhtmltopdf /bin/wkhtmltopdf
COPY --from=wkhtmltopdf /bin/wkhtmltoimage /bin/wkhtmltoimage
COPY --from=wkhtmltopdf /bin/libwkhtmltox* /bin/

# Set the entry point for the container
CMD ["/main"]

Code

pdfg, err := pdf.NewPDFGenerator()
	if err != nil {
		log.Fatal(err)
	}
	pdfg.LogLevel.Set("error")

	pdfg.Dpi.Set(300)

	fileName := "NFSe"
	buf := new(bytes.Buffer)
	for _, danfe := range parsedMsg.Danfes {
		resp, err := http.Get(danfe.Url)
		if err != nil {
			log.Fatal(err)
		}

		body, err := io.ReadAll(resp.Body)

		buf.Write(body)
		buf.WriteString(`<P style="page-break-before: always">`)

		fileName += fmt.Sprintf("_%s", danfe.Url)
	}

	page := pdf.NewPageReader(buf)
	//page.EnableLocalFileAccess.Set(true)

	pdfg.AddPage(page)

	err = pdfg.Create()
	if err != nil {
		log.Fatal(err)
	}

	fileName += ".pdf"

	filePathLocal, _ := filepath.Abs("/tmp/" + fileName)

	// Write buffer contents to file on disk
	err = pdfg.WriteFile(filePathLocal)
	if err != nil {
		log.Fatal(err)
	}
@samuelhgf
Copy link
Author

If i run from inside the container wkhtmltopdf "url to page" output.pdf it works

@SebastiaanKlippert
Copy link
Owner

SebastiaanKlippert commented Aug 11, 2023

I see that you have page.EnableLocalFileAccess.Set(true) commented out.
Does it give the same error with that enabled? This issue #55 was fixed with EnableLocalFileAccess
Maybe this helps wkhtmltopdf/wkhtmltopdf#4867

@samuelhgf
Copy link
Author

@SebastiaanKlippert Thanks for the quickly reply.
When I set page.EnableLocalFileAccess.Set(true) this is the output

neofin-save-danfe  | 2023/08/11 13:41:06 Error: Failed to load file:///tmp/notaprintimg.aspx?ccm=67602118&nf=16134&cod=BTLJXBFU&imprimir=1, with network status code 203 and http status code 0 - Error opening /tmp/notaprintimg.aspx: No such file or directory
neofin-save-danfe  | Error: Failed to load file:///tmp/notaprintimg.aspx?ccm=67602118&nf=16135&cod=PENU5ZSG&imprimir=1, with network status code 203 and http status code 0 - Error opening /tmp/notaprintimg.aspx: No such file or directory
neofin-save-danfe  | Exit with code 1 due to network error: ContentNotFoundError
neofin-save-danfe exited with code 1

@SebastiaanKlippert
Copy link
Owner

SebastiaanKlippert commented Aug 11, 2023

That looks more like the actual error. It is trying to access you local filesystem but that throws that about:blank error if you don't enable filesystem access.

I don't know what the content of your HTML is and what the content of the URLs in parsedMsg.Danfes is ,but I expect they just refer to /tmp/notaprintimg.aspx. Are you sure that file exists and is accessible in your container?

I cannot debug remotely, but if you just try the same code with simple HTML input it should work, then try to add a reference in that HTML to a local file and check if it still works.

@samuelhgf
Copy link
Author

Unfortunately I can't share the link because it's private. I'm trying to get clearance to do it.

But I can say to you that the HTML code of the link has it's files loaded using relative path. Ex:
<link href="../nfe.css?v=1.1" type="text/css" rel="stylesheet">

@SebastiaanKlippert
Copy link
Owner

I don't need the link or file, keep it private.

But yes that is a known bug in wkhtmltopdf itself, it will not be fixed there.
I thought you linked to /tmp on purpose. See #28 (comment) for more info. It is related to wkhtmltopdf/wkhtmltopdf#3014

You don't have to read all that, but the summary is that when using stdin in wkhtmltopdf (that is what passing it as a reader in Go does) the working directory is set to your systems TEMP dir.

As a workaround you can set absolute paths (that is always better). If you have no control over the source HTML you can replace the links in your Go code (replace href="../ with href="/actual_path/). You can get the working dir from your Go code.
Or you can set TMP or TEMP environment variables to your Go working dir in your container before you run the Go program, but not sure if I tested that. Setting it from Go does not work in any case, or I could have fixed it myself.

@samuelhgf
Copy link
Author

@SebastiaanKlippert But this will only working with local html files, or am I wrong ?

I understand that this fix when I have a local HTML file calling another files with relative path. In my case it's a like that in the HTML is calling files with ../

@SebastiaanKlippert
Copy link
Owner

Not sure what you mean, your main HTML file is loaded into memory and that is passed to wkhtmltopdf via stdin. That file cannot load resources with a relative file path (so the HTML cannot contain links to relative files).
So you either need to replace the relative paths in your HTML with absolute paths, or just store the main HTML on disk os.WriteFile("/path/index.html", buf.Bytes(), 0666) and render it using pdfg.AddPage("/path/index.html").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants