html2pdf

package module
v0.0.0-...-510c34c Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2019 License: MIT Imports: 8 Imported by: 0

README

html2pdf

html2pdf use wkhtmltopdf to generate PDF. It wrapper of the wkhtmltopdf using fasthttp template for html render.

This repo still under development. We accept any pull request. ^_^

Installation

  // dependency
  $ go get github.com/magicwebes/html2pdf
  $ go get github.com/valyala/fasttemplate

  // Downloading wkhtmltopdf from this website and install to the computer or server
  https://wkhtmltopdf.org/downloads.html

Quick Start

Convert html file to pdf and download
  filepath := "public/views/index.html"
  data := map[string]interface{}{
    "message": "hello",
  }
  outputPath := "public/views/index.pdf"

  if err := html2pdf.ParseHTML(filepath, data).ToFile(outputPath); err != nil {
      log.Println("error", err)
  }

Advance Usage

  filepath := "public/views/data.html"
  data := map[string]interface{}{
    "message": "hello", // pass data to html file
  }
  outputPath := "public/views/data.pdf"

  if err := html2pdf.ParseHTML(filepath, data).
    SetConfig(&html2pdf.Config{
        Orientation: html2pdf.OrientationLandscape,
        PageSize: html2pdf.PageSizeA4,
    }).
    ToFile(outputPath); err != nil {
      log.Println("error", err)
  }

Documentation

Index

Constants

View Source
const (
	OrientationLandscape = "Landscape" // Landscape mode
	OrientationPortrait  = "Portrait"  // Portrait mode
)

Constants for orientation modes

View Source
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
	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
	PageSizeB10       = "B10"       //	31 x 44 mm
	PageSizeC5E       = "C5E"       //	163 x 229 mm
	PageSizeComm10E   = "Comm10E"   //	105 x 241 mm, U.S. Common 10 Envelope
	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

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Title       string
	Orientation string
	PageSize    string
	Grayscale   bool
}

Config :

type PDF

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

PDF :

func ParseByte

func ParseByte(html []byte, data map[string]interface{}) *PDF

ParseByte :

func ParseHTML

func ParseHTML(path string, data map[string]interface{}) *PDF

ParseHTML :

func (*PDF) Grayscale

func (p *PDF) Grayscale() *PDF

Grayscale :

func (*PDF) SetConfig

func (p *PDF) SetConfig(c *Config) *PDF

SetConfig :

func (*PDF) SetMargin

func (p *PDF) SetMargin(top, right, bottom, left uint) *PDF

SetMargin :

func (*PDF) SetOrientation

func (p *PDF) SetOrientation(orientation string) *PDF

SetOrientation :

func (*PDF) SetPageSize

func (p *PDF) SetPageSize(size string) *PDF

SetPageSize :

func (*PDF) SetTitle

func (p *PDF) SetTitle(title string) *PDF

SetTitle :

func (*PDF) ToByte

func (p *PDF) ToByte() ([]byte, error)

ToByte :

func (*PDF) ToFile

func (p *PDF) ToFile(outputPath string) error

ToFile :

Directories

Path Synopsis
Package wkhtmltopdf contains wrappers around the wkhtmltopdf commandline tool
Package wkhtmltopdf contains wrappers around the wkhtmltopdf commandline tool

Jump to

Keyboard shortcuts

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