Documentation
¶
Overview ¶
Package pdf creates PDFs from HTML and Laravel Go views.
Index ¶
- Variables
- func BlankPDF() []byte
- func InlinedImage(ctx context.Context, path string, client *http.Client) (template.HTML, error)
- func PageBreak() template.HTML
- func PageNumber() template.HTML
- func TotalPages() template.HTML
- type BrowsershotDriver
- type Builder
- func (b *Builder) Apply(fn func(*Builder)) *Builder
- func (b *Builder) Attachment(ctx context.Context) (mail.Attachment, error)
- func (b *Builder) Base64(ctx context.Context) (string, error)
- func (b *Builder) Bytes(ctx context.Context) ([]byte, error)
- func (b *Builder) Cache(ttl time.Duration, key ...string) *Builder
- func (b *Builder) Contains(ctx context.Context, text ...string) (bool, error)
- func (b *Builder) Disk(name string) *Builder
- func (b *Builder) DocumentOutline() *Builder
- func (b *Builder) DontCache() *Builder
- func (b *Builder) Download(name string) *Builder
- func (b *Builder) Driver(name string) *Builder
- func (b *Builder) Encrypt(settings Encryption) *Builder
- func (b *Builder) FooterHTML(html string) *Builder
- func (b *Builder) FooterView(name string, data any) *Builder
- func (b *Builder) Format(format Format) *Builder
- func (b *Builder) HTML(html string) *Builder
- func (b *Builder) HeaderHTML(html string) *Builder
- func (b *Builder) HeaderView(name string, data any) *Builder
- func (b *Builder) Headers(headers http.Header) *Builder
- func (b *Builder) Inline(name string) *Builder
- func (b *Builder) Job(ctx context.Context, path string) (any, error)
- func (b *Builder) Landscape() *Builder
- func (b *Builder) Margins(top, right, bottom, left float64, unit Unit) *Builder
- func (b *Builder) Meta(m Metadata) *Builder
- func (b *Builder) Name(name string) *Builder
- func (b *Builder) Orientation(o Orientation) *Builder
- func (b *Builder) PageRanges(ranges string) *Builder
- func (b *Builder) PaperSize(width, height float64, unit Unit) *Builder
- func (b *Builder) Portrait() *Builder
- func (b *Builder) PostProcess(p PostProcessor) *Builder
- func (b *Builder) RenderRequest(ctx context.Context) (Request, error)
- func (b *Builder) Response(ctx context.Context) *web.Response
- func (b *Builder) Save(ctx context.Context, path string) error
- func (b *Builder) SaveQueued(ctx context.Context, path string, dispatch ...*queue.PendingDispatch) (queue.JobID, error)
- func (b *Builder) Scale(scale float64) *Builder
- func (b *Builder) SetDriver(d Driver) *Builder
- func (b *Builder) Tagged() *Builder
- func (b *Builder) View(name string, data any) *Builder
- func (b *Builder) WaitUntilReady(expression string, timeout time.Duration) *Builder
- func (b *Builder) When(condition bool, fn func(*Builder)) *Builder
- func (b *Builder) WithBrowsershot(fn func(*browsershot.Browsershot)) *Builder
- type Cache
- type ChromeDriver
- type CloudflareDriver
- type Config
- type DOMPDFDriver
- type DefaultEncrypter
- type Driver
- type DriverFunc
- type Encrypter
- type Encryption
- type Factory
- type FakeDriver
- type Format
- type GenerateJob
- type GotenbergDriver
- type HTTPError
- type LaravelCache
- type Margins
- type Metadata
- type Options
- type Orientation
- type Package
- type PaperSize
- type Permission
- type PostProcessor
- type ReadinessDriver
- type Request
- type Unit
- type WeasyPrintDriver
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalid = errors.New("pdf: invalid input") ErrUnsupported = errors.New("pdf: unsupported option") )
Functions ¶
func InlinedImage ¶
InlinedImage reads trusted paths or URLs. Never pass untrusted visitor input.
func PageNumber ¶
func TotalPages ¶
Types ¶
type BrowsershotDriver ¶
type BrowsershotDriver struct {
NodeBinary string
NpmBinary string
ChromePath string
NodeModulesPath string
IncludePath string
BinPath string
TempPath string
WriteOptionsToFile bool
NoSandbox bool
Timeout time.Duration
ConfigurationKey string
Customize func(*browsershot.Browsershot) `json:"-"`
}
BrowsershotDriver uses the released Node/Puppeteer backend.
func (BrowsershotDriver) Identity ¶
func (d BrowsershotDriver) Identity() string
func (BrowsershotDriver) SupportsReadiness ¶
func (BrowsershotDriver) SupportsReadiness() bool
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is mutable and must not be shared between goroutines.
func (*Builder) Attachment ¶
func (*Builder) DocumentOutline ¶
func (*Builder) Encrypt ¶
func (b *Builder) Encrypt(settings Encryption) *Builder
func (*Builder) FooterHTML ¶
func (*Builder) HeaderHTML ¶
func (*Builder) Job ¶
Job returns the configured replacement job for chains or explicit queue dispatch.
func (*Builder) Orientation ¶
func (b *Builder) Orientation(o Orientation) *Builder
func (*Builder) PageRanges ¶
func (*Builder) PostProcess ¶
func (b *Builder) PostProcess(p PostProcessor) *Builder
func (*Builder) RenderRequest ¶
func (*Builder) SaveQueued ¶
func (*Builder) WaitUntilReady ¶
func (*Builder) WithBrowsershot ¶
func (b *Builder) WithBrowsershot(fn func(*browsershot.Browsershot)) *Builder
type ChromeDriver ¶
type ChromeDriver struct {
Binary string
NoSandbox bool
Timeout time.Duration
UserDataDir string
Flags map[string]any
Environment []string
}
ChromeDriver controls Chrome directly, without Node.js or PHP.
func (ChromeDriver) Identity ¶
func (d ChromeDriver) Identity() string
func (ChromeDriver) SupportsReadiness ¶
func (ChromeDriver) SupportsReadiness() bool
type CloudflareDriver ¶
type CloudflareDriver struct {
Token string
AccountID string
// Endpoint overrides the API URL, for proxies and tests.
Endpoint string
Client *http.Client
}
func (CloudflareDriver) Identity ¶
func (d CloudflareDriver) Identity() string
type Config ¶
type Config struct {
Driver string
Drivers map[string]Driver
Defaults func(*Builder)
Cache Cache
AutomaticCache bool
CacheTTL time.Duration
CachePrefix string
CacheStore string
Encrypter Encrypter
// JobFactory must return the same serializable job type for every input.
JobFactory func(GenerateJob) any
Browsershot BrowsershotDriver
Chrome ChromeDriver
Cloudflare CloudflareDriver
Gotenberg GotenbergDriver
WeasyPrint WeasyPrintDriver
DOMPDF DOMPDFDriver
}
Config is applied after environment defaults, once per application.
type DOMPDFDriver ¶
type DOMPDFDriver struct {
PHP string
Autoload string
RemoteEnabled bool
Chroot string
FontDir string
FontCache string
Timeout time.Duration
}
DOMPDFDriver uses an application's Composer installation of dompdf/dompdf. There is no native Go DOMPDF engine. Other drivers do not need PHP.
func (DOMPDFDriver) Identity ¶
func (d DOMPDFDriver) Identity() string
type DefaultEncrypter ¶
type DefaultEncrypter struct{}
DefaultEncrypter uses PDF AES-256 revision 6, including object streams.
func (DefaultEncrypter) Encrypt ¶
func (DefaultEncrypter) Encrypt(ctx context.Context, data []byte, settings Encryption) ([]byte, error)
func (DefaultEncrypter) Identity ¶
func (DefaultEncrypter) Identity() string
type DriverFunc ¶
type DriverFunc struct {
Name string
Render func(context.Context, Request) ([]byte, error)
Readiness bool
}
DriverFunc adapts a renderer for custom drivers and application tests.
func (DriverFunc) Identity ¶
func (d DriverFunc) Identity() string
func (DriverFunc) SupportsReadiness ¶
func (d DriverFunc) SupportsReadiness() bool
type Encryption ¶
type Encryption struct {
UserPassword string `json:"user_password"`
OwnerPassword string `json:"owner_password"`
Permissions []Permission `json:"permissions,omitzero"`
}
Encryption grants all permissions when Permissions is nil, none when empty. Queue backends and caches must protect serialized passwords and PDF content.
type Factory ¶
type Factory struct {
// contains filtered or unexported fields
}
Factory is immutable after construction and safe for concurrent use when its adapters are.
func NewFactory ¶
NewFactory also works outside Laravel for HTML and local file output.
func (*Factory) HTML ¶
Example ¶
package main
import (
"bytes"
"context"
"fmt"
pdf "github.com/spatie/laravel-pdf-go"
)
func main() {
f, _ := pdf.NewFactory(pdf.Config{Driver: "fake", Drivers: map[string]pdf.Driver{"fake": &pdf.FakeDriver{}}})
data, _ := f.HTML("<h1>Hello world</h1>").Format(pdf.A4).Bytes(context.Background())
fmt.Println(bytes.HasPrefix(data, []byte("%PDF-")))
}
Output: true
type FakeDriver ¶
type FakeDriver struct {
// contains filtered or unexported fields
}
FakeDriver records rendered HTML and returns a valid blank one-page PDF. Set Config.Drivers and Config.Driver explicitly to install it in one application.
func (*FakeDriver) Identity ¶
func (*FakeDriver) Identity() string
func (*FakeDriver) Requests ¶
func (f *FakeDriver) Requests() []Request
func (*FakeDriver) SupportsReadiness ¶
func (*FakeDriver) SupportsReadiness() bool
type GenerateJob ¶
type GenerateJob struct {
Request Request `json:"request"`
Metadata Metadata `json:"metadata"`
Encryption *Encryption `json:"encryption,omitempty"`
Path string `json:"path"`
Disk string `json:"disk"`
Driver string `json:"driver"`
DriverIdentity string `json:"driver_identity"`
EncrypterIdentity string `json:"encrypter_identity"`
}
GenerateJob holds only serializable data. A worker uses its application's configuration.
type GotenbergDriver ¶
func (GotenbergDriver) Identity ¶
func (d GotenbergDriver) Identity() string
func (GotenbergDriver) SupportsReadiness ¶
func (GotenbergDriver) SupportsReadiness() bool
type HTTPError ¶
type HTTPError struct{ StatusCode int }
HTTPError intentionally omits provider bodies, which can contain HTML and credentials.
type LaravelCache ¶
type LaravelCache struct{ Store string }
type Options ¶
type Options struct {
Format Format `json:"format"`
PaperSize *PaperSize `json:"paper_size,omitempty"`
Margins *Margins `json:"margins,omitempty"`
Orientation Orientation `json:"orientation"`
Scale float64 `json:"scale"`
PageRanges string `json:"page_ranges"`
Tagged bool `json:"tagged"`
DocumentOutline bool `json:"document_outline"`
WaitForReady string `json:"wait_for_ready"`
ReadyTimeout time.Duration `json:"-"`
}
Options is serializable rendering input. Zero values keep engine defaults.
func (Options) MarshalJSON ¶
MarshalJSON represents durations as nanoseconds in both JSON implementations.
func (*Options) UnmarshalJSON ¶
type Orientation ¶
type Orientation string
const ( Portrait Orientation = "portrait" Landscape Orientation = "landscape" )
type Permission ¶
type Permission string
const ( Annotate Permission = "annot-forms" Assemble Permission = "assemble" Copy Permission = "copy" Extract Permission = "extract" FillForms Permission = "fill-forms" Modify Permission = "modify" Print Permission = "print" PrintHighResolution Permission = "print-high" )
type PostProcessor ¶
type ReadinessDriver ¶
type ReadinessDriver interface{ SupportsReadiness() bool }
ReadinessDriver marks renderers that evaluate Options.WaitForReady.
type Request ¶
type Request struct {
HTML string `json:"html"`
HeaderHTML string `json:"header_html"`
Options Options `json:"options"`
}
Request contains rendered HTML, ready for a driver or queue worker.
type WeasyPrintDriver ¶
WeasyPrintDriver runs WeasyPrint directly. Arguments never pass through a shell.
func (WeasyPrintDriver) Identity ¶
func (d WeasyPrintDriver) Identity() string
