From 729b91e965d725f6e01897ca69936bbb4052f31c Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Mon, 9 Dec 2019 17:01:51 +0100 Subject: [PATCH 1/8] allow custom HTTP client --- client.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index b3e1658..1524693 100644 --- a/client.go +++ b/client.go @@ -23,7 +23,8 @@ const ( // Client facilitates interacting with // the Gotenberg API. type Client struct { - Hostname string + Hostname string + HTTPClient *http.Client } // Request is a type for sending @@ -76,8 +77,11 @@ func (c *Client) Post(req Request) (*http.Response, error) { if err != nil { return nil, err } + if c.HTTPClient == nil { + c.HTTPClient = &http.Client{} + } URL := fmt.Sprintf("%s%s", c.Hostname, req.postURL()) - resp, err := http.Post(URL, contentType, body) /* #nosec */ + resp, err := c.HTTPClient.Post(URL, contentType, body) /* #nosec */ if err != nil { return nil, err } From 2a855bb66b3169cce4870b000d17dc5ebd75a2b5 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Mon, 9 Dec 2019 17:25:16 +0100 Subject: [PATCH 2/8] adding support for custom HTTP headers --- client.go | 36 +++++++++++++++++++++++++++++------- url.go | 13 ++++++++++++- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index 1524693..2600033 100644 --- a/client.go +++ b/client.go @@ -14,10 +14,11 @@ import ( ) const ( - resultFilename string = "resultFilename" - waitTimeout string = "waitTimeout" - webhookURL string = "webhookURL" - webhookURLTimeout string = "webhookURLTimeout" + resultFilename string = "resultFilename" + waitTimeout string = "waitTimeout" + webhookURL string = "webhookURL" + webhookURLTimeout string = "webhookURLTimeout" + webhookURLBaseHTTPHeaderKey string = "Gotenberg-Webhookurl-" ) // Client facilitates interacting with @@ -32,17 +33,20 @@ type Client struct { // the Gotenberg API. type Request interface { postURL() string + customHTTPHeaders() map[string]string formValues() map[string]string formFiles() map[string]string } type request struct { - values map[string]string + httpHeaders map[string]string + values map[string]string } func newRequest() *request { return &request{ - values: make(map[string]string), + httpHeaders: make(map[string]string), + values: make(map[string]string), } } @@ -66,6 +70,16 @@ func (req *request) WebhookURLTimeout(timeout float64) { req.values[webhookURLTimeout] = strconv.FormatFloat(timeout, 'f', 2, 64) } +// AddWebhookURLHTTPHeader add a webhook custom HTTP header. +func (req *request) AddWebhookURLHTTPHeader(key, value string) { + key = fmt.Sprintf("%s%s", webhookURLBaseHTTPHeaderKey, key) + req.httpHeaders[key] = value +} + +func (req *request) customHTTPHeaders() map[string]string { + return req.httpHeaders +} + func (req *request) formValues() map[string]string { return req.values } @@ -81,7 +95,15 @@ func (c *Client) Post(req Request) (*http.Response, error) { c.HTTPClient = &http.Client{} } URL := fmt.Sprintf("%s%s", c.Hostname, req.postURL()) - resp, err := c.HTTPClient.Post(URL, contentType, body) /* #nosec */ + httpReq, err := http.NewRequest(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 } diff --git a/url.go b/url.go index ef52bbc..67381cd 100644 --- a/url.go +++ b/url.go @@ -1,6 +1,11 @@ package gotenberg -const remoteURL string = "remoteURL" +import "fmt" + +const ( + remoteURL string = "remoteURL" + remoteURLBaseHTTPHeaderKey string = "Gotenberg-Remoteurl-" +) // URLRequest facilitates remote URL conversion // with the Gotenberg API. @@ -19,6 +24,12 @@ func (url *URLRequest) postURL() string { return "/convert/url" } +// AddRemoteURLHTTPHeader add a remote URL custom HTTP header. +func (url *URLRequest) AddRemoteURLHTTPHeader(key, value string) { + key = fmt.Sprintf("%s%s", remoteURLBaseHTTPHeaderKey, key) + url.httpHeaders[key] = value +} + // Compile-time checks to ensure type implements desired interfaces. var ( _ = Request(new(URLRequest)) From 1eae541f7caf5f0424f313c5219f2bffd08d9fa7 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Mon, 9 Dec 2019 17:25:36 +0100 Subject: [PATCH 3/8] updating golangci lint --- Makefile | 2 +- build/lint/Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 17640b3..351d4b7 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ GOLANG_VERSION=1.13 GOTENBERG_VERSION=6.0.1 VERSION=snapshot -GOLANGCI_LINT_VERSION=1.19.1 +GOLANGCI_LINT_VERSION=1.20.1 # gofmt and goimports all go files. fmt: diff --git a/build/lint/Dockerfile b/build/lint/Dockerfile index 61c2547..6d5674e 100644 --- a/build/lint/Dockerfile +++ b/build/lint/Dockerfile @@ -32,4 +32,4 @@ COPY go.sum . # Install module dependencies. RUN go mod download -CMD [ "golangci-lint", "run" ,"--tests=false", "--enable-all", "--disable=dupl" ] \ No newline at end of file +CMD [ "golangci-lint", "run" ,"--tests=false", "--enable-all", "--disable=dupl", "--disable=wsl" ] \ No newline at end of file From 0eebbf618e25c5409581e653ba7b58deeffed3bf Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Mon, 9 Dec 2019 17:50:58 +0100 Subject: [PATCH 4/8] adding support for custom http headers + custom http client --- client.go | 2 +- html_test.go | 12 ++++++++++++ markdown_test.go | 17 +++++++++++++++++ merge_test.go | 15 +++++++++++++++ office_test.go | 12 ++++++++++++ url_test.go | 12 ++++++++++++ 6 files changed, 69 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 2600033..9608330 100644 --- a/client.go +++ b/client.go @@ -55,7 +55,7 @@ func (req *request) ResultFilename(filename string) { req.values[resultFilename] = filename } -// WaitTiemout sets waitTimeout form field. +// WaitTimeout sets waitTimeout form field. func (req *request) WaitTimeout(timeout float64) { req.values[waitTimeout] = strconv.FormatFloat(timeout, 'f', 2, 64) } diff --git a/html_test.go b/html_test.go index 652382e..0c39c22 100644 --- a/html_test.go +++ b/html_test.go @@ -58,3 +58,15 @@ func TestHTMLWithoutHeaderFooter(t *testing.T) { err = os.RemoveAll(dirPath) assert.Nil(t, err) } + +func TestHTMLWebhook(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + req, err := NewHTMLRequest(test.HTMLTestFilePath(t, "index.html")) + require.Nil(t, err) + req.WebhookURL("https://google.com") + req.WebhookURLTimeout(5.0) + req.AddWebhookURLHTTPHeader("A-Header", "Foo") + resp, err := c.Post(req) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) +} diff --git a/markdown_test.go b/markdown_test.go index 40c1ce2..bbfc246 100644 --- a/markdown_test.go +++ b/markdown_test.go @@ -68,3 +68,20 @@ func TestMarkdownWithoutHeaderFooter(t *testing.T) { err = os.RemoveAll(dirPath) assert.Nil(t, err) } + +func TestMarkdownWebhook(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + req, err := NewMarkdownRequest( + test.MarkdownTestFilePath(t, "index.html"), + test.MarkdownTestFilePath(t, "paragraph1.md"), + test.MarkdownTestFilePath(t, "paragraph2.md"), + test.MarkdownTestFilePath(t, "paragraph3.md"), + ) + require.Nil(t, err) + req.WebhookURL("https://google.com") + req.WebhookURLTimeout(5.0) + req.AddWebhookURLHTTPHeader("A-Header", "Foo") + resp, err := c.Post(req) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) +} diff --git a/merge_test.go b/merge_test.go index 36c621d..86c20e4 100644 --- a/merge_test.go +++ b/merge_test.go @@ -28,3 +28,18 @@ func TestMerge(t *testing.T) { err = os.RemoveAll(dirPath) assert.Nil(t, err) } + +func TestMergeWebhook(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + req, err := NewMergeRequest( + test.PDFTestFilePath(t, "gotenberg.pdf"), + test.PDFTestFilePath(t, "gotenberg.pdf"), + ) + require.Nil(t, err) + req.WebhookURL("https://google.com") + req.WebhookURLTimeout(5.0) + req.AddWebhookURLHTTPHeader("A-Header", "Foo") + resp, err := c.Post(req) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) +} diff --git a/office_test.go b/office_test.go index b83bc6c..e098e28 100644 --- a/office_test.go +++ b/office_test.go @@ -26,3 +26,15 @@ func TestOffice(t *testing.T) { err = os.RemoveAll(dirPath) assert.Nil(t, err) } + +func TestOfficeWebhook(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + req, err := NewOfficeRequest(test.OfficeTestFilePath(t, "document.docx")) + require.Nil(t, err) + req.WebhookURL("https://google.com") + req.WebhookURLTimeout(5.0) + req.AddWebhookURLHTTPHeader("A-Header", "Foo") + resp, err := c.Post(req) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) +} diff --git a/url_test.go b/url_test.go index 10e511c..be1a703 100644 --- a/url_test.go +++ b/url_test.go @@ -16,6 +16,7 @@ func TestURL(t *testing.T) { req.ResultFilename("foo.pdf") req.WaitTimeout(5) req.WaitDelay(1) + req.AddRemoteURLHTTPHeader("A-Header", "Foo") err := req.Header(test.URLTestFilePath(t, "header.html")) require.Nil(t, err) err = req.Footer(test.URLTestFilePath(t, "footer.html")) @@ -50,3 +51,14 @@ func TestURLWithoutHeaderFooter(t *testing.T) { err = os.RemoveAll(dirPath) assert.Nil(t, err) } + +func TestURLWebhook(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + req := NewURLRequest("http://google.com") + req.WebhookURL("https://google.com") + req.WebhookURLTimeout(5.0) + req.AddWebhookURLHTTPHeader("A-Header", "Foo") + resp, err := c.Post(req) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) +} From e2e579a5ddb257d3e17765603d8dfa7101d88fd9 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Mon, 9 Dec 2019 17:51:10 +0100 Subject: [PATCH 5/8] updating tests Dockerfile --- Makefile | 5 +++-- build/tests/Dockerfile | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 351d4b7..a2ac492 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ GOLANG_VERSION=1.13 -GOTENBERG_VERSION=6.0.1 +GOTENBERG_VERSION=6 +GOTENBERG_LOG_LEVEL=DEBUG VERSION=snapshot GOLANGCI_LINT_VERSION=1.20.1 @@ -15,5 +16,5 @@ lint: # run all tests. tests: - docker build --build-arg GOLANG_VERSION=$(GOLANG_VERSION) --build-arg GOTENBERG_VERSION=$(GOTENBERG_VERSION) -t thecodingmachine/gotenberg-go-client:tests -f build/tests/Dockerfile . + docker build --build-arg GOLANG_VERSION=$(GOLANG_VERSION) --build-arg GOTENBERG_VERSION=$(GOTENBERG_VERSION) --build-arg GOTENBERG_LOG_LEVEL=$(GOTENBERG_LOG_LEVEL) -t thecodingmachine/gotenberg-go-client:tests -f build/tests/Dockerfile . docker run --rm -it -v "$(PWD):/tests" thecodingmachine/gotenberg-go-client:tests \ No newline at end of file diff --git a/build/tests/Dockerfile b/build/tests/Dockerfile index 6adf701..b7fec42 100644 --- a/build/tests/Dockerfile +++ b/build/tests/Dockerfile @@ -14,7 +14,8 @@ USER root # | Libraries used in the build process of this image. # | -RUN apt-get install -y git gcc +RUN apt-get update &&\ + apt-get install -y git gcc # |-------------------------------------------------------------------------- # | Golang @@ -38,6 +39,10 @@ ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH # | Last instructions of this build. # | +ARG GOTENBERG_LOG_LEVEL + +ENV LOG_LEVEL=${GOTENBERG_LOG_LEVEL} + # Define our workding outside of $GOPATH (we're using go modules). WORKDIR /tests From d71235807c1e4c00144343cf14ffb544b726bc71 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Tue, 10 Dec 2019 11:53:27 +0100 Subject: [PATCH 6/8] bc: document may now be from fpath, string, or byte array --- Makefile | 2 +- README.md | 61 ++++++++++++------ build/tests/docker-entrypoint.sh | 2 +- chrome.go | 34 +++++------ client.go | 14 ++--- document.go | 102 +++++++++++++++++++++++++++++++ go.mod | 7 +-- go.sum | 2 - html.go | 38 ++++-------- html_test.go | 69 +++++++++++++++------ markdown.go | 49 +++++---------- markdown_test.go | 72 +++++++++++----------- merge.go | 24 +++----- merge_test.go | 18 +++--- office.go | 21 +++---- office_test.go | 8 ++- url_test.go | 25 ++++---- 17 files changed, 318 insertions(+), 230 deletions(-) create mode 100644 document.go diff --git a/Makefile b/Makefile index a2ac492..5c9cb07 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ GOLANG_VERSION=1.13 GOTENBERG_VERSION=6 -GOTENBERG_LOG_LEVEL=DEBUG +GOTENBERG_LOG_LEVEL=ERROR VERSION=snapshot GOLANGCI_LINT_VERSION=1.20.1 diff --git a/README.md b/README.md index b62e31a..4040429 100644 --- a/README.md +++ b/README.md @@ -5,36 +5,57 @@ A simple Go client for interacting with a Gotenberg API. ## Install ```bash -$ go get -u github.com/thecodingmachine/gotenberg-go-client/v6 +$ go get -u github.com/thecodingmachine/gotenberg-go-client/v7 ``` ## Usage ```golang -import "github.com/thecodingmachine/gotenberg-go-client/v6" - -func main() { - // HTML conversion example. - c := &gotenberg.Client{Hostname: "http://localhost:3000"} - req, _ := gotenberg.NewHTMLRequest("index.html") - req.Header("header.html") - req.Footer("footer.html") - req.Assets( - "font.woff", - "img.gif", - "style.css", - ) - req.PaperSize(gotenberg.A4) - req.Margins(gotenberg.NormalMargins) - req.Landscape(false) - dest := "foo.pdf" - c.Store(req, dest) +import ( + "time" + "net/http" + + "github.com/thecodingmachine/gotenberg-go-client/v7" +) + +// create the client. +client := &gotenberg.Client{Hostname: "http://localhost:3000"} +// ... or use your own *http.Client. +httpClient := &http.Client{ + Timeout: time.Duration(5) * time.Second, } +client := &gotenberg.Client{Hostname: "http://localhost:3000", HTTPClient: httpClient} + +// prepare the files required for your conversion. + +// from a path. +index, _ := gotenberg.NewDocumentFromPath("index.html", "/path/to/file") +// ... or from a string. +index, _ := gotenberg.NewDocumentFromString("index.html", "Foo") +// ... or from bytes. +index, _ := gotenberg.NewDocumentFromBytes("index.html", []byte("Foo")) + +header, _ := gotenberg.NewDocumentFromPath("header.html", "/path/to/file") +footer, _ := gotenberg.NewDocumentFromPath("footer.html", "/path/to/file") +style, _ := gotenberg.NewDocumentFromPath("style.css", "/path/to/file") +img, _ := gotenberg.NewDocumentFromPath("img.png", "/path/to/file") + +req := gotenberg.NewHTMLRequest(index) +req.Header(header) +req.Footer(footer) +req.Assets(style, img) +req.PaperSize(gotenberg.A4) +req.Margins(gotenberg.NoMargins) + +//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") + +// if you wish to redirect the response directly to the browser, you may also use: +resp, _ := client.Post(req) ``` For more complete usages, head to the [documentation](https://thecodingmachine.github.io/gotenberg). - ## Badges [![Travis CI](https://travis-ci.org/thecodingmachine/gotenberg-go-client.svg?branch=master)](https://travis-ci.org/thecodingmachine/gotenberg-go-client) diff --git a/build/tests/docker-entrypoint.sh b/build/tests/docker-entrypoint.sh index e1186e4..ae2acdc 100755 --- a/build/tests/docker-entrypoint.sh +++ b/build/tests/docker-entrypoint.sh @@ -5,5 +5,5 @@ set -xe # Testing Go client. gotenberg & sleep 10 -go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg-go-client/v6 +go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg-go-client/v7 sleep 10 # allows Gotenberg to remove generated files. \ No newline at end of file diff --git a/chrome.go b/chrome.go index 826d265..5eac16f 100644 --- a/chrome.go +++ b/chrome.go @@ -46,14 +46,14 @@ var ( ) type chromeRequest struct { - headerFilePath string - footerFilePath string + header Document + footer Document *request } func newChromeRequest() *chromeRequest { - return &chromeRequest{"", "", newRequest()} + return &chromeRequest{nil, nil, newRequest()} } // WaitDelay sets waitDelay form field. @@ -62,21 +62,13 @@ func (req *chromeRequest) WaitDelay(delay float64) { } // Header sets header form file. -func (req *chromeRequest) Header(fpath string) error { - if !fileExists(fpath) { - return fmt.Errorf("%s: header file does not exist", fpath) - } - req.headerFilePath = fpath - return nil +func (req *chromeRequest) Header(header Document) { + req.header = header } // Footer sets footer form file. -func (req *chromeRequest) Footer(fpath string) error { - if !fileExists(fpath) { - return fmt.Errorf("%s: footer file does not exist", fpath) - } - req.footerFilePath = fpath - return nil +func (req *chromeRequest) Footer(footer Document) { + req.footer = footer } // PaperSize sets paperWidth and paperHeight form fields. @@ -104,9 +96,13 @@ func (req *chromeRequest) GoogleChromeRpccBufferSize(bufferSize int64) { req.values[googleChromeRpccBufferSize] = strconv.FormatInt(bufferSize, 10) } -func (req *chromeRequest) formFiles() map[string]string { - files := make(map[string]string) - files["header.html"] = req.headerFilePath - files["footer.html"] = req.footerFilePath +func (req *chromeRequest) formFiles() map[string]Document { + files := make(map[string]Document) + if req.header != nil { + files["header.html"] = req.header + } + if req.footer != nil { + files["footer.html"] = req.footer + } return files } diff --git a/client.go b/client.go index 9608330..6fcae1a 100644 --- a/client.go +++ b/client.go @@ -35,7 +35,7 @@ type Request interface { postURL() string customHTTPHeaders() map[string]string formValues() map[string]string - formFiles() map[string]string + formFiles() map[string]Document } type request struct { @@ -164,14 +164,10 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) { body := &bytes.Buffer{} writer := multipart.NewWriter(body) defer writer.Close() // nolint: errcheck - for filename, fpath := range req.formFiles() { - // https://github.com/thecodingmachine/gotenberg-go-client/issues/3 - if fpath == "" { - continue - } - in, err := os.Open(fpath) + for filename, document := range req.formFiles() { + in, err := document.Reader() if err != nil { - return nil, "", fmt.Errorf("%s: opening file: %v", filename, err) + return nil, "", fmt.Errorf("%s: creating reader: %v", filename, err) } defer in.Close() // nolint: errcheck part, err := writer.CreateFormFile("files", filename) @@ -180,7 +176,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) { } _, err = io.Copy(part, in) if err != nil { - return nil, "", fmt.Errorf("%s: copying file: %v", filename, err) + return nil, "", fmt.Errorf("%s: copying data: %v", filename, err) } } for name, value := range req.formValues() { diff --git a/document.go b/document.go new file mode 100644 index 0000000..f7b8cfe --- /dev/null +++ b/document.go @@ -0,0 +1,102 @@ +package gotenberg + +import ( + "bytes" + "fmt" + "io" + "io/ioutil" + "os" + "strings" +) + +// Document reprents a file which +// will be send to the Gotenberg API. +type Document interface { + Filename() string + Reader() (io.ReadCloser, error) +} + +type document struct { + filename string +} + +func (doc *document) Filename() string { + return doc.filename +} + +type documentFromPath struct { + fpath string + + *document +} + +// NewDocumentFromPath creates a Document from +// a file path. +func NewDocumentFromPath(filename, fpath string) (Document, error) { + if !fileExists(fpath) { + return nil, fmt.Errorf("%s: file %s does not exist", fpath, filename) + } + return &documentFromPath{ + fpath, + &document{filename}, + }, nil +} + +func (doc *documentFromPath) Reader() (io.ReadCloser, error) { + in, err := os.Open(doc.fpath) + if err != nil { + return nil, fmt.Errorf("%s: opening file: %v", doc.Filename(), err) + } + return in, nil +} + +type documentFromString struct { + data string + + *document +} + +// NewDocumentFromString creates a Document from +// a string. +func NewDocumentFromString(filename, data string) (Document, error) { + if len(data) == 0 { + return nil, fmt.Errorf("%s: string is empty", filename) + } + return &documentFromString{ + data, + &document{filename}, + }, nil +} + +func (doc *documentFromString) Reader() (io.ReadCloser, error) { + return ioutil.NopCloser(strings.NewReader(doc.data)), nil +} + +type documentFromBytes struct { + data []byte + + *document +} + +// NewDocumentFromBytes creates a Document from +// bytes. +func NewDocumentFromBytes(filename string, data []byte) (Document, error) { + if len(data) == 0 { + return nil, fmt.Errorf("%s: bytes are empty", filename) + } + return &documentFromBytes{ + data, + &document{filename}, + }, nil +} + +func (doc *documentFromBytes) Reader() (io.ReadCloser, error) { + return ioutil.NopCloser(bytes.NewReader(doc.data)), nil +} + +// Compile-time checks to ensure type implements desired interfaces. +var ( + _ = Document(new(documentFromPath)) + _ = Document(new(documentFromString)) + _ = Document(new(documentFromBytes)) +) diff --git a/go.mod b/go.mod index 06b5e6d..ef358e3 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,5 @@ -module github.com/thecodingmachine/gotenberg-go-client/v6 +module github.com/thecodingmachine/gotenberg-go-client/v7 go 1.13 -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/stretchr/testify v1.3.0 -) +require github.com/stretchr/testify v1.3.0 diff --git a/go.sum b/go.sum index 4f76e62..4347755 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/html.go b/html.go index d7b20b3..5b959fa 100644 --- a/html.go +++ b/html.go @@ -1,49 +1,33 @@ package gotenberg -import ( - "fmt" - "path/filepath" -) - // HTMLRequest facilitates HTML conversion // with the Gotenberg API. type HTMLRequest struct { - indexFilePath string - assetFilePaths []string + index Document + assets []Document *chromeRequest } // NewHTMLRequest create HTMLRequest. -func NewHTMLRequest(indexFilePath string) (*HTMLRequest, error) { - if !fileExists(indexFilePath) { - return nil, fmt.Errorf("%s: index file does not exist", indexFilePath) - } - return &HTMLRequest{indexFilePath, nil, newChromeRequest()}, nil +func NewHTMLRequest(index Document) *HTMLRequest { + return &HTMLRequest{index, []Document{}, newChromeRequest()} } // Assets sets assets form files. -func (req *HTMLRequest) Assets(fpaths ...string) error { - for _, fpath := range fpaths { - if !fileExists(fpath) { - return fmt.Errorf("%s: file does not exist", fpath) - } - } - req.assetFilePaths = fpaths - return nil +func (req *HTMLRequest) Assets(assets ...Document) { + req.assets = assets } func (req *HTMLRequest) postURL() string { return "/convert/html" } -func (req *HTMLRequest) formFiles() map[string]string { - files := make(map[string]string) - files["index.html"] = req.indexFilePath - files["header.html"] = req.headerFilePath - files["footer.html"] = req.footerFilePath - for _, fpath := range req.assetFilePaths { - files[filepath.Base(fpath)] = fpath +func (req *HTMLRequest) formFiles() map[string]Document { + files := make(map[string]Document) + files["index.html"] = req.index + for _, asset := range req.assets { + files[asset.Filename()] = asset } return files } diff --git a/html_test.go b/html_test.go index 0c39c22..068b22b 100644 --- a/html_test.go +++ b/html_test.go @@ -7,30 +7,42 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thecodingmachine/gotenberg-go-client/v6/test" + "github.com/thecodingmachine/gotenberg-go-client/v7/test" ) func TestHTML(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} - req, err := NewHTMLRequest(test.HTMLTestFilePath(t, "index.html")) + index, err := NewDocumentFromPath("index.html", test.HTMLTestFilePath(t, "index.html")) require.Nil(t, err) - req.ResultFilename("foo.pdf") - req.WaitTimeout(5) - req.WaitDelay(1) - err = req.Header(test.HTMLTestFilePath(t, "header.html")) - require.Nil(t, err) - err = req.Footer(test.HTMLTestFilePath(t, "footer.html")) + req := NewHTMLRequest(index) + dirPath, err := test.Rand() require.Nil(t, err) - err = req.Assets( - test.HTMLTestFilePath(t, "font.woff"), - test.HTMLTestFilePath(t, "img.gif"), - test.HTMLTestFilePath(t, "style.css"), - ) + dest := fmt.Sprintf("%s/foo.pdf", dirPath) + err = c.Store(req, dest) + assert.Nil(t, err) + assert.FileExists(t, dest) + err = os.RemoveAll(dirPath) + assert.Nil(t, err) +} + +func TestHTMLFromString(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + index, err := NewDocumentFromString("index.html", "Foo") + req := NewHTMLRequest(index) + dirPath, err := test.Rand() require.Nil(t, err) - req.PaperSize(A4) - req.Margins(NormalMargins) - req.Landscape(false) - req.GoogleChromeRpccBufferSize(1048576) + dest := fmt.Sprintf("%s/foo.pdf", dirPath) + err = c.Store(req, dest) + assert.Nil(t, err) + assert.FileExists(t, dest) + err = os.RemoveAll(dirPath) + assert.Nil(t, err) +} + +func TestHTMLFromBytes(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + index, err := NewDocumentFromBytes("index.html", []byte("Foo")) + req := NewHTMLRequest(index) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -41,10 +53,26 @@ func TestHTML(t *testing.T) { assert.Nil(t, err) } -func TestHTMLWithoutHeaderFooter(t *testing.T) { +func TestHTMLComplete(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} - req, err := NewHTMLRequest(test.HTMLTestFilePath(t, "index.html")) + index, err := NewDocumentFromPath("index.html", test.HTMLTestFilePath(t, "index.html")) + require.Nil(t, err) + req := NewHTMLRequest(index) + header, err := NewDocumentFromPath("header.html", test.HTMLTestFilePath(t, "header.html")) + require.Nil(t, err) + req.Header(header) + footer, err := NewDocumentFromPath("footer.html", test.HTMLTestFilePath(t, "footer.html")) + require.Nil(t, err) + req.Footer(footer) + font, err := NewDocumentFromPath("font.woff", test.HTMLTestFilePath(t, "font.woff")) require.Nil(t, err) + img, err := NewDocumentFromPath("img.gif", test.HTMLTestFilePath(t, "img.gif")) + require.Nil(t, err) + style, err := NewDocumentFromPath("style.css", test.HTMLTestFilePath(t, "style.css")) + req.Assets(font, img, style) + req.ResultFilename("foo.pdf") + req.WaitTimeout(5) + req.WaitDelay(1) req.PaperSize(A4) req.Margins(NormalMargins) req.Landscape(false) @@ -61,8 +89,9 @@ func TestHTMLWithoutHeaderFooter(t *testing.T) { func TestHTMLWebhook(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} - req, err := NewHTMLRequest(test.HTMLTestFilePath(t, "index.html")) + index, err := NewDocumentFromPath("index.html", test.HTMLTestFilePath(t, "index.html")) require.Nil(t, err) + req := NewHTMLRequest(index) req.WebhookURL("https://google.com") req.WebhookURLTimeout(5.0) req.AddWebhookURLHTTPHeader("A-Header", "Foo") diff --git a/markdown.go b/markdown.go index 9af5c88..105d330 100644 --- a/markdown.go +++ b/markdown.go @@ -1,58 +1,37 @@ package gotenberg -import ( - "fmt" - "path/filepath" -) - // MarkdownRequest facilitates Markdown conversion // with the Gotenberg API. type MarkdownRequest struct { - indexFilePath string - markdownFilePaths []string - assetFilePaths []string + index Document + markdowns []Document + assets []Document *chromeRequest } // NewMarkdownRequest create MarkdownRequest. -func NewMarkdownRequest(indexFilePath string, markdownFilePaths ...string) (*MarkdownRequest, error) { - if !fileExists(indexFilePath) { - return nil, fmt.Errorf("%s: index file does not exist", indexFilePath) - } - for _, fpath := range markdownFilePaths { - if !fileExists(fpath) { - return nil, fmt.Errorf("%s: markdown file does not exist", fpath) - } - } - return &MarkdownRequest{indexFilePath, markdownFilePaths, nil, newChromeRequest()}, nil +func NewMarkdownRequest(index Document, markdowns ...Document) *MarkdownRequest { + return &MarkdownRequest{index, markdowns, []Document{}, newChromeRequest()} } // Assets sets assets form files. -func (req *MarkdownRequest) Assets(fpaths ...string) error { - for _, fpath := range fpaths { - if !fileExists(fpath) { - return fmt.Errorf("%s: file does not exist", fpath) - } - } - req.assetFilePaths = fpaths - return nil +func (req *MarkdownRequest) Assets(assets ...Document) { + req.assets = assets } func (req *MarkdownRequest) postURL() string { return "/convert/markdown" } -func (req *MarkdownRequest) formFiles() map[string]string { - files := make(map[string]string) - files["index.html"] = req.indexFilePath - files["header.html"] = req.headerFilePath - files["footer.html"] = req.footerFilePath - for _, fpath := range req.markdownFilePaths { - files[filepath.Base(fpath)] = fpath +func (req *MarkdownRequest) formFiles() map[string]Document { + files := make(map[string]Document) + files["index.html"] = req.index + for _, markdown := range req.markdowns { + files[markdown.Filename()] = markdown } - for _, fpath := range req.assetFilePaths { - files[filepath.Base(fpath)] = fpath + for _, asset := range req.assets { + files[asset.Filename()] = asset } return files } diff --git a/markdown_test.go b/markdown_test.go index bbfc246..9b83a01 100644 --- a/markdown_test.go +++ b/markdown_test.go @@ -7,35 +7,20 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thecodingmachine/gotenberg-go-client/v6/test" + "github.com/thecodingmachine/gotenberg-go-client/v7/test" ) func TestMarkdown(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} - req, err := NewMarkdownRequest( - test.MarkdownTestFilePath(t, "index.html"), - test.MarkdownTestFilePath(t, "paragraph1.md"), - test.MarkdownTestFilePath(t, "paragraph2.md"), - test.MarkdownTestFilePath(t, "paragraph3.md"), - ) + index, err := NewDocumentFromPath("index.html", test.MarkdownTestFilePath(t, "index.html")) require.Nil(t, err) - req.ResultFilename("foo.pdf") - req.WaitTimeout(5) - req.WaitDelay(1) - err = req.Header(test.MarkdownTestFilePath(t, "header.html")) + markdown1, err := NewDocumentFromPath("paragraph1.md", test.MarkdownTestFilePath(t, "paragraph1.md")) require.Nil(t, err) - err = req.Footer(test.MarkdownTestFilePath(t, "footer.html")) + markdown2, err := NewDocumentFromPath("paragraph2.md", test.MarkdownTestFilePath(t, "paragraph2.md")) require.Nil(t, err) - err = req.Assets( - test.MarkdownTestFilePath(t, "font.woff"), - test.MarkdownTestFilePath(t, "img.gif"), - test.MarkdownTestFilePath(t, "style.css"), - ) + markdown3, err := NewDocumentFromPath("paragraph3.md", test.MarkdownTestFilePath(t, "paragraph3.md")) require.Nil(t, err) - req.PaperSize(A4) - req.Margins(NormalMargins) - req.Landscape(false) - req.GoogleChromeRpccBufferSize(1048576) + req := NewMarkdownRequest(index, markdown1, markdown2, markdown3) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -46,15 +31,32 @@ func TestMarkdown(t *testing.T) { assert.Nil(t, err) } -func TestMarkdownWithoutHeaderFooter(t *testing.T) { +func TestMarkdownComplete(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} - req, err := NewMarkdownRequest( - test.MarkdownTestFilePath(t, "index.html"), - test.MarkdownTestFilePath(t, "paragraph1.md"), - test.MarkdownTestFilePath(t, "paragraph2.md"), - test.MarkdownTestFilePath(t, "paragraph3.md"), - ) + index, err := NewDocumentFromPath("index.html", test.MarkdownTestFilePath(t, "index.html")) + require.Nil(t, err) + markdown1, err := NewDocumentFromPath("paragraph1.md", test.MarkdownTestFilePath(t, "paragraph1.md")) + require.Nil(t, err) + markdown2, err := NewDocumentFromPath("paragraph2.md", test.MarkdownTestFilePath(t, "paragraph2.md")) + require.Nil(t, err) + markdown3, err := NewDocumentFromPath("paragraph3.md", test.MarkdownTestFilePath(t, "paragraph3.md")) require.Nil(t, err) + req := NewMarkdownRequest(index, markdown1, markdown2, markdown3) + header, err := NewDocumentFromPath("header.html", test.MarkdownTestFilePath(t, "header.html")) + require.Nil(t, err) + req.Header(header) + footer, err := NewDocumentFromPath("footer.html", test.MarkdownTestFilePath(t, "footer.html")) + require.Nil(t, err) + req.Footer(footer) + font, err := NewDocumentFromPath("font.woff", test.MarkdownTestFilePath(t, "font.woff")) + require.Nil(t, err) + img, err := NewDocumentFromPath("img.gif", test.MarkdownTestFilePath(t, "img.gif")) + require.Nil(t, err) + style, err := NewDocumentFromPath("style.css", test.MarkdownTestFilePath(t, "style.css")) + req.Assets(font, img, style) + req.ResultFilename("foo.pdf") + req.WaitTimeout(5) + req.WaitDelay(1) req.PaperSize(A4) req.Margins(NormalMargins) req.Landscape(false) @@ -71,13 +73,15 @@ func TestMarkdownWithoutHeaderFooter(t *testing.T) { func TestMarkdownWebhook(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} - req, err := NewMarkdownRequest( - test.MarkdownTestFilePath(t, "index.html"), - test.MarkdownTestFilePath(t, "paragraph1.md"), - test.MarkdownTestFilePath(t, "paragraph2.md"), - test.MarkdownTestFilePath(t, "paragraph3.md"), - ) + index, err := NewDocumentFromPath("index.html", test.MarkdownTestFilePath(t, "index.html")) + require.Nil(t, err) + markdown1, err := NewDocumentFromPath("paragraph1.md", test.MarkdownTestFilePath(t, "paragraph1.md")) + require.Nil(t, err) + markdown2, err := NewDocumentFromPath("paragraph2.md", test.MarkdownTestFilePath(t, "paragraph2.md")) + require.Nil(t, err) + markdown3, err := NewDocumentFromPath("paragraph3.md", test.MarkdownTestFilePath(t, "paragraph3.md")) require.Nil(t, err) + req := NewMarkdownRequest(index, markdown1, markdown2, markdown3) req.WebhookURL("https://google.com") req.WebhookURLTimeout(5.0) req.AddWebhookURLHTTPHeader("A-Header", "Foo") diff --git a/merge.go b/merge.go index e0af9cc..5a286c5 100644 --- a/merge.go +++ b/merge.go @@ -1,36 +1,26 @@ package gotenberg -import ( - "fmt" - "path/filepath" -) - // MergeRequest facilitates merging PDF // with the Gotenberg API. type MergeRequest struct { - filePaths []string + pdfs []Document *request } // NewMergeRequest create MergeRequest. -func NewMergeRequest(fpaths ...string) (*MergeRequest, error) { - for _, fpath := range fpaths { - if !fileExists(fpath) { - return nil, fmt.Errorf("%s: file does not exist", fpath) - } - } - return &MergeRequest{fpaths, newRequest()}, nil +func NewMergeRequest(pdfs ...Document) *MergeRequest { + return &MergeRequest{pdfs, newRequest()} } func (req *MergeRequest) postURL() string { return "/merge" } -func (req *MergeRequest) formFiles() map[string]string { - files := make(map[string]string) - for _, fpath := range req.filePaths { - files[filepath.Base(fpath)] = fpath +func (req *MergeRequest) formFiles() map[string]Document { + files := make(map[string]Document) + for _, pdf := range req.pdfs { + files[pdf.Filename()] = pdf } return files } diff --git a/merge_test.go b/merge_test.go index 86c20e4..63f29a1 100644 --- a/merge_test.go +++ b/merge_test.go @@ -7,16 +7,16 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thecodingmachine/gotenberg-go-client/v6/test" + "github.com/thecodingmachine/gotenberg-go-client/v7/test" ) func TestMerge(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} - req, err := NewMergeRequest( - test.PDFTestFilePath(t, "gotenberg.pdf"), - test.PDFTestFilePath(t, "gotenberg.pdf"), - ) + pdf1, err := NewDocumentFromPath("gotenberg1.pdf", test.PDFTestFilePath(t, "gotenberg.pdf")) require.Nil(t, err) + pdf2, err := NewDocumentFromPath("gotenberg2.pdf", test.PDFTestFilePath(t, "gotenberg.pdf")) + require.Nil(t, err) + req := NewMergeRequest(pdf1, pdf2) req.ResultFilename("foo.pdf") req.WaitTimeout(5) dirPath, err := test.Rand() @@ -31,11 +31,11 @@ func TestMerge(t *testing.T) { func TestMergeWebhook(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} - req, err := NewMergeRequest( - test.PDFTestFilePath(t, "gotenberg.pdf"), - test.PDFTestFilePath(t, "gotenberg.pdf"), - ) + pdf1, err := NewDocumentFromPath("gotenberg1.pdf", test.PDFTestFilePath(t, "gotenberg.pdf")) + require.Nil(t, err) + pdf2, err := NewDocumentFromPath("gotenberg2.pdf", test.PDFTestFilePath(t, "gotenberg.pdf")) require.Nil(t, err) + req := NewMergeRequest(pdf1, pdf2) req.WebhookURL("https://google.com") req.WebhookURLTimeout(5.0) req.AddWebhookURLHTTPHeader("A-Header", "Foo") diff --git a/office.go b/office.go index b519baf..c41bd1d 100644 --- a/office.go +++ b/office.go @@ -1,8 +1,6 @@ package gotenberg import ( - "fmt" - "path/filepath" "strconv" ) @@ -11,19 +9,14 @@ const landscapeOffice string = "landscape" // OfficeRequest facilitates Office documents // conversion with the Gotenberg API. type OfficeRequest struct { - filePaths []string + docs []Document *request } // NewOfficeRequest create OfficeRequest. -func NewOfficeRequest(fpaths ...string) (*OfficeRequest, error) { - for _, fpath := range fpaths { - if !fileExists(fpath) { - return nil, fmt.Errorf("%s: file does not exist", fpath) - } - } - return &OfficeRequest{fpaths, newRequest()}, nil +func NewOfficeRequest(docs ...Document) *OfficeRequest { + return &OfficeRequest{docs, newRequest()} } // Landscape sets landscape form field. @@ -35,10 +28,10 @@ func (req *OfficeRequest) postURL() string { return "/convert/office" } -func (req *OfficeRequest) formFiles() map[string]string { - files := make(map[string]string) - for _, fpath := range req.filePaths { - files[filepath.Base(fpath)] = fpath +func (req *OfficeRequest) formFiles() map[string]Document { + files := make(map[string]Document) + for _, doc := range req.docs { + files[doc.Filename()] = doc } return files } diff --git a/office_test.go b/office_test.go index e098e28..fada6f0 100644 --- a/office_test.go +++ b/office_test.go @@ -7,13 +7,14 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thecodingmachine/gotenberg-go-client/v6/test" + "github.com/thecodingmachine/gotenberg-go-client/v7/test" ) func TestOffice(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} - req, err := NewOfficeRequest(test.OfficeTestFilePath(t, "document.docx")) + doc, err := NewDocumentFromPath("document.docx", test.OfficeTestFilePath(t, "document.docx")) require.Nil(t, err) + req := NewOfficeRequest(doc) req.ResultFilename("foo.pdf") req.WaitTimeout(5) req.Landscape(false) @@ -29,8 +30,9 @@ func TestOffice(t *testing.T) { func TestOfficeWebhook(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} - req, err := NewOfficeRequest(test.OfficeTestFilePath(t, "document.docx")) + doc, err := NewDocumentFromPath("document.docx", test.OfficeTestFilePath(t, "document.docx")) require.Nil(t, err) + req := NewOfficeRequest(doc) req.WebhookURL("https://google.com") req.WebhookURLTimeout(5.0) req.AddWebhookURLHTTPHeader("A-Header", "Foo") diff --git a/url_test.go b/url_test.go index be1a703..4024dbb 100644 --- a/url_test.go +++ b/url_test.go @@ -7,24 +7,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thecodingmachine/gotenberg-go-client/v6/test" + "github.com/thecodingmachine/gotenberg-go-client/v7/test" ) func TestURL(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} req := NewURLRequest("http://google.com") - req.ResultFilename("foo.pdf") - req.WaitTimeout(5) - req.WaitDelay(1) - req.AddRemoteURLHTTPHeader("A-Header", "Foo") - err := req.Header(test.URLTestFilePath(t, "header.html")) - require.Nil(t, err) - err = req.Footer(test.URLTestFilePath(t, "footer.html")) - require.Nil(t, err) - req.PaperSize(A4) - req.Margins(NormalMargins) - req.Landscape(false) - req.GoogleChromeRpccBufferSize(1048576) dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) @@ -35,9 +23,18 @@ func TestURL(t *testing.T) { assert.Nil(t, err) } -func TestURLWithoutHeaderFooter(t *testing.T) { +func TestURLComplete(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} req := NewURLRequest("http://google.com") + header, err := NewDocumentFromPath("header.html", test.HTMLTestFilePath(t, "header.html")) + require.Nil(t, err) + req.Header(header) + footer, err := NewDocumentFromPath("footer.html", test.HTMLTestFilePath(t, "footer.html")) + require.Nil(t, err) + req.Footer(footer) + req.ResultFilename("foo.pdf") + req.WaitTimeout(5) + req.WaitDelay(1) req.PaperSize(A4) req.Margins(NormalMargins) req.Landscape(false) From edc60c16069d110148c3fd015206b99ae1546ad2 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Tue, 10 Dec 2019 13:55:01 +0100 Subject: [PATCH 7/8] adding missing remote url header in tests --- url_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/url_test.go b/url_test.go index 4024dbb..f7d8a38 100644 --- a/url_test.go +++ b/url_test.go @@ -39,6 +39,7 @@ func TestURLComplete(t *testing.T) { req.Margins(NormalMargins) req.Landscape(false) req.GoogleChromeRpccBufferSize(1048576) + req.AddRemoteURLHTTPHeader("A-Header", "Foo") dirPath, err := test.Rand() require.Nil(t, err) dest := fmt.Sprintf("%s/foo.pdf", dirPath) From 82551fbfdd07dee971373a08a35c052705e11b23 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Tue, 10 Dec 2019 15:40:35 +0100 Subject: [PATCH 8/8] adding page ranges --- chrome.go | 6 ++++++ html_test.go | 11 +++++++++++ markdown_test.go | 17 +++++++++++++++++ office.go | 10 +++++++++- office_test.go | 11 +++++++++++ url_test.go | 9 +++++++++ 6 files changed, 63 insertions(+), 1 deletion(-) diff --git a/chrome.go b/chrome.go index 5eac16f..04cd759 100644 --- a/chrome.go +++ b/chrome.go @@ -14,6 +14,7 @@ const ( marginLeft string = "marginLeft" marginRight string = "marginRight" landscapeChrome string = "landscape" + pageRanges string = "pageRanges" googleChromeRpccBufferSize string = "googleChromeRpccBufferSize" ) @@ -91,6 +92,11 @@ func (req *chromeRequest) Landscape(isLandscape bool) { req.values[landscapeChrome] = strconv.FormatBool(isLandscape) } +// PageRanges sets pageRanges form field. +func (req *chromeRequest) PageRanges(ranges string) { + req.values[pageRanges] = ranges +} + // GoogleChromeRpccBufferSize sets googleChromeRpccBufferSize form field. func (req *chromeRequest) GoogleChromeRpccBufferSize(bufferSize int64) { req.values[googleChromeRpccBufferSize] = strconv.FormatInt(bufferSize, 10) diff --git a/html_test.go b/html_test.go index 068b22b..a2de1be 100644 --- a/html_test.go +++ b/html_test.go @@ -87,6 +87,17 @@ func TestHTMLComplete(t *testing.T) { assert.Nil(t, err) } +func TestHTMLPageRanges(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + index, err := NewDocumentFromPath("index.html", test.HTMLTestFilePath(t, "index.html")) + require.Nil(t, err) + req := NewHTMLRequest(index) + req.PageRanges("1-1") + resp, err := c.Post(req) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) +} + func TestHTMLWebhook(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} index, err := NewDocumentFromPath("index.html", test.HTMLTestFilePath(t, "index.html")) diff --git a/markdown_test.go b/markdown_test.go index 9b83a01..61fb3e5 100644 --- a/markdown_test.go +++ b/markdown_test.go @@ -71,6 +71,23 @@ func TestMarkdownComplete(t *testing.T) { assert.Nil(t, err) } +func TestMarkdownPageRanges(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + index, err := NewDocumentFromPath("index.html", test.MarkdownTestFilePath(t, "index.html")) + require.Nil(t, err) + markdown1, err := NewDocumentFromPath("paragraph1.md", test.MarkdownTestFilePath(t, "paragraph1.md")) + require.Nil(t, err) + markdown2, err := NewDocumentFromPath("paragraph2.md", test.MarkdownTestFilePath(t, "paragraph2.md")) + require.Nil(t, err) + markdown3, err := NewDocumentFromPath("paragraph3.md", test.MarkdownTestFilePath(t, "paragraph3.md")) + require.Nil(t, err) + req := NewMarkdownRequest(index, markdown1, markdown2, markdown3) + req.PageRanges("1-1") + resp, err := c.Post(req) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) +} + func TestMarkdownWebhook(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} index, err := NewDocumentFromPath("index.html", test.MarkdownTestFilePath(t, "index.html")) diff --git a/office.go b/office.go index c41bd1d..4ceccfb 100644 --- a/office.go +++ b/office.go @@ -4,7 +4,10 @@ import ( "strconv" ) -const landscapeOffice string = "landscape" +const ( + landscapeOffice string = "landscape" + pageRangesOffice string = "pageRanges" +) // OfficeRequest facilitates Office documents // conversion with the Gotenberg API. @@ -24,6 +27,11 @@ func (req *OfficeRequest) Landscape(isLandscape bool) { req.values[landscapeOffice] = strconv.FormatBool(isLandscape) } +// PageRanges sets pageRanges form field. +func (req *OfficeRequest) PageRanges(ranges string) { + req.values[pageRangesOffice] = ranges +} + func (req *OfficeRequest) postURL() string { return "/convert/office" } diff --git a/office_test.go b/office_test.go index fada6f0..81622e7 100644 --- a/office_test.go +++ b/office_test.go @@ -28,6 +28,17 @@ func TestOffice(t *testing.T) { assert.Nil(t, err) } +func TestOfficePageRanges(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + doc, err := NewDocumentFromPath("document.docx", test.OfficeTestFilePath(t, "document.docx")) + require.Nil(t, err) + req := NewOfficeRequest(doc) + req.PageRanges("1-1") + resp, err := c.Post(req) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) +} + func TestOfficeWebhook(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} doc, err := NewDocumentFromPath("document.docx", test.OfficeTestFilePath(t, "document.docx")) diff --git a/url_test.go b/url_test.go index f7d8a38..3901c0c 100644 --- a/url_test.go +++ b/url_test.go @@ -50,6 +50,15 @@ func TestURLComplete(t *testing.T) { assert.Nil(t, err) } +func TestURLPageRanges(t *testing.T) { + c := &Client{Hostname: "http://localhost:3000"} + req := NewURLRequest("http://google.com") + req.PageRanges("1-1") + resp, err := c.Post(req) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) +} + func TestURLWebhook(t *testing.T) { c := &Client{Hostname: "http://localhost:3000"} req := NewURLRequest("http://google.com")