mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-13 19:02:15 +01:00
4.0.0 (#42)
* adding URL conversions * updating gotenberg version in documentation * pkg: input as variadic string (#39) * pkg: input as variadic string * pkg: fix link to github doc * update doc about v4 client * make doc * fixing missing variadic inputs in doc * adding more fonts * updating PHP documentation
This commit is contained in:
@@ -19,11 +19,11 @@ func main() {
|
||||
req, _ := gotenberg.NewHTMLRequest("index.html")
|
||||
req.SetHeader("header.html")
|
||||
req.SetFooter("footer.html")
|
||||
req.SetAssets([]string{
|
||||
req.SetAssets(
|
||||
"font.woff",
|
||||
"img.gif",
|
||||
"style.css",
|
||||
})
|
||||
)
|
||||
req.SetPaperSize(gotenberg.A4)
|
||||
req.SetMargins(gotenberg.NormalMargins)
|
||||
req.SetLandscape(false)
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
remoteURL string = "remoteURL"
|
||||
webhookURL string = "webhookURL"
|
||||
paperWidth string = "paperWidth"
|
||||
paperHeight string = "paperHeight"
|
||||
@@ -71,7 +72,6 @@ type Request interface {
|
||||
type ChromeRequest interface {
|
||||
SetHeader(fpath string) error
|
||||
SetFooter(fpath string) error
|
||||
SetAssets(fpaths []string) error
|
||||
SetPaperSize(size [2]float64)
|
||||
SetMargins(margins [4]float64)
|
||||
SetLandscape(isLandscape bool)
|
||||
|
||||
@@ -3,6 +3,6 @@ Package gotenberg is a Go client for
|
||||
interacting with a Gotenberg API.
|
||||
|
||||
For more complete usages, head to the documentation:
|
||||
https://thecodingmachine.gotenberg.github.io.
|
||||
https://thecodingmachine.github.io/gotenberg/
|
||||
*/
|
||||
package gotenberg
|
||||
|
||||
@@ -48,7 +48,7 @@ func (html *HTMLRequest) SetFooter(fpath string) error {
|
||||
}
|
||||
|
||||
// SetAssets sets assets form files.
|
||||
func (html *HTMLRequest) SetAssets(fpaths []string) error {
|
||||
func (html *HTMLRequest) SetAssets(fpaths ...string) error {
|
||||
for _, fpath := range fpaths {
|
||||
if !fileExists(fpath) {
|
||||
return fmt.Errorf("%s: file does not exist", fpath)
|
||||
|
||||
@@ -19,11 +19,11 @@ func TestHTML(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
err = req.SetFooter(test.HTMLTestFilePath(t, "footer.html"))
|
||||
require.Nil(t, err)
|
||||
err = req.SetAssets([]string{
|
||||
err = req.SetAssets(
|
||||
test.HTMLTestFilePath(t, "font.woff"),
|
||||
test.HTMLTestFilePath(t, "img.gif"),
|
||||
test.HTMLTestFilePath(t, "style.css"),
|
||||
})
|
||||
)
|
||||
require.Nil(t, err)
|
||||
req.SetPaperSize(A4)
|
||||
req.SetMargins(NormalMargins)
|
||||
|
||||
@@ -18,7 +18,7 @@ type MarkdownRequest struct {
|
||||
}
|
||||
|
||||
// NewMarkdownRequest create MarkdownRequest.
|
||||
func NewMarkdownRequest(indexFilePath string, markdownFilePaths []string) (*MarkdownRequest, error) {
|
||||
func NewMarkdownRequest(indexFilePath string, markdownFilePaths ...string) (*MarkdownRequest, error) {
|
||||
if !fileExists(indexFilePath) {
|
||||
return nil, fmt.Errorf("%s: index file does not exist", indexFilePath)
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func (markdown *MarkdownRequest) SetFooter(fpath string) error {
|
||||
}
|
||||
|
||||
// SetAssets sets assets form files.
|
||||
func (markdown *MarkdownRequest) SetAssets(fpaths []string) error {
|
||||
func (markdown *MarkdownRequest) SetAssets(fpaths ...string) error {
|
||||
for _, fpath := range fpaths {
|
||||
if !fileExists(fpath) {
|
||||
return fmt.Errorf("%s: file does not exist", fpath)
|
||||
|
||||
@@ -15,22 +15,20 @@ func TestMarkdown(t *testing.T) {
|
||||
c := &Client{Hostname: "http://localhost:3000"}
|
||||
req, err := NewMarkdownRequest(
|
||||
test.MarkdownTestFilePath(t, "index.html"),
|
||||
[]string{
|
||||
test.MarkdownTestFilePath(t, "paragraph1.md"),
|
||||
test.MarkdownTestFilePath(t, "paragraph2.md"),
|
||||
test.MarkdownTestFilePath(t, "paragraph3.md"),
|
||||
},
|
||||
test.MarkdownTestFilePath(t, "paragraph1.md"),
|
||||
test.MarkdownTestFilePath(t, "paragraph2.md"),
|
||||
test.MarkdownTestFilePath(t, "paragraph3.md"),
|
||||
)
|
||||
require.Nil(t, err)
|
||||
err = req.SetHeader(test.MarkdownTestFilePath(t, "header.html"))
|
||||
require.Nil(t, err)
|
||||
err = req.SetFooter(test.MarkdownTestFilePath(t, "footer.html"))
|
||||
require.Nil(t, err)
|
||||
err = req.SetAssets([]string{
|
||||
err = req.SetAssets(
|
||||
test.MarkdownTestFilePath(t, "font.woff"),
|
||||
test.MarkdownTestFilePath(t, "img.gif"),
|
||||
test.MarkdownTestFilePath(t, "style.css"),
|
||||
})
|
||||
)
|
||||
require.Nil(t, err)
|
||||
req.SetPaperSize(A4)
|
||||
req.SetMargins(NormalMargins)
|
||||
|
||||
@@ -13,7 +13,7 @@ type MergeRequest struct {
|
||||
}
|
||||
|
||||
// NewMergeRequest create MergeRequest.
|
||||
func NewMergeRequest(fpaths []string) (*MergeRequest, error) {
|
||||
func NewMergeRequest(fpaths ...string) (*MergeRequest, error) {
|
||||
for _, fpath := range fpaths {
|
||||
if !fileExists(fpath) {
|
||||
return nil, fmt.Errorf("%s: file does not exist", fpath)
|
||||
|
||||
@@ -13,10 +13,10 @@ import (
|
||||
|
||||
func TestMerge(t *testing.T) {
|
||||
c := &Client{Hostname: "http://localhost:3000"}
|
||||
req, err := NewMergeRequest([]string{
|
||||
req, err := NewMergeRequest(
|
||||
test.PDFTestFilePath(t, "gotenberg.pdf"),
|
||||
test.PDFTestFilePath(t, "gotenberg.pdf"),
|
||||
})
|
||||
)
|
||||
require.Nil(t, err)
|
||||
dirPath, err := rand.Get()
|
||||
require.Nil(t, err)
|
||||
|
||||
@@ -14,7 +14,7 @@ type OfficeRequest struct {
|
||||
}
|
||||
|
||||
// NewOfficeRequest create OfficeRequest.
|
||||
func NewOfficeRequest(fpaths []string) (*OfficeRequest, error) {
|
||||
func NewOfficeRequest(fpaths ...string) (*OfficeRequest, error) {
|
||||
for _, fpath := range fpaths {
|
||||
if !fileExists(fpath) {
|
||||
return nil, fmt.Errorf("%s: file does not exist", fpath)
|
||||
|
||||
@@ -13,9 +13,7 @@ import (
|
||||
|
||||
func TestOffice(t *testing.T) {
|
||||
c := &Client{Hostname: "http://localhost:3000"}
|
||||
req, err := NewOfficeRequest([]string{
|
||||
test.OfficeTestFilePath(t, "document.docx"),
|
||||
})
|
||||
req, err := NewOfficeRequest(test.OfficeTestFilePath(t, "document.docx"))
|
||||
require.Nil(t, err)
|
||||
req.SetLandscape(false)
|
||||
dirPath, err := rand.Get()
|
||||
|
||||
85
pkg/url.go
Normal file
85
pkg/url.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package gotenberg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// URLRequest facilitates remote URL conversion
|
||||
// with the Gotenberg API.
|
||||
type URLRequest struct {
|
||||
headerFilePath string
|
||||
footerFilePath string
|
||||
values map[string]string
|
||||
}
|
||||
|
||||
// NewURLRequest create URLRequest.
|
||||
func NewURLRequest(URL string) *URLRequest {
|
||||
req := &URLRequest{values: make(map[string]string)}
|
||||
req.values[remoteURL] = URL
|
||||
return req
|
||||
}
|
||||
|
||||
// SetWebhookURL sets webhookURL form field.
|
||||
func (url *URLRequest) SetWebhookURL(webhookURL string) {
|
||||
url.values[webhookURL] = webhookURL
|
||||
}
|
||||
|
||||
// SetHeader sets header form file.
|
||||
func (url *URLRequest) SetHeader(fpath string) error {
|
||||
if !fileExists(fpath) {
|
||||
return fmt.Errorf("%s: header file does not exist", fpath)
|
||||
}
|
||||
url.headerFilePath = fpath
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetFooter sets footer form file.
|
||||
func (url *URLRequest) SetFooter(fpath string) error {
|
||||
if !fileExists(fpath) {
|
||||
return fmt.Errorf("%s: footer file does not exist", fpath)
|
||||
}
|
||||
url.footerFilePath = fpath
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetPaperSize sets paperWidth and paperHeight form fields.
|
||||
func (url *URLRequest) SetPaperSize(size [2]float64) {
|
||||
url.values[paperWidth] = fmt.Sprintf("%f", size[0])
|
||||
url.values[paperHeight] = fmt.Sprintf("%f", size[1])
|
||||
}
|
||||
|
||||
// SetMargins sets marginTop, marginBottom,
|
||||
// marginLeft and marginRight form fields.
|
||||
func (url *URLRequest) SetMargins(margins [4]float64) {
|
||||
url.values[marginTop] = fmt.Sprintf("%f", margins[0])
|
||||
url.values[marginBottom] = fmt.Sprintf("%f", margins[1])
|
||||
url.values[marginLeft] = fmt.Sprintf("%f", margins[2])
|
||||
url.values[marginRight] = fmt.Sprintf("%f", margins[3])
|
||||
}
|
||||
|
||||
// SetLandscape sets landscape form field.
|
||||
func (url *URLRequest) SetLandscape(isLandscape bool) {
|
||||
url.values[landscape] = strconv.FormatBool(isLandscape)
|
||||
}
|
||||
|
||||
func (url *URLRequest) getPostURL() string {
|
||||
return "/convert/url"
|
||||
}
|
||||
|
||||
func (url *URLRequest) getFormValues() map[string]string {
|
||||
return url.values
|
||||
}
|
||||
|
||||
func (url *URLRequest) getFormFiles() map[string]string {
|
||||
files := make(map[string]string)
|
||||
files["header.html"] = url.headerFilePath
|
||||
files["footer.html"] = url.footerFilePath
|
||||
return files
|
||||
}
|
||||
|
||||
// Compile-time checks to ensure type implements desired interfaces.
|
||||
var (
|
||||
_ = Request(new(URLRequest))
|
||||
_ = ChromeRequest(new(URLRequest))
|
||||
)
|
||||
41
pkg/url_test.go
Normal file
41
pkg/url_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package gotenberg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/rand"
|
||||
"github.com/thecodingmachine/gotenberg/test"
|
||||
)
|
||||
|
||||
func TestURL(t *testing.T) {
|
||||
c := &Client{Hostname: "http://localhost:3000"}
|
||||
req := NewURLRequest("http://google.com")
|
||||
err := req.SetHeader(test.URLTestFilePath(t, "header.html"))
|
||||
require.Nil(t, err)
|
||||
err = req.SetFooter(test.URLTestFilePath(t, "footer.html"))
|
||||
require.Nil(t, err)
|
||||
require.Nil(t, err)
|
||||
req.SetPaperSize(A4)
|
||||
req.SetMargins(NormalMargins)
|
||||
req.SetLandscape(false)
|
||||
dirPath, err := rand.Get()
|
||||
require.Nil(t, err)
|
||||
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 TestConcurrentURL(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
go func() {
|
||||
TestURL(t)
|
||||
}()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user