Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GOLANG_VERSION=1.24.2
GOTENBERG_VERSION=8.20.1
GOTENBERG_VERSION=8.25.0
GOLANGCI_LINT_VERSION=2.1.2

REPO=starwalkn/gotenberg-go-client/v8
Expand Down
5 changes: 4 additions & 1 deletion baserequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type baseRequester interface {
customHeaders() map[httpHeader]string
formFields() map[formField]string
formDocuments() map[string]document.Document
formEmbeds() map[string]document.Document
}

type baseRequest struct {
Expand Down Expand Up @@ -105,13 +106,14 @@ func ensureWebhookMethod(method string) string {
// this is equivalent to map[string]map[string]string, which this method accepts, but headers map can be nil.
//
// URLs MUST return a Content-Disposition header with a filename parameter.
func (br *baseRequest) DownloadFrom(downloads map[string]map[string]string) {
func (br *baseRequest) DownloadFrom(downloads map[string]map[string]string, embedded bool) {
dfs := make([]downloadFrom, 0, len(downloads))

for url, headers := range downloads {
dfs = append(dfs, downloadFrom{
URL: url,
ExtraHTTPHeaders: headers,
Embedded: embedded,
})
}

Expand All @@ -126,4 +128,5 @@ func (br *baseRequest) DownloadFrom(downloads map[string]map[string]string) {
type downloadFrom struct {
URL string `json:"url"`
ExtraHTTPHeaders map[string]string `json:"extraHttpHeaders"`
Embedded bool `json:"embedded"`
}
5 changes: 5 additions & 0 deletions chromium.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ func (req *chromiumRequest) GenerateTaggedPDF(val bool) {
req.fields[fieldChromiumGenerateTaggedPDF] = strconv.FormatBool(val)
}

func (req *chromiumRequest) Encrypt(userPassword, ownerPassword string) {
req.fields[fieldUserPassword] = userPassword
req.fields[fieldOwnerPassword] = ownerPassword
}

// ScreenshotWidth Width sets the device screen width in pixels.
func (req *chromiumRequest) ScreenshotWidth(width int) {
req.fields[fieldScreenshotWidth] = strconv.Itoa(width)
Expand Down
6 changes: 4 additions & 2 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ type formField string

// Common property.
const (
fieldMetadata formField = "metadata"
fieldDownloadFrom formField = "downloadFrom"
fieldMetadata formField = "metadata"
fieldDownloadFrom formField = "downloadFrom"
fieldUserPassword formField = "userPassword"
fieldOwnerPassword formField = "ownerPassword"
)

// URL request property.
Expand Down
22 changes: 21 additions & 1 deletion flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package gotenberg
import "github.com/starwalkn/gotenberg-go-client/v8/document"

type FlattenRequest struct {
pdfs []document.Document
pdfs []document.Document
embeds []document.Document

*baseRequest
}
Expand All @@ -28,3 +29,22 @@ func (req *FlattenRequest) formDocuments() map[string]document.Document {

return files
}

func (req *FlattenRequest) formEmbeds() map[string]document.Document {
embeds := make(map[string]document.Document)

for _, embed := range req.embeds {
embeds[embed.Filename()] = embed
}

return embeds
}

func (req *FlattenRequest) Embeds(docs ...document.Document) {
req.embeds = append(req.embeds, docs...)
}

// Compile-time checks to ensure type implements desired interfaces.
var (
_ = MultipartRequester(new(FlattenRequest))
)
22 changes: 21 additions & 1 deletion html.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ const (
type HTMLRequest struct {
index document.Document
assets []document.Document
embeds []document.Document

*chromiumRequest
}

func NewHTMLRequest(index document.Document) *HTMLRequest {
return &HTMLRequest{index, []document.Document{}, newChromiumRequest()}
return &HTMLRequest{
index: index,
assets: []document.Document{},
embeds: []document.Document{},
chromiumRequest: newChromiumRequest(),
}
}

func (req *HTMLRequest) endpoint() string {
Expand Down Expand Up @@ -47,6 +53,20 @@ func (req *HTMLRequest) formDocuments() map[string]document.Document {
return files
}

func (req *HTMLRequest) formEmbeds() map[string]document.Document {
embeds := make(map[string]document.Document)

for _, embed := range req.embeds {
embeds[embed.Filename()] = embed
}

return embeds
}

func (req *HTMLRequest) Embeds(docs ...document.Document) {
req.embeds = docs
}

// Assets sets assets form files.
func (req *HTMLRequest) Assets(assets ...document.Document) {
req.assets = assets
Expand Down
60 changes: 52 additions & 8 deletions html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func TestHTML(t *testing.T) {
req.PaperSize(A4)
req.Margins(NormalMargins)
req.Scale(1.5)
dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.pdf", dirPath)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
Expand Down Expand Up @@ -90,9 +90,9 @@ func TestHTMLScreenshot(t *testing.T) {
err = req.Cookies(cks)
require.NoError(t, err)

dirPath := t.TempDir()
req.Format(JPEG)
dest := fmt.Sprintf("%s/foo.jpeg", dirPath)

dest := fmt.Sprintf("%s/foo.jpeg", t.TempDir())
err = c.StoreScreenshot(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
Expand All @@ -117,8 +117,8 @@ func TestHTMLPdfA(t *testing.T) {
require.NoError(t, err)

req.PdfA(PdfA3b)
dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.pdf", dirPath)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
Expand All @@ -142,12 +142,56 @@ func TestHTMLPdfUA(t *testing.T) {
require.NoError(t, err)

req.PdfUA()
dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.pdf", dirPath)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
isPDFUA, err := test.IsPDFUA(dest)
require.NoError(t, err)
assert.True(t, isPDFUA)
}

func TestHTMLEmbeds(t *testing.T) {
c, err := NewClient("http://localhost:3000", http.DefaultClient)
require.NoError(t, err)

index, err := document.FromPath("index.html", test.HTMLTestFilePath(t, "index.html"))
require.NoError(t, err)
req := NewHTMLRequest(index)
req.Trace("testHTMLEmbeds")
req.UseBasicAuth("foo", "bar")

var embeds []document.Document

header, err := document.FromPath("header.html", test.HTMLTestFilePath(t, "header.html"))
require.NoError(t, err)
embeds = append(embeds, header)
footer, err := document.FromPath("footer.html", test.HTMLTestFilePath(t, "footer.html"))
require.NoError(t, err)
embeds = append(embeds, footer)
font, err := document.FromPath("font.woff", test.HTMLTestFilePath(t, "font.woff"))
require.NoError(t, err)
embeds = append(embeds, font)
img, err := document.FromPath("img.gif", test.HTMLTestFilePath(t, "img.gif"))
require.NoError(t, err)
embeds = append(embeds, img)
style, err := document.FromPath("style.css", test.HTMLTestFilePath(t, "style.css"))
require.NoError(t, err)
embeds = append(embeds, style)

req.Embeds(embeds...)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)

isPDF, err := test.IsPDF(dest)
require.NoError(t, err)
assert.True(t, isPDF)

hasEmbeds, err := test.HasEmbeds(dest)
require.NoError(t, err)
assert.True(t, hasEmbeds)
}
23 changes: 22 additions & 1 deletion libreoffice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ const endpointOfficeConvert = "/forms/libreoffice/convert"

// LibreOfficeRequest facilitates LibreOffice documents conversion with the Gotenberg API.
type LibreOfficeRequest struct {
docs []document.Document
docs []document.Document
embeds []document.Document

*baseRequest
}

func NewLibreOfficeRequest(docs ...document.Document) *LibreOfficeRequest {
return &LibreOfficeRequest{
docs: docs,
embeds: []document.Document{},
baseRequest: newBaseRequest(),
}
}
Expand All @@ -35,6 +37,20 @@ func (req *LibreOfficeRequest) formDocuments() map[string]document.Document {
return files
}

func (req *LibreOfficeRequest) formEmbeds() map[string]document.Document {
embeds := make(map[string]document.Document)

for _, embed := range req.embeds {
embeds[embed.Filename()] = embed
}

return embeds
}

func (req *LibreOfficeRequest) Embeds(docs ...document.Document) {
req.embeds = docs
}

// Password sets the password for opening the source file.
func (req *LibreOfficeRequest) Password(password string) {
req.fields[fieldOfficePassword] = password
Expand Down Expand Up @@ -201,6 +217,11 @@ func (req *LibreOfficeRequest) UpdateIndexes(value bool) {
req.fields[fieldOfficeUpdateIndexes] = strconv.FormatBool(value)
}

func (req *LibreOfficeRequest) Encrypt(userPassword, ownerPassword string) {
req.fields[fieldUserPassword] = userPassword
req.fields[fieldOwnerPassword] = ownerPassword
}

// Compile-time checks to ensure type implements desired interfaces.
var (
_ = MultipartRequester(new(LibreOfficeRequest))
Expand Down
56 changes: 42 additions & 14 deletions libreoffice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestLibreOffice(t *testing.T) {
req.Trace("testLibreOffice")
req.UseBasicAuth("foo", "bar")
req.OutputFilename("foo.pdf")
dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.pdf", dirPath)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
Expand Down Expand Up @@ -62,8 +62,8 @@ func TestLibreOfficeLosslessCompression(t *testing.T) {
req.UseBasicAuth("foo", "bar")
req.OutputFilename("foo.pdf")
req.LosslessImageCompression()
dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.pdf", dirPath)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
Expand All @@ -85,8 +85,8 @@ func TestLibreOfficeCompression(t *testing.T) {
req.Quality(1)
req.ReduceImageResolution()
req.MaxImageResolution(75)
dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.pdf", dirPath)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
Expand All @@ -107,8 +107,8 @@ func TestLibreOfficeMultipleWithoutMerge(t *testing.T) {
req.Trace("testLibreOfficeMultipleWithoutMerge")
req.UseBasicAuth("foo", "bar")
req.OutputFilename("foo.zip")
dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.zip", dirPath)

dest := fmt.Sprintf("%s/foo.zip", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
Expand All @@ -132,8 +132,8 @@ func TestLibreOfficeMultipleWithMerge(t *testing.T) {
req.UseBasicAuth("foo", "bar")
req.OutputFilename("foo.pdf")
req.Merge()
dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.pdf", dirPath)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
Expand All @@ -157,8 +157,8 @@ func TestLibreOfficePdfA(t *testing.T) {
req.UseBasicAuth("foo", "bar")
req.OutputFilename("foo.pdf")
req.PdfA(PdfA3b)
dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.pdf", dirPath)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
Expand All @@ -178,12 +178,40 @@ func TestLibreOfficePdfUA(t *testing.T) {
req.UseBasicAuth("foo", "bar")
req.OutputFilename("foo.pdf")
req.PdfUA()
dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.pdf", dirPath)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
isPDFUA, err := test.IsPDFUA(dest)
require.NoError(t, err)
assert.True(t, isPDFUA)
}

func TestLibreOfficeEmbeds(t *testing.T) {
c, err := NewClient("http://localhost:3000", http.DefaultClient)
require.NoError(t, err)

doc1, err := document.FromPath("document1.docx", test.LibreOfficeTestFilePath(t, "document.docx"))
require.NoError(t, err)

req := NewLibreOfficeRequest(doc1)
req.Trace("testLibreOfficeEmbeds")
req.UseBasicAuth("foo", "bar")
req.OutputFilename("foo.pdf")
req.Merge()

doc2, err := document.FromPath("document2.docx", test.LibreOfficeTestFilePath(t, "document.docx"))
require.NoError(t, err)

req.Embeds(doc2)

dest := fmt.Sprintf("%s/foo.pdf", t.TempDir())
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)

hasEmbeds, err := test.HasEmbeds(dest)
require.NoError(t, err)
assert.True(t, hasEmbeds)
}
Loading