【发布时间】:2022-12-27 17:01:02
【问题描述】:
After trying lot of solutions for this error, I am posting this issue here. I have written a method which converts html to pdf and returns pdf bytes as output.
import (
"bytes"
"github.com/SebastiaanKlippert/go-wkhtmltopdf"
)
func HtmlToPdf(htmlData *string) ([]byte, error) {
pdfg, err := wkhtmltopdf.NewPDFGenerator()
if err != nil {
return nil, err
}
pdfg.AddPage(wkhtmltopdf.NewPageReader(bytes.NewReader([]byte(*htmlData))))
//nolint: gomnd
pdfg.Dpi.Set(600)
jb, err := pdfg.ToJSON()
if err != nil {
return nil, err
}
pdfgFromJSON, err := wkhtmltopdf.NewPDFGeneratorFromJSON(bytes.NewReader(jb))
if err != nil {
return nil, err
}
err = pdfgFromJSON.Create()
if err != nil {
return nil, err
}
pdfBytes := pdfgFromJSON.Bytes()
return pdfBytes, nil }
Calling this method returns errorwkhtmltopdf not foundI have tried the following solutions
-
which wkhtmltopdf /usr/local/bin/wkhtmltopdf and then setting the WKHTMLTOPDF_PATH: /usr/local/bin/wkhtmltopdf in environment section of my code
-
Using setPath at the top of HtmlToPdf Method like
wkhtmltopdf.SetPath("/usr/local/bin/wkhtmltopdf")
In this case the error changes to fork/exec /usr/local/bin/wkhtmltopdf: no such file or directory
-
Also tried after moving the wkhtml files to /usr/local/go/bin/ and using path /usr/local/go/bin/wkhtmltopdf
-
Converting any url to pdf using command line also works fine.
Note : Hitting wkhtmltopdf --version in terminal gives wkhtmltopdf 0.12.6 (with patched qt) and package is installed using go get github.com/SebastiaanKlippert/go-wkhtmltopdf
Any other solutions?
-
【问题讨论】:
-
instead of setting
PATHas/usr/local/go/bin/wkhtmltopdftry setting it as/usr/local/go/bin -
note that WKHTMLTOPDF_PATH should point to directory not executable
-
Pointed the WKHTMLTOPDF_PATH to /usr/local/bin and printed the final path which is /usr/local/bin/wkhtmltopdf . File is present at this path still my method is returning the same error wkhtmltopdf not found
标签: go wkhtmltopdf