Documentation
¶
Overview ¶
Package html2pdf provides functionality to convert HTML content to PDF documents using Chrome/Chromium. It manages a pool of Chrome instances to handle concurrent conversions efficiently and safely.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrEmptyPDF = errors.New("PDF generation produced empty output") ErrTimeout = errors.New("conversion timeout exceeded") ErrInitialize = errors.New("failed to initialize converter") ErrConversion = errors.New("conversion failed") ErrCleanup = errors.New("failed to cleanup resources") ErrShutdown = errors.New("converter is shutting down") )
Functions ¶
This section is empty.
Types ¶
type Converter ¶
type Converter struct {
// contains filtered or unexported fields
}
Converter manages a pool of Chrome instances for converting HTML to PDF. It handles concurrent conversions and cleanup of resources.
func New ¶
New creates a new Converter instance with the specified options. It initializes Chrome workers and sets up signal handling for graceful shutdown. Returns an error if initialization fails.
func (*Converter) Close ¶
Close shuts down the Converter and releases all associated resources. It ensures all Chrome instances are properly terminated. Should be called when the Converter is no longer needed.
func (*Converter) Convert ¶
Convert transforms the provided HTML string into a PDF document. It uses a pooled Chrome instance and applies the specified PDF options. The context can be used to cancel the operation or set a deadline. Returns the PDF as a byte slice or an error if the conversion fails.
type Option ¶
type Option func(*Converter)
Option represents a function that modifies Converter configuration.
func WithDebug ¶
WithDebug enables or disables debug logging. The debug flag must be a boolean value, otherwise it will be false.
func WithRetryAttempts ¶
WithRetryAttempts sets the number of retry attempts for failed conversions. The number must be positive, otherwise it will be ignored. If n <= 0, the default number of attempts (3) is used.
func WithRetryDelay ¶
WithRetryDelay sets the delay between retry attempts in seconds. The duration must be positive, otherwise it will be ignored. If d <= 0, the default delay (1s) is used.
func WithTimeout ¶
WithTimeout sets the maximum duration for a single conversion operation. The duration must be positive, otherwise it will be ignored. If d <= 0, the default timeout (60s) is used.
func WithWorkers ¶
WithWorkers sets the number of concurrent Chrome workers. The number must be positive, otherwise it will be ignored. If n <= 0, the default number of workers (1) is used.
type PDFOption ¶
type PDFOption func(*pdfConfig)
PDFOption represents a function that modifies PDF generation settings.
func WithMargins ¶
WithMargins sets the PDF margins in inches. The top, right, bottom, and left values must be positive, otherwise they will be ignored. If any value is <= 0, the default margins (0.5x0.5) are used.
func WithPaperSize ¶
WithPaperSize sets the PDF paper size in inches. The width and height must be positive, otherwise they will be ignored. If width or height <= 0, the default size (8.5x11) is used.
func WithWaitForAnimations ¶
WithWaitForAnimations sets how long to wait for animations to complete in milliseconds. The duration must be positive, otherwise it will be ignored. If d <= 0, the default duration (500ms) is used.
func WithWaitForLoad ¶
WithWaitForLoad sets whether to wait for the page to load before generating the PDF. The wait flag must be a boolean value, otherwise it will be false.
func WithWaitTimeout ¶
WithWaitTimeout sets the maximum duration to wait for page load and animations in seconds. The duration must be positive, otherwise it will be ignored. If d <= 0, the default timeout (60s) is used.