pdf

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 11 Imported by: 0

README

togo

togo-framework/pdf

marketplace pkg.go.dev MIT

Part of the togo framework.

Install

togo install togo-framework/pdf

togo

togo · pdf

HTML → PDF for togo apps. A swappable render engine (default: headless Chromium via chromedp), a Go API, and a REST endpoint.

togo install togo-framework/pdf

Blank-importing the package registers the pdf provider with the kernel. Pick the engine with PDF_DRIVER (default chromium). The Chromium driver needs a Chrome/Chromium binary available on the host.

Use it

Go:

import "github.com/togo-framework/pdf"

svc, _ := pdf.FromKernel(k)
out, err := svc.Render(ctx, "<h1>Invoice</h1><p>Thanks!</p>", pdf.Options{Landscape: false})

RESTPOST /api/pdfapplication/pdf:

curl -X POST localhost:8080/api/pdf \
  -H 'content-type: application/json' \
  -d '{"html":"<h1>Hello</h1>","filename":"hello.pdf"}' \
  --output hello.pdf
# or render a live URL:
curl -X POST localhost:8080/api/pdf -d '{"options":{"url":"https://to-go.dev"}}' --output page.pdf

Options: url, landscape, printBackground, paperWidth/paperHeight (inches, default A4), scale, timeoutSeconds.

Add an engine

Implement pdf.Renderer and pdf.RegisterDriver("wkhtmltopdf", factory) in your plugin's init(), then set PDF_DRIVER=wkhtmltopdf.

MIT © togo


Premium sponsors

ID8 Media  ·  One Studio

Support togo — become a sponsor.

Documentation

Overview

Package pdf is togo's HTML→PDF plugin. It exposes a swappable Renderer driver (default: headless Chromium via chromedp), a Go API pdf.Render(...), and a REST endpoint POST /api/pdf. Drivers register via pdf.RegisterDriver; pick one with PDF_DRIVER (default "chromium").

Install: `togo install togo-framework/pdf` (blank-import registers it).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterDriver

func RegisterDriver(name string, f DriverFactory)

RegisterDriver registers a PDF engine by name (call from a plugin's init()).

Types

type DriverFactory

type DriverFactory func(k *togo.Kernel) (Renderer, error)

DriverFactory builds a Renderer from the kernel (env-configured).

type Options

type Options struct {
	// URL renders a live page instead of the HTML body when set.
	URL string `json:"url,omitempty"`
	// Landscape orientation. Default false (portrait).
	Landscape bool `json:"landscape,omitempty"`
	// PrintBackground includes background graphics. Default true.
	PrintBackground *bool `json:"printBackground,omitempty"`
	// Paper size in inches (PaperWidth x PaperHeight). 0 = A4 (8.27 x 11.69).
	PaperWidth  float64 `json:"paperWidth,omitempty"`
	PaperHeight float64 `json:"paperHeight,omitempty"`
	// Margins in inches.
	MarginTop, MarginBottom, MarginLeft, MarginRight float64 `json:"-"`
	// Scale of the page rendering (0.1–2). 0 = 1.0.
	Scale float64 `json:"scale,omitempty"`
	// TimeoutSeconds caps a render. 0 = 30s.
	TimeoutSeconds int `json:"timeoutSeconds,omitempty"`
}

Options tune a render. Zero values fall back to driver defaults (A4, default margins).

type Renderer

type Renderer interface {
	Render(ctx context.Context, html string, opts Options) ([]byte, error)
}

Renderer converts HTML (or a URL via Options) to a PDF document.

type Service

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

Service is the pdf runtime stored on the kernel (k.Get("pdf")).

func FromKernel

func FromKernel(k *togo.Kernel) (*Service, bool)

FromKernel fetches the pdf service from the kernel container.

func (*Service) Driver

func (s *Service) Driver() string

Driver returns the active engine name.

func (*Service) Render

func (s *Service) Render(ctx context.Context, html string, opts Options) ([]byte, error)

Render produces a PDF from HTML (or opts.URL).

Jump to

Keyboard shortcuts

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