Skip to content
This repository was archived by the owner on Apr 14, 2024. It is now read-only.

Commit d712358

Browse files
author
Julien Neuhart
committed
bc: document may now be from fpath, string, or byte array
1 parent e2e579a commit d712358

File tree

17 files changed

+318
-230
lines changed

17 files changed

+318
-230
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GOLANG_VERSION=1.13
22
GOTENBERG_VERSION=6
3-
GOTENBERG_LOG_LEVEL=DEBUG
3+
GOTENBERG_LOG_LEVEL=ERROR
44
VERSION=snapshot
55
GOLANGCI_LINT_VERSION=1.20.1
66

README.md

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,57 @@ A simple Go client for interacting with a Gotenberg API.
55
## Install
66

77
```bash
8-
$ go get -u github.com/thecodingmachine/gotenberg-go-client/v6
8+
$ go get -u github.com/thecodingmachine/gotenberg-go-client/v7
99
```
1010

1111
## Usage
1212

1313
```golang
14-
import "github.com/thecodingmachine/gotenberg-go-client/v6"
15-
16-
func main() {
17-
// HTML conversion example.
18-
c := &gotenberg.Client{Hostname: "http://localhost:3000"}
19-
req, _ := gotenberg.NewHTMLRequest("index.html")
20-
req.Header("header.html")
21-
req.Footer("footer.html")
22-
req.Assets(
23-
"font.woff",
24-
"img.gif",
25-
"style.css",
26-
)
27-
req.PaperSize(gotenberg.A4)
28-
req.Margins(gotenberg.NormalMargins)
29-
req.Landscape(false)
30-
dest := "foo.pdf"
31-
c.Store(req, dest)
14+
import (
15+
"time"
16+
"net/http"
17+
18+
"github.com/thecodingmachine/gotenberg-go-client/v7"
19+
)
20+
21+
// create the client.
22+
client := &gotenberg.Client{Hostname: "http://localhost:3000"}
23+
// ... or use your own *http.Client.
24+
httpClient := &http.Client{
25+
Timeout: time.Duration(5) * time.Second,
3226
}
27+
client := &gotenberg.Client{Hostname: "http://localhost:3000", HTTPClient: httpClient}
28+
29+
// prepare the files required for your conversion.
30+
31+
// from a path.
32+
index, _ := gotenberg.NewDocumentFromPath("index.html", "/path/to/file")
33+
// ... or from a string.
34+
index, _ := gotenberg.NewDocumentFromString("index.html", "<html>Foo</html>")
35+
// ... or from bytes.
36+
index, _ := gotenberg.NewDocumentFromBytes("index.html", []byte("<html>Foo</html>"))
37+
38+
header, _ := gotenberg.NewDocumentFromPath("header.html", "/path/to/file")
39+
footer, _ := gotenberg.NewDocumentFromPath("footer.html", "/path/to/file")
40+
style, _ := gotenberg.NewDocumentFromPath("style.css", "/path/to/file")
41+
img, _ := gotenberg.NewDocumentFromPath("img.png", "/path/to/file")
42+
43+
req := gotenberg.NewHTMLRequest(index)
44+
req.Header(header)
45+
req.Footer(footer)
46+
req.Assets(style, img)
47+
req.PaperSize(gotenberg.A4)
48+
req.Margins(gotenberg.NoMargins)
49+
50+
//store method allows you to... store the resulting PDF in a particular destination.
51+
client.Store(req, "path/you/want/the/pdf/to/be/stored.pdf")
52+
53+
// if you wish to redirect the response directly to the browser, you may also use:
54+
resp, _ := client.Post(req)
3355
```
3456

3557
For more complete usages, head to the [documentation](https://thecodingmachine.github.io/gotenberg).
3658

37-
3859
## Badges
3960

4061
[![Travis CI](https://travis-ci.org/thecodingmachine/gotenberg-go-client.svg?branch=master)](https://travis-ci.org/thecodingmachine/gotenberg-go-client)

build/tests/docker-entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ set -xe
55
# Testing Go client.
66
gotenberg &
77
sleep 10
8-
go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg-go-client/v6
8+
go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg-go-client/v7
99
sleep 10 # allows Gotenberg to remove generated files.

chrome.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ var (
4646
)
4747

4848
type chromeRequest struct {
49-
headerFilePath string
50-
footerFilePath string
49+
header Document
50+
footer Document
5151

5252
*request
5353
}
5454

5555
func newChromeRequest() *chromeRequest {
56-
return &chromeRequest{"", "", newRequest()}
56+
return &chromeRequest{nil, nil, newRequest()}
5757
}
5858

5959
// WaitDelay sets waitDelay form field.
@@ -62,21 +62,13 @@ func (req *chromeRequest) WaitDelay(delay float64) {
6262
}
6363

6464
// Header sets header form file.
65-
func (req *chromeRequest) Header(fpath string) error {
66-
if !fileExists(fpath) {
67-
return fmt.Errorf("%s: header file does not exist", fpath)
68-
}
69-
req.headerFilePath = fpath
70-
return nil
65+
func (req *chromeRequest) Header(header Document) {
66+
req.header = header
7167
}
7268

7369
// Footer sets footer form file.
74-
func (req *chromeRequest) Footer(fpath string) error {
75-
if !fileExists(fpath) {
76-
return fmt.Errorf("%s: footer file does not exist", fpath)
77-
}
78-
req.footerFilePath = fpath
79-
return nil
70+
func (req *chromeRequest) Footer(footer Document) {
71+
req.footer = footer
8072
}
8173

8274
// PaperSize sets paperWidth and paperHeight form fields.
@@ -104,9 +96,13 @@ func (req *chromeRequest) GoogleChromeRpccBufferSize(bufferSize int64) {
10496
req.values[googleChromeRpccBufferSize] = strconv.FormatInt(bufferSize, 10)
10597
}
10698

107-
func (req *chromeRequest) formFiles() map[string]string {
108-
files := make(map[string]string)
109-
files["header.html"] = req.headerFilePath
110-
files["footer.html"] = req.footerFilePath
99+
func (req *chromeRequest) formFiles() map[string]Document {
100+
files := make(map[string]Document)
101+
if req.header != nil {
102+
files["header.html"] = req.header
103+
}
104+
if req.footer != nil {
105+
files["footer.html"] = req.footer
106+
}
111107
return files
112108
}

client.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Request interface {
3535
postURL() string
3636
customHTTPHeaders() map[string]string
3737
formValues() map[string]string
38-
formFiles() map[string]string
38+
formFiles() map[string]Document
3939
}
4040

4141
type request struct {
@@ -164,14 +164,10 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
164164
body := &bytes.Buffer{}
165165
writer := multipart.NewWriter(body)
166166
defer writer.Close() // nolint: errcheck
167-
for filename, fpath := range req.formFiles() {
168-
// https://github.com/thecodingmachine/gotenberg-go-client/issues/3
169-
if fpath == "" {
170-
continue
171-
}
172-
in, err := os.Open(fpath)
167+
for filename, document := range req.formFiles() {
168+
in, err := document.Reader()
173169
if err != nil {
174-
return nil, "", fmt.Errorf("%s: opening file: %v", filename, err)
170+
return nil, "", fmt.Errorf("%s: creating reader: %v", filename, err)
175171
}
176172
defer in.Close() // nolint: errcheck
177173
part, err := writer.CreateFormFile("files", filename)
@@ -180,7 +176,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
180176
}
181177
_, err = io.Copy(part, in)
182178
if err != nil {
183-
return nil, "", fmt.Errorf("%s: copying file: %v", filename, err)
179+
return nil, "", fmt.Errorf("%s: copying data: %v", filename, err)
184180
}
185181
}
186182
for name, value := range req.formValues() {

document.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package gotenberg
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
"strings"
10+
)
11+
12+
// Document reprents a file which
13+
// will be send to the Gotenberg API.
14+
type Document interface {
15+
Filename() string
16+
Reader() (io.ReadCloser, error)
17+
}
18+
19+
type document struct {
20+
filename string
21+
}
22+
23+
func (doc *document) Filename() string {
24+
return doc.filename
25+
}
26+
27+
type documentFromPath struct {
28+
fpath string
29+
30+
*document
31+
}
32+
33+
// NewDocumentFromPath creates a Document from
34+
// a file path.
35+
func NewDocumentFromPath(filename, fpath string) (Document, error) {
36+
if !fileExists(fpath) {
37+
return nil, fmt.Errorf("%s: file %s does not exist", fpath, filename)
38+
}
39+
return &documentFromPath{
40+
fpath,
41+
&document{filename},
42+
}, nil
43+
}
44+
45+
func (doc *documentFromPath) Reader() (io.ReadCloser, error) {
46+
in, err := os.Open(doc.fpath)
47+
if err != nil {
48+
return nil, fmt.Errorf("%s: opening file: %v", doc.Filename(), err)
49+
}
50+
return in, nil
51+
}
52+
53+
type documentFromString struct {
54+
data string
55+
56+
*document
57+
}
58+
59+
// NewDocumentFromString creates a Document from
60+
// a string.
61+
func NewDocumentFromString(filename, data string) (Document, error) {
62+
if len(data) == 0 {
63+
return nil, fmt.Errorf("%s: string is empty", filename)
64+
}
65+
return &documentFromString{
66+
data,
67+
&document{filename},
68+
}, nil
69+
}
70+
71+
func (doc *documentFromString) Reader() (io.ReadCloser, error) {
72+
return ioutil.NopCloser(strings.NewReader(doc.data)), nil
73+
}
74+
75+
type documentFromBytes struct {
76+
data []byte
77+
78+
*document
79+
}
80+
81+
// NewDocumentFromBytes creates a Document from
82+
// bytes.
83+
func NewDocumentFromBytes(filename string, data []byte) (Document, error) {
84+
if len(data) == 0 {
85+
return nil, fmt.Errorf("%s: bytes are empty", filename)
86+
}
87+
return &documentFromBytes{
88+
data,
89+
&document{filename},
90+
}, nil
91+
}
92+
93+
func (doc *documentFromBytes) Reader() (io.ReadCloser, error) {
94+
return ioutil.NopCloser(bytes.NewReader(doc.data)), nil
95+
}
96+
97+
// Compile-time checks to ensure type implements desired interfaces.
98+
var (
99+
_ = Document(new(documentFromPath))
100+
_ = Document(new(documentFromString))
101+
_ = Document(new(documentFromBytes))
102+
)

go.mod

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
module github.com/thecodingmachine/gotenberg-go-client/v6
1+
module github.com/thecodingmachine/gotenberg-go-client/v7
22

33
go 1.13
44

5-
require (
6-
github.com/davecgh/go-spew v1.1.1 // indirect
7-
github.com/stretchr/testify v1.3.0
8-
)
5+
require github.com/stretchr/testify v1.3.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
53
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
64
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
75
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

html.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,33 @@
11
package gotenberg
22

3-
import (
4-
"fmt"
5-
"path/filepath"
6-
)
7-
83
// HTMLRequest facilitates HTML conversion
94
// with the Gotenberg API.
105
type HTMLRequest struct {
11-
indexFilePath string
12-
assetFilePaths []string
6+
index Document
7+
assets []Document
138

149
*chromeRequest
1510
}
1611

1712
// NewHTMLRequest create HTMLRequest.
18-
func NewHTMLRequest(indexFilePath string) (*HTMLRequest, error) {
19-
if !fileExists(indexFilePath) {
20-
return nil, fmt.Errorf("%s: index file does not exist", indexFilePath)
21-
}
22-
return &HTMLRequest{indexFilePath, nil, newChromeRequest()}, nil
13+
func NewHTMLRequest(index Document) *HTMLRequest {
14+
return &HTMLRequest{index, []Document{}, newChromeRequest()}
2315
}
2416

2517
// Assets sets assets form files.
26-
func (req *HTMLRequest) Assets(fpaths ...string) error {
27-
for _, fpath := range fpaths {
28-
if !fileExists(fpath) {
29-
return fmt.Errorf("%s: file does not exist", fpath)
30-
}
31-
}
32-
req.assetFilePaths = fpaths
33-
return nil
18+
func (req *HTMLRequest) Assets(assets ...Document) {
19+
req.assets = assets
3420
}
3521

3622
func (req *HTMLRequest) postURL() string {
3723
return "/convert/html"
3824
}
3925

40-
func (req *HTMLRequest) formFiles() map[string]string {
41-
files := make(map[string]string)
42-
files["index.html"] = req.indexFilePath
43-
files["header.html"] = req.headerFilePath
44-
files["footer.html"] = req.footerFilePath
45-
for _, fpath := range req.assetFilePaths {
46-
files[filepath.Base(fpath)] = fpath
26+
func (req *HTMLRequest) formFiles() map[string]Document {
27+
files := make(map[string]Document)
28+
files["index.html"] = req.index
29+
for _, asset := range req.assets {
30+
files[asset.Filename()] = asset
4731
}
4832
return files
4933
}

0 commit comments

Comments
 (0)