diff --git a/README.md b/README.md index 764539c..7494325 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ -**⚠️ Not working for Gotenberg >= 7 ⚠️** +**🔥 Working with Gotenberg version 8 and higher! 🔥** # Gotenberg Go client -A simple Go client for interacting with a Gotenberg API. +A simple Go client for interacting with a Gotenberg API (forked github.com/thecodingmachine/gotenberg-go-client/v7). ## Install ```bash -$ go get -u github.com/thecodingmachine/gotenberg-go-client/v7 +$ go get -u github.com/dcaraxes/gotenberg-go-client/v8 ``` ## Usage @@ -17,7 +17,7 @@ import ( "time" "net/http" - "github.com/thecodingmachine/gotenberg-go-client/v7" + "github.com/dcaraxes/gotenberg-go-client/v8" ) // create the client. @@ -49,6 +49,7 @@ req.Assets(style, img) req.PaperSize(gotenberg.A4) req.Margins(gotenberg.NoMargins) req.Scale(0.75) +req.SkipNetworkIdleEvent() // for higher PDF generation speed // store method allows you to... store the resulting PDF in a particular destination. client.Store(req, "path/you/want/the/pdf/to/be/stored.pdf") diff --git a/chrome.go b/chrome.go index d8de6dd..a534e6b 100644 --- a/chrome.go +++ b/chrome.go @@ -18,6 +18,7 @@ const ( googleChromeRpccBufferSize string = "googleChromeRpccBufferSize" scale string = "scale" skipNetworkIdleEvent string = "skipNetworkIdleEvent" + singlePage string = "singlePage" ) // nolint: gochecknoglobals @@ -104,11 +105,17 @@ func (req *chromeRequest) GoogleChromeRpccBufferSize(bufferSize int64) { req.values[googleChromeRpccBufferSize] = strconv.FormatInt(bufferSize, 10) } -// Scale sets scale form field +// Scale sets scale form field. func (req *chromeRequest) Scale(scaleFactor float64) { req.values[scale] = fmt.Sprintf("%f", scaleFactor) } +// SkipNetworkIdleEvent sets skipNetworkIdleEvent form field as true. func (req *chromeRequest) SkipNetworkIdleEvent() { req.values[skipNetworkIdleEvent] = "true" } + +// SinglePage sets singlePage form field as true. +func (req *chromeRequest) SinglePage() { + req.values[singlePage] = "true" +} diff --git a/client.go b/client.go index 6257f0c..0282a34 100644 --- a/client.go +++ b/client.go @@ -34,6 +34,7 @@ type Client struct { // the Gotenberg API. type Request interface { postURL() string + screenshotURL() string customHTTPHeaders() map[string]string formValues() map[string]string formFiles() map[string]Document @@ -91,6 +92,10 @@ func (c *Client) Post(req Request) (*http.Response, error) { return c.PostContext(context.Background(), req) } +func (c *Client) Screenshot(req Request) (*http.Response, error) { + return c.ScreenshotContext(context.Background(), req) +} + // PostContext sends a request to the Gotenberg API // and returns the response. // The created HTTP request can be canceled by the passed context. @@ -118,6 +123,33 @@ func (c *Client) PostContext(ctx context.Context, req Request) (*http.Response, return resp, nil } +// ScreenshotContext sends a request to the Gotenberg API +// and returns the response. +// The created HTTP request can be canceled by the passed context. +func (c *Client) ScreenshotContext(ctx context.Context, req Request) (*http.Response, error) { + body, contentType, err := multipartForm(req) + if err != nil { + return nil, err + } + if c.HTTPClient == nil { + c.HTTPClient = &http.Client{} + } + URL := fmt.Sprintf("%s%s", c.Hostname, req.screenshotURL()) + httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, URL, body) + if err != nil { + return nil, err + } + httpReq.Header.Set("Content-Type", contentType) + for key, value := range req.customHTTPHeaders() { + httpReq.Header.Set(key, value) + } + resp, err := c.HTTPClient.Do(httpReq) /* #nosec */ + if err != nil { + return nil, err + } + return resp, nil +} + // Store creates the resulting PDF to given destination. func (c *Client) Store(req Request, dest string) error { return c.StoreContext(context.Background(), req, dest) diff --git a/html.go b/html.go index 0cf18a7..ac7f3ee 100644 --- a/html.go +++ b/html.go @@ -23,6 +23,10 @@ func (req *HTMLRequest) postURL() string { return "/convert/html" } +func (req *HTMLRequest) screenshotURL() string { + return "/screenshot/html" +} + func (req *HTMLRequest) formFiles() map[string]Document { files := make(map[string]Document) files["index.html"] = req.index