go-wkhtmltopdf: github.com/SebastiaanKlippert/go-wkhtmltopdf Index | Examples | Files

package wkhtmltopdf

import "github.com/SebastiaanKlippert/go-wkhtmltopdf"

Package wkhtmltopdf contains wrappers around the wkhtmltopdf commandline tool



Package Files

json.go options.go wkhtmltopdf.go


const (
    OrientationLandscape = "Landscape" // Landscape mode
    OrientationPortrait  = "Portrait"  // Portrait mode

Constants for orientation modes

const (
    PageSizeA0        = "A0"        //	841 x 1189 mm
    PageSizeA1        = "A1"        //	594 x 841 mm
    PageSizeA2        = "A2"        //	420 x 594 mm
    PageSizeA3        = "A3"        //	297 x 420 mm
    PageSizeA4        = "A4"        //	210 x 297 mm, 8.26
    PageSizeA5        = "A5"        //	148 x 210 mm
    PageSizeA6        = "A6"        //	105 x 148 mm
    PageSizeA7        = "A7"        //	74 x 105 mm
    PageSizeA8        = "A8"        //	52 x 74 mm
    PageSizeA9        = "A9"        //	37 x 52 mm
    PageSizeB0        = "B0"        //	1000 x 1414 mm
    PageSizeB1        = "B1"        //	707 x 1000 mm
    PageSizeB10       = "B10"       //	31 x 44 mm
    PageSizeB2        = "B2"        //	500 x 707 mm
    PageSizeB3        = "B3"        //	353 x 500 mm
    PageSizeB4        = "B4"        //	250 x 353 mm
    PageSizeB5        = "B5"        //	176 x 250 mm, 6.93
    PageSizeB6        = "B6"        //	125 x 176 mm
    PageSizeB7        = "B7"        //	88 x 125 mm
    PageSizeB8        = "B8"        //	62 x 88 mm
    PageSizeB9        = "B9"        //	33 x 62 mm
    PageSizeC5E       = "C5E"       //	163 x 229 mm
    PageSizeComm10E   = "Comm10E"   //	105 x 241 mm, U.S. Common 10 Envelope
    PageSizeCustom    = "Custom"    //	Unknown, or a user defined size.
    PageSizeDLE       = "DLE"       //	110 x 220 mm
    PageSizeExecutive = "Executive" //	7.5 x 10 inches, 190.5 x 254 mm
    PageSizeFolio     = "Folio"     //	210 x 330 mm
    PageSizeLedger    = "Ledger"    //	431.8 x 279.4 mm
    PageSizeLegal     = "Legal"     //	8.5 x 14 inches, 215.9 x 355.6 mm
    PageSizeLetter    = "Letter"    //	8.5 x 11 inches, 215.9 x 279.4 mm
    PageSizeTabloid   = "Tabloid"   //	279.4 x 431.8 mm

Constants for page sizes

func GetPath Uses

func GetPath() string

GetPath gets the path to wkhtmltopdf

func SetPath Uses

func SetPath(path string)

SetPath sets the path to wkhtmltopdf

type PDFGenerator Uses

type PDFGenerator struct {
    Cover      cover
    TOC        toc
    OutputFile string //filename to write to, default empty (writes to internal buffer)
    // contains filtered or unexported fields

PDFGenerator is the main wkhtmltopdf struct, always use NewPDFGenerator to obtain a new PDFGenerator struct

func NewPDFGenerator Uses

func NewPDFGenerator() (*PDFGenerator, error)

NewPDFGenerator returns a new PDFGenerator struct with all options created and checks if wkhtmltopdf can be found on the system


// Create new PDF generator
pdfg, err := NewPDFGenerator()
if err != nil {

// Set global options

// Create a new input page from an URL
page := NewPage("https://godoc.org/github.com/SebastiaanKlippert/go-wkhtmltopdf")

// Set options for this page

// Add to document

// Create PDF document in internal buffer
err = pdfg.Create()
if err != nil {

// Write buffer contents to file on disk
err = pdfg.WriteFile("./simplesample.pdf")
if err != nil {




func NewPDFGeneratorFromJSON Uses

func NewPDFGeneratorFromJSON(jsonReader io.Reader) (*PDFGenerator, error)

NewPDFGeneratorFromJSON creates a new PDFGenerator and restores all the settings and pages from a JSON byte slice which should be created using PDFGenerator.ToJSON().


const html = `<!doctype html><html><head><title>WKHTMLTOPDF TEST</title></head><body>HELLO PDF</body></html>`

// Client code
pdfg := NewPDFPreparer()

// The html string is also saved as base64 string in the JSON file
jsonBytes, err := pdfg.ToJSON()
if err != nil {

// The JSON can be saved, uploaded, etc.

// Server code, create a new PDF generator from JSON, also looks for the wkhtmltopdf executable
pdfgFromJSON, err := NewPDFGeneratorFromJSON(bytes.NewReader(jsonBytes))
if err != nil {

// Create the PDF
err = pdfgFromJSON.Create()
if err != nil {

// Use the PDF
fmt.Printf("PDF size %d bytes", pdfgFromJSON.Buffer().Len())

func NewPDFPreparer Uses

func NewPDFPreparer() *PDFGenerator

NewPDFPreparer returns a PDFGenerator object without looking for the wkhtmltopdf executable file. This is useful to prepare a PDF file that is generated elsewhere and you just want to save the config as JSON. Note that Create() can not be called on this object unless you call SetPath yourself.

func (*PDFGenerator) AddPage Uses

func (pdfg *PDFGenerator) AddPage(p page)

AddPage adds a new input page to the document. A page is an input HTML page, it can span multiple pages in the output document. It is a Page when read from file or URL or a PageReader when read from memory.

func (*PDFGenerator) ArgString Uses

func (pdfg *PDFGenerator) ArgString() string

ArgString returns Args as a single string

func (*PDFGenerator) Args Uses

func (pdfg *PDFGenerator) Args() []string

Args returns the commandline arguments as a string slice

func (*PDFGenerator) Buffer Uses

func (pdfg *PDFGenerator) Buffer() *bytes.Buffer

Buffer returns the embedded output buffer used if OutputFile is empty

func (*PDFGenerator) Bytes Uses

func (pdfg *PDFGenerator) Bytes() []byte

Bytes returns the output byte slice from the output buffer used if OutputFile is empty

func (*PDFGenerator) Create Uses

func (pdfg *PDFGenerator) Create() error

Create creates the PDF document and stores it in the internal buffer if no error is returned

func (*PDFGenerator) ResetPages Uses

func (pdfg *PDFGenerator) ResetPages()

ResetPages drops all pages previously added by AddPage or SetPages. This allows reuse of current instance of PDFGenerator with all of it's configuration preserved.

func (*PDFGenerator) SetOutput Uses

func (pdfg *PDFGenerator) SetOutput(w io.Writer)

SetOutput sets the output to write the PDF to, when this method is called, the internal buffer will not be used, so the Bytes(), Buffer() and WriteFile() methods will not work.

func (*PDFGenerator) SetPages Uses

func (pdfg *PDFGenerator) SetPages(p []page)

SetPages resets all pages

func (*PDFGenerator) SetStderr Uses

func (pdfg *PDFGenerator) SetStderr(w io.Writer)

SetStderr sets the output writer for Stderr when running the wkhtmltopdf command. You only need to call this when you want to print the output of wkhtmltopdf (like the progress messages in verbose mode). If not called, or if w is nil, the output of Stderr is kept in an internal buffer and returned as error message if there was an error when calling wkhtmltopdf.

func (*PDFGenerator) ToJSON Uses

func (pdfg *PDFGenerator) ToJSON() ([]byte, error)

ToJSON creates JSON of the complete representation of the PDFGenerator. It also saves all pages, for a PageReader page, the content is stored as a Base64 string in the JSON.

func (*PDFGenerator) WriteFile Uses

func (pdfg *PDFGenerator) WriteFile(filename string) error

WriteFile writes the contents of the output buffer to a file

type Page Uses

type Page struct {
    Input string

Page is the input struct for each page

func NewPage Uses

func NewPage(input string) *Page

NewPage creates a new input page from a local or web resource (filepath or URL)

func (*Page) Args Uses

func (p *Page) Args() []string

Args returns the argument slice and is part of the page interface

func (*Page) InputFile Uses

func (p *Page) InputFile() string

InputFile returns the input string and is part of the page interface

func (*Page) Reader Uses

func (p *Page) Reader() io.Reader

Reader returns the io.Reader and is part of the page interface

type PageOptions Uses

type PageOptions struct {
    // contains filtered or unexported fields

PageOptions are options for each input page

func NewPageOptions Uses

func NewPageOptions() PageOptions

NewPageOptions returns a new PageOptions struct with all options

func (*PageOptions) Args Uses

func (po *PageOptions) Args() []string

Args returns the argument slice

type PageReader Uses

type PageReader struct {
    Input io.Reader

PageReader is one input page (a HTML document) that is read from an io.Reader You can add only one Page from a reader

func NewPageReader Uses

func NewPageReader(input io.Reader) *PageReader

NewPageReader creates a new PageReader from an io.Reader

func (*PageReader) Args Uses

func (pr *PageReader) Args() []string

Args returns the argument slice and is part of the page interface

func (*PageReader) InputFile Uses

func (pr *PageReader) InputFile() string

InputFile returns the input string and is part of the page interface

func (*PageReader) Reader Uses

func (pr *PageReader) Reader() io.Reader

Reader returns the io.Reader and is part of the page interface

Package wkhtmltopdf imports 13 packages (graph) and is imported by 6 packages. Updated 2019-09-03. Refresh now. Tools for package owners.