gotenberg

package
Version: v7.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package gotenberg provides most of the logic of the module system.

caddyserver/caddy, licensed under the Apache License 2.0, has significantly inspired this module system.

More details are available on https://caddyserver.com/.

Index

Constants

View Source
const (
	FormatPDFA1a string = "PDF/A-1a"
	FormatPDFA1b string = "PDF/A-1b"
	FormatPDFA2a string = "PDF/A-2a"
	FormatPDFA2b string = "PDF/A-2b"
	FormatPDFA2u string = "PDF/A-2u"
	FormatPDFA3a string = "PDF/A-3a"
	FormatPDFA3b string = "PDF/A-3b"
	FormatPDFA3u string = "PDF/A-3u"
)

Variables

View Source
var (
	// ErrPDFEngineMethodNotAvailable happens if a PDFEngine method is not
	// available in the implementation.
	ErrPDFEngineMethodNotAvailable = errors.New("method not available")

	// ErrPDFFormatNotAvailable happens if a PDFEngine Convert's method does
	// not handle a specific format.
	ErrPDFFormatNotAvailable = errors.New("PDF format not available")
)

Functions

func MkdirAll

func MkdirAll() (string, error)

MkdirAll creates a random directory based on the temporary path and returns its absolute path.

func MustRegisterModule

func MustRegisterModule(mod Module)

MustRegisterModule registers a module.

To register a module, create an init() method in the module main go file:

func init() {
	gotenberg.MustRegisterModule(YourModule{})
}

Then, in the main command (github.com/gotenberg/gotenberg/v7/cmd/gotenberg), import the module:

imports (
	// Gotenberg modules.
	_ "your_module_path"
)

func NewDirPath

func NewDirPath() string

NewDirPath returns a random absolute path based on the temporary path.

func TmpPath

func TmpPath() string

TmpPath returns the default directory to use for temporary files and directories. Most if not all files and directories created by the application and its dependencies must be based on this default directory.

Types

type App

type App interface {
	Start() error
	// StartupMessage returns a custom message to display on startup. If it
	// returns an empty string, a default startup message is used instead.
	StartupMessage() string
	Stop(ctx context.Context) error
}

App is a module interface for modules which can be started or stopped by the application.

type Cmd

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

Cmd wraps an exec.Cmd.

func Command

func Command(logger *zap.Logger, binPath string, args ...string) Cmd

Command creates a Cmd without a context. It configures the internal exec.Cmd of Cmd so that we may kill its unix process and all its children without creating orphans.

See https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773.

func CommandContext

func CommandContext(ctx context.Context, logger *zap.Logger, binPath string, args ...string) (Cmd, error)

CommandContext creates a Cmd with a context. It configures the internal exec.Cmd of Cmd so that we may kill its unix process and all its children without creating orphans.

See https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773.

func (Cmd) Exec

func (cmd Cmd) Exec() error

Exec executes the command and wait for its completion or until the context is done. In any case, it kills the unix process and all its children.

func (Cmd) Kill

func (cmd Cmd) Kill() error

Kill kills the unix process and all its children without creating orphans.

See https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773.

func (Cmd) Start

func (cmd Cmd) Start() error

Start starts the command but does not wait for its completion.

type Context

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

Context is a struct which helps initializing modules. When provisioning, a module may use the context to get other modules that it needs internally.

func NewContext

func NewContext(
	flags ParsedFlags,
	descriptors []ModuleDescriptor,
) *Context

NewContext creates a Context. In a module, prefer the Provisioner interface to get a Context.

func (*Context) Module

func (ctx *Context) Module(kind interface{}) (interface{}, error)

Module returns a module which satisfies the requested interface.

func (m *YourModule) Provision(ctx *gotenberg.Context) error {
	mod, _ := ctx.Module(new(ModuleInterface))
	real := mod.(ModuleInterface)
}

If the module has not yet been initialized, this method initializes it. Otherwise, returns the already initialized instance.

func (*Context) Modules

func (ctx *Context) Modules(kind interface{}) ([]interface{}, error)

Modules returns the list of modules which satisfies the requested interface.

func (m *YourModule) Provision(ctx *gotenberg.Context) error {
	mods, _ := ctx.Modules(new(ModuleInterface))
	for _, mod := range mods {
		real := mod.(ModuleInterface)
		// ...
	}
}

If one or more modules have not yet been initialized, this method initializes them. Otherwise, returns the already initialized instances.

func (Context) ParsedFlags

func (ctx Context) ParsedFlags() ParsedFlags

ParsedFlags returns the parsed flags.

