gotenberg

package module
v7.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2022 License: MIT Imports: 13 Imported by: 0

README

⚠️ Not working for Gotenberg < 7 any more⚠️

Gotenberg Go client

A simple Go client for interacting with a Gotenberg API.

https://github.com/thecodingmachine/gotenberg-go-client

Note: 更新为gotenberg v7的API,如html/url/markdown和office; Todo: 由于v7中merge操作被集成到了office接口中,此客户端需要进一步适配。

Install

$ go get -u github.com/thecodingmachine/gotenberg-go-client/v7

Usage

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", "<html>Foo</html>")
// ... or from bytes.
index, _ := gotenberg.NewDocumentFromBytes("index.html", []byte("<html>Foo</html>"))

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)
req.Scale(0.75)

// 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.

Badges

Travis CI GoDoc Go Report Card

Documentation

Overview

Package gotenberg is a Go client for interacting with a Gotenberg API.

For more complete usages, head to the documentation: https://thecodingmachine.github.io/gotenberg/

Index

Constants

This section is empty.

Variables

View Source
var (
	// 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}
)

nolint: gochecknoglobals

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
}

Client facilitates interacting with the Gotenberg API.

func (*Client) Post

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

Post sends a request to the Gotenberg API and returns the response.

func (*Client) PostContext

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

PostContext sends a request to the Gotenberg API and returns the response. The created HTTP request can be canceled by the passed context.

func (*Client) Store

func (c *Client) Store(req Request, dest string) error

Store creates the resulting PDF to given destination.

func (*Client) StoreContext

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

StoreContext creates the resulting PDF to given destination. The created HTTP request can be canceled by the passed context.

type Document

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

Document reprents a file which will be send to the Gotenberg API.

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 HTMLRequest

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

HTMLRequest facilitates HTML conversion with the Gotenberg API.

func NewHTMLRequest

func NewHTMLRequest(index Document) *HTMLRequest

NewHTMLRequest create HTMLRequest.

func (*HTMLRequest) Assets

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

Assets sets assets form files.

func (HTMLRequest) Footer

func (req HTMLRequest) Footer(footer Document)

Footer sets footer form file.

func (HTMLRequest) GoogleChromeRpccBufferSize

func (req HTMLRequest) GoogleChromeRpccBufferSize(bufferSize int64)

GoogleChromeRpccBufferSize sets googleChromeRpccBufferSize form field.

func (HTMLRequest) Header

func (req HTMLRequest) Header(header Document)

Header sets header form file.

func (HTMLRequest) Landscape

func (req HTMLRequest) Landscape(isLandscape bool)

Landscape sets landscape form field.

func (HTMLRequest) Margins

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

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

func (HTMLRequest) PageRanges

func (req HTMLRequest) PageRanges(ranges string)

PageRanges sets pageRanges form field.

func (HTMLRequest) PaperSize

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

PaperSize sets paperWidth and paperHeight form fields.

func (HTMLRequest) Scale

func (req HTMLRequest) Scale(scaleFactor float64)

Scale sets scale form field

func (HTMLRequest) WaitDelay

func (req HTMLRequest) WaitDelay(delay float64)

WaitDelay sets waitDelay form field.

type MarkdownRequest

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

MarkdownRequest facilitates Markdown conversion with the Gotenberg API.

func NewMarkdownRequest

func NewMarkdownRequest(index Document, markdowns ...Document) *MarkdownRequest

NewMarkdownRequest create MarkdownRequest.

func (*MarkdownRequest) Assets

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

Assets sets assets form files.

func (MarkdownRequest) Footer

func (req MarkdownRequest) Footer(footer Document)

Footer sets footer form file.

func (MarkdownRequest) GoogleChromeRpccBufferSize

func (req MarkdownRequest) GoogleChromeRpccBufferSize(bufferSize int64)

GoogleChromeRpccBufferSize sets googleChromeRpccBufferSize form field.

func (MarkdownRequest) Header

func (req MarkdownRequest) Header(header Document)

Header sets header form file.

func (MarkdownRequest) Landscape

func (req MarkdownRequest) Landscape(isLandscape bool)

Landscape sets landscape form field.

func (MarkdownRequest) Margins

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

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

func (MarkdownRequest) PageRanges

func (req MarkdownRequest) PageRanges(ranges string)

PageRanges sets pageRanges form field.

func (MarkdownRequest) PaperSize

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

