feat: pdf
This commit is contained in:
44
pdf/resource.go
Normal file
44
pdf/resource.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package pdf
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/starwalkn/gotenberg-go-client/v8"
|
||||
"github.com/starwalkn/gotenberg-go-client/v8/document"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
gotenberg *gotenberg.Client
|
||||
}
|
||||
|
||||
func New(hostname string) (service Service, err error) {
|
||||
service.gotenberg, err = gotenberg.NewClient(hostname, http.DefaultClient)
|
||||
return
|
||||
}
|
||||
|
||||
func (s Service) HtmlToPdf(html string) (pdf io.ReadCloser, err error) {
|
||||
index, err := document.FromString("index.html", html)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req := gotenberg.NewHTMLRequest(index)
|
||||
req.PaperSize(gotenberg.A4)
|
||||
req.Margins(gotenberg.PageMargins{
|
||||
Top: 0.5,
|
||||
Bottom: 0.5,
|
||||
Left: 0.5,
|
||||
Right: 0.6,
|
||||
Unit: gotenberg.IN,
|
||||
})
|
||||
|
||||
// Skips the IDLE events for faster PDF conversion.
|
||||
req.SkipNetworkIdleEvent(true)
|
||||
req.EmulatePrintMediaType()
|
||||
|
||||
resp, err := s.gotenberg.Send(context.Background(), req)
|
||||
pdf = resp.Body
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user