print2pdf

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2024 License: MIT Imports: 16 Imported by: 2

Documentation

Overview

Package print2pdf provides functions to save a webpage as a PDF file, leveraging Chromium and the DevTools Protocol.

Requires the environment variable CHROMIUM_PATH to be set with the full path to the Chromium binary.

The StartBrowser() function starts a headless instance of Chromium, to reduce startup time in long running services (like a web server), and therefore must be called before any call PrintPDF(). These functions can (and probably should) use different contexts: the one passed to StartBrowser() closes the whole browser when done or cancelled, while the one passed to PrintPDF() closes only the tab it uses.

Index

Constants

This section is empty.

Variables

View Source
var ChromiumPath = os.Getenv("CHROMIUM_PATH")

Chromium binary path. Required.

View Source
var FormatsMap = map[string]PrintFormat{
	"Letter":  {8.5, 11},
	"Legal":   {8.5, 14},
	"Tabloid": {11, 17},
	"Ledger":  {17, 11},
	"A0":      {33.1, 46.8},
	"A1":      {23.4, 33.1},
	"A2":      {16.54, 23.4},
	"A3":      {11.7, 16.54},
	"A4":      {8.27, 11.7},
	"A5":      {5.83, 8.27},
	"A6":      {4.13, 5.83},
}

Map of format names to their dimensions, in inches. Taken from https://pptr.dev/api/puppeteer.paperformat.

Functions

func Elapsed

func Elapsed(message string) func()

Write Elapsed time, to be used with defer.

func PrintPDF added in v0.2.0

func PrintPDF(ctx context.Context, data GetPDFParams, h PDFHandler) (string, error)

Print a webpage in PDF format and write the result to the input handler. Cancelling the context will close the tab. StartBrowser() must have been called once before calling this function.

func Ptr

func Ptr[T any](v T) *T

Convert literal values to pointers.

func Running added in v0.1.9

func Running() bool

Check if the browser is still running.

func StartBrowser added in v0.3.0

func StartBrowser(ctx context.Context) error

Allocate a browser to be reused by multiple invocations, to reduce startup time. Cancelling the context will close the browser. This function must be called before starting to print PDFs.

Types

type FileHandler added in v0.2.0

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

FileHandler handles saving a file in a local path.

func NewFileHandler added in v0.2.0

func NewFileHandler(path string) (FileHandler, error)

NewFileHandler returns a new instance of FileHandler.

func (FileHandler) Close added in v0.2.0

func (fh FileHandler) Close() error

Implement io.Closer interface.

func (FileHandler) Handle added in v0.2.0

func (fh FileHandler) Handle(r io.Reader) (string, error)

Implement PDFHandler interface.

type GetPDFParams added in v0.1.8

type GetPDFParams struct {
	// URL of the webpage to save. Required.
	Url string `json:"url"`
	// Filename of the generated PDF. A ".pdf" suffix will be appended if not present. Required.
	FileName string `json:"file_name"`
	// Media type to emulate. Accepted values are "print" and "screen". Default is "print".
	Media string `json:"media,omitempty"`
	// Page format. See FormatsMap for accepted values. Default is "A4".
	Format string `json:"format,omitempty"`
	// Print background graphics. Default is true.
	Background *bool `json:"background,omitempty"`
	// Page orientation. Accepted values are "landscape" and "portrait". Default is "portrait".
	Layout string `json:"layout,omitempty"`
	// Page margins in inches. Default is all 0.
	Margins *PrintMargins `json:"margin,omitempty"`
	// Scale of the webpage rendering. Default is 1.
	Scale float64 `json:"scale,omitempty"`
}

Parameters for generating a PDF.

type PDFHandler added in v0.2.0

type PDFHandler interface {
	// Include io.Closer interface, to accomodate implementations that need it.
	io.Closer
	// Handle writes the input stream to the implemented storage location, returning a URI (local path, URL, ...) or an error if any occur.
	Handle(io.Reader) (string, error)
}

PDFHandler is an interface implementing methods to handle saving a file to a storage location.

type PrintFormat

type PrintFormat struct {
	Width  float64
	Height float64
}

Represents a print format's width and height, in inches.

type PrintMargins

type PrintMargins struct {
	Top    float64 `json:"top,omitempty"`
	Bottom float64 `json:"bottom,omitempty"`
	Left   float64 `json:"left,omitempty"`
	Right  float64 `json:"right,omitempty"`
}

Page margins of the generated PDF, in inches.

type S3Handler added in v0.2.0

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

S3Handler handles uploading a file to an AWS S3 bucket.

func NewS3Handler added in v0.2.0

func NewS3Handler(ctx context.Context, bucket, fileName string) (S3Handler, error)

NewS3Handler returns a new instance of S3Uploader.

func (S3Handler) Close added in v0.2.0

func (sh S3Handler) Close() error

Implement io.Closer interface (noop).

func (S3Handler) Handle added in v0.2.0

func (sh S3Handler) Handle(r io.Reader) (string, error)

Implement PDFHandler interface.

type StreamHandleReader added in v0.2.0

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

StreamHandleReader is a helper to read a StreamHandle returned by chromedp when printing a web page to PDF with "ReturnAsStream" transfer mode. For more information about the protocol, see:

func NewStreamHandleReader added in v0.2.0

func NewStreamHandleReader(ctx context.Context, h chromedpio.StreamHandle) *StreamHandleReader

NewStreamHandleReader returns a new insance of StreamHandleReader.

func (*StreamHandleReader) Close added in v0.2.0

func (r *StreamHandleReader) Close() error

Implement io.Closer interface.

func (*StreamHandleReader) Read added in v0.2.0

func (r *StreamHandleReader) Read(p []byte) (int, error)

Implement io.Reader interface.

type StreamHandler added in v0.2.0

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

StreamHandler handles streaming a file.

func NewStreamHandler added in v0.2.0

func NewStreamHandler(w io.Writer) StreamHandler

NewStreamHandler returns a new instance of StreamHandler, which will stream the file to the provided writer.

func (StreamHandler) Close added in v0.2.0

func (sh StreamHandler) Close() error

Implement io.Closer interface (noop).

func (StreamHandler) Handle added in v0.2.0

func (sh StreamHandler) Handle(r io.Reader) (string, error)

Implement PDFHandler interface.

type ValidationError

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

Validation error in supplied parameter.

func NewValidationError

func NewValidationError(message string) ValidationError

Create a new validation error.

func (ValidationError) Error

func (v ValidationError) Error() string

Implement error interface.

Jump to

Keyboard shortcuts

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