func (m *YourModule) Provision(ctx *gotenberg.Context) error {
	flags := ctx.ParsedFlags()
	m.foo = flags.RequiredString("foo")
}

type LoggerProvider

type LoggerProvider interface {
	Logger(mod Module) (*zap.Logger, error)
}

LoggerProvider is a module interface which exposes a method for creating a zap.Logger for other modules.

func (m *YourModule) Provision(ctx *gotenberg.Context) error {
	provider, _ := ctx.Module(new(gotenberg.LoggerProvider))
	logger, _   := provider.(gotenberg.LoggerProvider).Logger(m)
}

type Module

type Module interface {
	Descriptor() ModuleDescriptor
}

Module is a sort of plugin which adds new functionalities to the application or other modules.

type YourModule struct {
	property string
}

func (YourModule) Descriptor() gotenberg.ModuleDescriptor {
	return gotenberg.ModuleDescriptor{
		ID: "your_module",
		FlagSet: func() *flag.FlagSet {
			fs := flag.NewFlagSet("your_module", flag.ExitOnError)
			fs.String("your_module-property", "default value", "flag description")

			return fs
		}(),
		New: func() gotenberg.Module { return new(YourModule) },
	}
}

type ModuleDescriptor

type ModuleDescriptor struct {
	// ID is the unique name (snake case) of the module.
	// Required.
	ID string

	// FlagSet is the definition of the flags of the module.
	// Optional.
	FlagSet *flag.FlagSet

	// New returns a new and empty instance of the module's type.
	// Required.
	New func() Module
}

ModuleDescriptor describes your module for the application.

func GetModuleDescriptors

func GetModuleDescriptors() []ModuleDescriptor

GetModuleDescriptors returns the descriptors of all registered modules.

type PDFEngine

type PDFEngine interface {
	// Merge merges the given PDFs into a unique PDF. The pages' order reflects
	// order of the given files.
	Merge(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error

	// Convert converts the given PDF to a specific PDF format.
	Convert(ctx context.Context, logger *zap.Logger, format, inputPath, outputPath string) error
}

PDFEngine is a module interface which exposes methods for manipulating one or more PDFs. Implementations may abstract powerful tools like PDFtk, or fulfill those methods contracts in Golang directly.

type PDFEngineProvider

type PDFEngineProvider interface {
	PDFEngine() (PDFEngine, error)
}

PDFEngineProvider is a module interface which exposes a method for creating a PDFEngine for other modules.

func (m *YourModule) Provision(ctx *gotenberg.Context) error {
	provider, _  := ctx.Module(new(gotenberg.PDFEngineProvider))
	pdfengines, _ := provider.(gotenberg.PDFEngineProvider).PDFEngine()
}

type ParsedFlags

type ParsedFlags struct {
	*flag.FlagSet
}

ParsedFlags wraps a flag.FlagSet so that retrieving the typed values is easier.

func (*ParsedFlags) MustBool

func (f *ParsedFlags) MustBool(name string) bool

MustBool returns the boolean value of a flag given by name. It panics if an error occurs.

func (*ParsedFlags) MustDuration

func (f *ParsedFlags) MustDuration(name string) time.Duration

MustDuration returns the time.Duration value of a flag given by name. It panics if an error occurs.

func (*ParsedFlags) MustFloat64

func (f *ParsedFlags) MustFloat64(name string) float64

MustFloat64 returns the float value of a flag given by name. It panics if an error occurs.

func (*ParsedFlags) MustHumanReadableBytesString

func (f *ParsedFlags) MustHumanReadableBytesString(name string) string

MustHumanReadableBytesString returns the human-readable bytes string of a flag given by name. It panics if an error occurs.

func (*ParsedFlags) MustInt

func (f *ParsedFlags) MustInt(name string) int

MustInt returns the int value of a flag given by name. It panics if an error occurs.

func (*ParsedFlags) MustRegexp

func (f *ParsedFlags) MustRegexp(name string) *regexp.Regexp

MustRegexp returns the regular expression of a flag given by name. It panics if an error occurs.

func (*ParsedFlags) MustString

func (f *ParsedFlags) MustString(name string) string

MustString returns the string value of a flag given by name. It panics if an error occurs.

func (*ParsedFlags) MustStringSlice

func (f *ParsedFlags) MustStringSlice(name string) []string

MustStringSlice returns the string slice value of a flag given by name. It panics if an error occurs.

type Provisioner

type Provisioner interface {
	Provision(*Context) error
}

Provisioner is a module interface for modules which have to be initialized according to flags, environment variables, the context, etc.

type Validator

type Validator interface {
	Validate() error
}

Validator is a module interface for modules which have to be validated after provisioning.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL