gotenberg

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

⚠️ Working with Gotenberg >= 7 ⚠️

Gotenberg Go client

A simple Go client for interacting with a Gotenberg API.

Inspired by thecodingmachine/gotenberg-go-client

Install

$ go get -u github.com/commitsmart/gotenberg-go-client

Usage

ctx := context.Background()
httpClient := &http.Client{
    Timeout: time.Duration(5) * time.Second,
}
client := gotenberg.NewClient("localhost:3000", httpClient)

// from a path.
index, err := gotenberg.NewDocumentFromPath("index.html", "/path/to/file")
check(err)
// ... or from a string.
// index, err := gotenberg.NewDocumentFromString("index.html", "<html>Foo</html>")
// ... or from bytes.
// index, err := gotenberg.NewDocumentFromBytes("index.html", []byte("<html>Foo</html>"))

header, err := gotenberg.NewDocumentFromPath("header.html", "/path/to/file")
check(err)
footer, err := gotenberg.NewDocumentFromPath("footer.html", "/path/to/file")
check(err)
style, err := gotenberg.NewDocumentFromPath("style.css", "/path/to/file")
check(err)
img, err := gotenberg.NewDocumentFromPath("img.png", "/path/to/file")
check(err)

req := gotenberg.NewConvertHTMLRequest(index)
req.Header(header)
req.Footer(footer)
req.Assets(style, img)
req.PaperSize(gotenberg.A4)
req.Margins(gotenberg.NoMargins)
req.Scale(0.75)
req.PreferCssPageSize(false)
req.OmitBackground(false)
req.PrintBackground(true)
//req.UserAgent("")
req.FailOnConsoleExceptions(true)
req.EmulatedMediaType("print")
req.PDFFormat("PDF/A-1a")
req.ExtraHttpHeaders("{\"MyHeader\": \"MyValue\"}")

// store method allows you to... store the resulting PDF in a particular destination.
client.Store(ctx, 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, err := client.Post(ctx, req)
check(err)

bb, err := io.ReadAll(resp.Body)
check(err)

For more complete guides read the documentation.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// A0 paper size.
	A0 = [2]float64{33.1, 46.8}
	// A1 paper size.
	A1 = [2]float64{23.4, 33.1}
	// A2 paper size.
	A2 = [2]float64{16.54, 23.4}
	// A3 paper size.
	A3 = [2]float64{11.7, 16.5}
	// A4 paper size.
	A4 = [2]float64{8.27, 11.7}
	// A5 paper size.
	A5 = [2]float64{5.8, 8.3}
	// A6 paper size.
	A6 = [2]float64{4.1, 5.8}
	// Letter paper size.
	Letter = [2]float64{8.5, 11}
	// Legal paper size.
	Legal = [2]float64{8.5, 14}
	// Tabloid paper size.
	Tabloid = [2]float64{11, 17}
	// Ledger paper size.
	Ledger = [2]float64{17, 11}
)

Paper Sizes

View Source
var (
	// NoMargins removes margins.
	NoMargins = [4]float64{0, 0, 0, 0}
	// NormalMargins uses 1 inche margins.
	NormalMargins = [4]float64{1, 1, 1, 1}
	// LargeMargins uses 2 inche margins.
	LargeMargins = [4]float64{2, 2, 2, 2}
)

nolint:gochecknoglobals

Functions

This section is empty.

Types

type Client

type Client struct {
	Hostname   string
	HTTPClient *http.Client
}

func NewClient

func NewClient(hostname string, httpClient *http.Client) *Client

func (*Client) Post

func (c *Client) Post(ctx context.Context, req Request) (*http.Response, error)

func (*Client) Store

func (c *Client) Store(ctx context.Context, req Request, dest string) error

type ConvertHTMLRequest

type ConvertHTMLRequest struct {
	// contains filtered or unexported fields
}

func NewConvertHTMLRequest

func NewConvertHTMLRequest(index Document) *ConvertHTMLRequest