PaperSize sets paperWidth and paperHeight form fields.

func (MarkdownRequest) Scale

func (req MarkdownRequest) Scale(scaleFactor float64)

Scale sets scale form field

func (MarkdownRequest) WaitDelay

func (req MarkdownRequest) WaitDelay(delay float64)

WaitDelay sets waitDelay form field.

type MergeRequest

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

MergeRequest facilitates merging PDF with the Gotenberg API.

func NewMergeRequest

func NewMergeRequest(pdfs ...Document) *MergeRequest

NewMergeRequest create MergeRequest.

func (MergeRequest) AddWebhookURLHTTPHeader

func (req MergeRequest) AddWebhookURLHTTPHeader(key, value string)

AddWebhookURLHTTPHeader add a webhook custom HTTP header.

func (MergeRequest) ResultFilename

func (req MergeRequest) ResultFilename(filename string)

ResultFilename sets resultFilename form field.

func (MergeRequest) WaitTimeout

func (req MergeRequest) WaitTimeout(timeout float64)

WaitTimeout sets waitTimeout form field.

func (MergeRequest) WebhookURL

func (req MergeRequest) WebhookURL(url string)

WebhookURL sets webhookURL form field.

func (MergeRequest) WebhookURLTimeout

func (req MergeRequest) WebhookURLTimeout(timeout float64)

WebhookURLTimeout sets webhookURLTimeout form field.

type OfficeRequest

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

OfficeRequest facilitates Office documents conversion with the Gotenberg API.

func NewOfficeRequest

func NewOfficeRequest(docs ...Document) *OfficeRequest

NewOfficeRequest create OfficeRequest.

func (OfficeRequest) AddWebhookURLHTTPHeader

func (req OfficeRequest) AddWebhookURLHTTPHeader(key, value string)

AddWebhookURLHTTPHeader add a webhook custom HTTP header.

func (*OfficeRequest) Landscape

func (req *OfficeRequest) Landscape(isLandscape bool)

Landscape sets landscape form field.

func (*OfficeRequest) PageRanges

func (req *OfficeRequest) PageRanges(ranges string)

PageRanges sets pageRanges form field.

func (OfficeRequest) ResultFilename

func (req OfficeRequest) ResultFilename(filename string)

ResultFilename sets resultFilename form field.

func (OfficeRequest) WaitTimeout

func (req OfficeRequest) WaitTimeout(timeout float64)

WaitTimeout sets waitTimeout form field.

func (OfficeRequest) WebhookURL

func (req OfficeRequest) WebhookURL(url string)

WebhookURL sets webhookURL form field.

func (OfficeRequest) WebhookURLTimeout

func (req OfficeRequest) WebhookURLTimeout(timeout float64)

WebhookURLTimeout sets webhookURLTimeout form field.

type Request

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

Request is a type for sending form values and form files to the Gotenberg API.

type URLRequest

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

URLRequest facilitates remote URL conversion with the Gotenberg API.

func NewURLRequest

func NewURLRequest(url string) *URLRequest

NewURLRequest create URLRequest.

func (*URLRequest) AddRemoteURLHTTPHeader

func (req *URLRequest) AddRemoteURLHTTPHeader(key, value string)

AddRemoteURLHTTPHeader add a remote URL custom HTTP header.

func (URLRequest) Footer

func (req URLRequest) Footer(footer Document)

Footer sets footer form file.

func (URLRequest) GoogleChromeRpccBufferSize

func (req URLRequest) GoogleChromeRpccBufferSize(bufferSize int64)

GoogleChromeRpccBufferSize sets googleChromeRpccBufferSize form field.

func (URLRequest) Header

func (req URLRequest) Header(header Document)

Header sets header form file.

func (URLRequest) Landscape

func (req URLRequest) Landscape(isLandscape bool)

Landscape sets landscape form field.

func (URLRequest) Margins

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

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

func (URLRequest) PageRanges

func (req URLRequest) PageRanges(ranges string)

PageRanges sets pageRanges form field.

func (URLRequest) PaperSize

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

PaperSize sets paperWidth and paperHeight form fields.

func (URLRequest) Scale

func (req URLRequest) Scale(scaleFactor float64)

Scale sets scale form field

func (URLRequest) WaitDelay

func (req URLRequest) WaitDelay(delay float64)

WaitDelay sets waitDelay form field.

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