pdfire

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2019 License: MIT Imports: 22 Imported by: 0

README

PDFire - HTML to PDF converter

Convert your HTML pages to beautiful PDF files. Supports the latest HTML, CSS and Javascript features.

This tool creates PDF files through the Chrome DevTools Protocol using the chromedp & pdfcpu package.

Installation

go get github.com/modernice/pdfire

Usage

Pre-configured server
package main

import (
    "net/http"

    "github.com/modernice/pdfire/server"
)

func main() {
    if err := http.ListenAndServe("localhost:3000", server.New()); err != nil {
      panic(err)
    }
}
curl -X POST localhost:3000/conversions \n
    -H "Content-Type: application/json" \n
    -d '{"url": "https://google.com"}'
Manual use
package main

import (
    "bytes"
    "strings"
    "github.com/modernice/pdfire"
)

func main() {
    pdf := bytes.NewBuffer(make([]byte, 0))
    options := pdfire.NewOptions()

    // Create PDF from an URL
    err := pdfire.URLToPDF("https://google.com", pdf, options)

    // Create PDF from an HTML string
    err := pdfire.HTMLToPDF(strings.NewReader("<p>Example paragraph</p>"), pdf, options)

    // Create PDF from an HTML file
    err := pdfire.FileToPDF("/path/to/file", pdf, options)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidJSON is a JSON syntax error.
	ErrInvalidJSON = errors.New("the json request is malformed")
	// ErrNoSource states that the options neither have an HTML string nor a URL.
	ErrNoSource = errors.New("no url or html provided")
)
View Source
var (
	// MediaScreen is the CSS "screen" media.
	MediaScreen = Media("screen")
	// MediaPrint is the CSS "print" media.
	MediaPrint = Media("print")
)
View Source
var (
	// ErrTimeout is returned when the conversion times out.
	ErrTimeout = errors.New("conversion timed out")
	// ErrWaitUntilTimeout is returned when the Chrome DevTools times out while waiting for the "load" or "DOMContentLoaded" event.
	ErrWaitUntilTimeout = errors.New("WaitUntil timed out")
	// ErrNoBody is returned when the page has no 'body' element.
	ErrNoBody = errors.New("page has no 'body' element")
)
View Source
var PaperFormats = map[string]struct {
	Width  float64
	Height float64
}{
	"letter": {
		Width:  8.5,
		Height: 11,
	},
	"legal": {
		Width:  8.5,
		Height: 14,
	},
	"tabloid": {
		Width:  11,
		Height: 17,
	},
	"ledger": {
		Width:  17,
		Height: 11,
	},
	"a0": {
		Width:  33.1,
		Height: 46.8,
	},
	"a1": {
		Width:  23.4,
		Height: 33.1,
	},
	"a2": {
		Width:  16.54,
		Height: 23.4,
	},
	"a3": {
		Width:  11.7,
		Height: 16.54,
	},
	"a4": {
		Width:  8.27,
		Height: 11.7,
	},
	"a5": {
		Width:  5.83,
		Height: 8.27,
	},
	"a6": {
		Width:  4.13,
		Height: 5.83,
	},
}

PaperFormats are the available paper formats.

View Source
var UnitToPixels = map[string]float64{
	"px": 1,
	"in": 96,
	"cm": 37.8,
	"mm": 3.78,
}

UnitToPixels contains the to-pixel-ratios for the different units.

Functions

func Convert

func Convert(ctx context.Context, w io.Writer, options *ConversionOptions) error

Convert creates a PDF from the given options.

func ConvertHTML

func ConvertHTML(ctx context.Context, w io.Writer, options *ConversionOptions) error

ConvertHTML creates a PDF from an HTML string.

func ConvertURL

func ConvertURL(ctx context.Context, w io.Writer, options *ConversionOptions) error

ConvertURL creates a PDF from a URL.

func Merge

func Merge(ctx context.Context, w io.Writer, options *MergeOptions) error

Merge creates multiple PDFs and merges them together into a single file.

Types

type ConversionOptions

type ConversionOptions struct {
	HTML                   string
	URL                    string
	PDFParams              *page.PrintToPDFParams `json:"pdfParams"`
	ViewportWidth          int64
	ViewportHeight         int64
	BlockAds               bool
	Selector               string
	WaitForSelector        string
	WaitForSelectorTimeout time.Duration
	WaitUntil              string
	WaitUntilTimeout       time.Duration
	Delay                  time.Duration
	Timeout                time.Duration
	Headers                map[string]interface{}
	EmulateMedia           Media
	OwnerPassword          string
	UserPassword           string
	Watermark              *WatermarkConfig
}

ConversionOptions are the conversion options.

func NewConversionOptions

func NewConversionOptions() *ConversionOptions

NewConversionOptions returns new converter options with default values.

func NewConversionOptionsFromJSON

func NewConversionOptionsFromJSON(r io.Reader) (*ConversionOptions, error)

NewConversionOptionsFromJSON returns new converter options from JSON.

func NewConversionOptionsFromJSONString

func NewConversionOptionsFromJSONString(json string) (*ConversionOptions, error)

NewConversionOptionsFromJSONString returns new converter options from JSON.

type Media

type Media string

Media is a CSS media.

type MergeOptions

type MergeOptions struct {
	Documents     []*ConversionOptions
	OwnerPassword string
	UserPassword  string
	Watermark     *WatermarkConfig
}

MergeOptions are the merge options.

func NewMergeOptions

func NewMergeOptions() *MergeOptions

NewMergeOptions returns new merge options.

func NewMergeOptionsFromJSON

func NewMergeOptionsFromJSON(r io.Reader) (*MergeOptions, error)

NewMergeOptionsFromJSON returns new merge options from JSON.

func NewMergeOptionsFromJSONString

func NewMergeOptionsFromJSONString(json string) (*MergeOptions, error)

NewMergeOptionsFromJSONString returns new merge options from JSON.

type ParseError

type ParseError struct {
	Key   string
	Value interface{}
}

ParseError is returned when a PDF parameter cannot be parsed from a request body.

func (*ParseError) Error

func (e *ParseError) Error() string

type WatermarkConfig added in v1.1.0

type WatermarkConfig struct {
	Query string
	OnTop bool
	Pages []string
}

WatermarkConfig is the pdfcpu watermark configuration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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