func (*ConvertHTMLRequest) Assets

func (req *ConvertHTMLRequest) Assets(assets ...Document)

Assets sets assets form files.

func (ConvertHTMLRequest) EmulatedMediaType

func (req ConvertHTMLRequest) EmulatedMediaType(mediaType string)

EmulatedMediaType sets emulatedMediaType form field

func (ConvertHTMLRequest) ExtraHttpHeaders

func (req ConvertHTMLRequest) ExtraHttpHeaders(headers string)

ExtraHttpHeaders sets extraHttpHeaders form field e.g.: extraHttpHeaders="{\"MyHeader\": \"MyValue\"}"

func (ConvertHTMLRequest) FailOnConsoleExceptions

func (req ConvertHTMLRequest) FailOnConsoleExceptions(isFailOnConsoleExceptions bool)

FailOnConsoleExceptions sets failOnConsoleExceptions form field

func (ConvertHTMLRequest) Footer

func (req ConvertHTMLRequest) Footer(footer Document)

Footer sets footer form file.

func (ConvertHTMLRequest) Header

func (req ConvertHTMLRequest) Header(header Document)

Header sets header form file.

func (ConvertHTMLRequest) Landscape

func (req ConvertHTMLRequest) Landscape(isLandscape bool)

Landscape sets landscape form field.

func (ConvertHTMLRequest) Margins

func (req ConvertHTMLRequest) Margins(margins [4]float64)

Margins sets marginTop, marginBottom, marginLeft and marginRight form fields.

func (ConvertHTMLRequest) NativePageRanges

func (req ConvertHTMLRequest) NativePageRanges(ranges string)

NativePageRanges sets pageRanges form field.

func (ConvertHTMLRequest) OmitBackground

func (req ConvertHTMLRequest) OmitBackground(isOmitBackground bool)

OmitBackground sets omitBackground form field

func (ConvertHTMLRequest) PDFFormat

func (req ConvertHTMLRequest) PDFFormat(format string)

PDFFormat sets pdfFormat form field

func (ConvertHTMLRequest) PaperSize

func (req ConvertHTMLRequest) PaperSize(size [2]float64)

PaperSize sets paperWidth and paperHeight form fields.

func (ConvertHTMLRequest) PreferCssPageSize

func (req ConvertHTMLRequest) PreferCssPageSize(isPreferCssPageSize bool)

PreferCssPageSize sets preferCssPageSize form field

func (ConvertHTMLRequest) PrintBackground

func (req ConvertHTMLRequest) PrintBackground(isPrintBackground bool)

PrintBackground sets printBackground form field

func (ConvertHTMLRequest) Scale

func (req ConvertHTMLRequest) Scale(scaleFactor float64)

Scale sets scale form field

func (ConvertHTMLRequest) UserAgent

func (req ConvertHTMLRequest) UserAgent(agent string)

UserAgent sets userAgent form field e.g.: userAgent="Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38

(KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"

func (ConvertHTMLRequest) WaitDelay

func (req ConvertHTMLRequest) WaitDelay(d time.Duration)

WaitDelay sets waitDelay form field

func (ConvertHTMLRequest) WaitForExpression

func (req ConvertHTMLRequest) WaitForExpression(expression string)

WaitForExpression sets waitForExpression form field

type Document

type Document interface {
	Filename() string
	Reader() (io.ReadCloser, error)
}

func NewDocumentFromBytes

func NewDocumentFromBytes(filename string, data []byte) (Document, error)

NewDocumentFromBytes creates a Document from bytes.

func NewDocumentFromPath

func NewDocumentFromPath(filename, fpath string) (Document, error)

NewDocumentFromPath creates a Document from a file path.

func NewDocumentFromString

func NewDocumentFromString(filename, data string) (Document, error)

NewDocumentFromString creates a Document from a string.

type Request

type Request interface {
	// contains filtered or unexported methods
}

Directories

Path Synopsis
Package test contains useful functions used across tests.
Package test contains useful functions used across tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL