Skip to content

Commit

Permalink
feat: added screenshots feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Pikeev committed Mar 23, 2024
1 parent 066a77d commit 8bf5058
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 8 additions & 1 deletion chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
googleChromeRpccBufferSize string = "googleChromeRpccBufferSize"
scale string = "scale"
skipNetworkIdleEvent string = "skipNetworkIdleEvent"
singlePage string = "singlePage"
)

// nolint: gochecknoglobals
Expand Down Expand Up @@ -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"
}
32 changes: 32 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8bf5058

Please sign in to comment.