Categorygithub.com/gogap/go-wkhtmltox
repositorypackage
0.0.0-20191219095322-3135b6e34d94
Repository: https://github.com/gogap/go-wkhtmltox.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author

# README

go-wkhtmltox

Run as a service

Run at local

> go get github.com/gogap/go-wkhtmltox
> cd $GOPATH/src/github.com/gogap/go-wkhtmltox
> go build
> ./go-wkhtmltox run -c app.conf

Run at docker

docker pull idocking/go-wkhtmltox:latest
docker run -it -d -p 8080:8080 idocking/go-wkhtmltox:latest ./go-wkhtmltox run

or

docker-compose up -d

then you could access the 8080 port in osx, you could get the docker ip by command docker-machine ip, and the access service by IP:8080

Config

app.conf

{

	service {
		path = "/v1"
		
		cors {
			allowed-origins = ["*"]
		}

		gzip-enabled = true

		graceful {
			timeout = 10s
		}

		http {
			address = ":8080"
			enabled = true
		}

		https {
			address = ":443"
			enabled = false
			cert    = ""
			key     = ""
		}

		templates  {
			render-html {
				template = "templates/render_html.tmpl"
			}

			binary {
				template = "templates/binary.tmpl"
			}
		}
	}

	wkhtmltox {
		fetchers {
			http {
				driver = http
				options {}
			}

			data {
				driver = data
				options {}
			}
		}
	}
}

API

{
	"to" : "image",
	"fetcher": {
		"name": "http",
		"params": {
		}
	},
	"converter":{
		"uri": "https://www.bing.com"
	},
	"template": "render-data"
}

Request Args

FieldValuesUsage
toimage,pdfconvert to
fetcherif is nil, converter.uri could not be empty, it will pass to wkhtmltox
fetcher.namefetcher name in app.conf
fetcher.paramsdifferent fetcher driver has different options
converterthe options for converter

converter

the converter is the following json struct

{
  "uri":"https://www.bing.com",
   ...
}

ToImageOptions

type ToImageOptions struct {
	URI     string       `json:"uri"`
	Crop    CropOptions  `json:"crop"`    // Cropping options
	Format  string       `json:"format"`  // Image format, default is png
	Quality int          `json:"quality"` // Output image quality (between 0 and 100) (default 94)
	Width   int          `json:"width"`   // Default is 1024
	Height  int          `json:"height"`  // Set screen height (default is calculated from page content) (default 0)
	Extend  ExtendParams `json:"extend"`  // Other params
}

type CropOptions struct {
	X int `json:"x"` // Set x coordinate for cropping
	Y int `json:"y"` // Set y coordinate for cropping
	H int `json:"h"` // Set height for cropping
	W int `json:"w"` // Set width for cropping
}

ToPDFOptions

type ToPDFOptions struct {
	URI            string       `json:"uri"`
	NoCollate      bool         `json:"no_collate"`       // Collate when printing multiple copies, default is true. --collate or --no-collate
	Copies         int          `json:"copies"`           // Number of copies to print into the pdf default is 1
	GrayScale      bool         `json:"gray_scale"`       // PDF will be generated in grayscale
	LowQuality     bool         `json:"low_quality"`      // Generates lower quality pdf/ps. Useful to shrink the result document space
	Orientation    Orientation  `json:"orientation"`      // Set orientation to Landscape or Portrait (default Portrait)
	PageSize       string       `json:"page_size"`        // Set paper size to: A4, Letter, etc. (default A4)
	PrintMediaType bool         `json:"print_media_type"` // Use print media-type instead of screen. --print-media-type or --no-print-media-type
	Extend         ExtendParams `json:"extend"`           // Other params
}

type ExtendParams map[string]string

Use curl

To image

curl -X POST \
  http://IP:8080/v1/convert \
  -H 'accept-encoding: gzip' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
		"to" : "image",
		"converter":{
			"uri": "https://www.bing.com"
	    }
    }'

To pdf

curl -X POST \
  http://IP:8080/v1/convert \
  -H 'accept-encoding: gzip' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
		"to" : "pdf",
		"converter":{
			"uri": "https://www.bing.com"
		}
    }'

if you enabled gzip, you should add arg --compressed to curl

Screenshot

bing.com

Template

The defualt template is

{"code":{{.Code}},"message":"{{.Message}}"{{if .Result}},"result":{{.Result|Jsonify}}{{end}}}

response example:

{"code":0,"message":"","result":{"data":"bGl.............}}

we could add template to render as different response, we have another example template named render-data

{
	"to" : "image",
	"converter":{
		"uri": "https://www.bing.com"
	},
	"template": "render-html"
}

the response is

<html>
	<body>
	     	<img src="data:image/jpeg;base64,bGl............"/> 
 	</body>
</html>

So, the template will render at brower directly. you could add more your templates

Template funcs

Funcusage
base64Encodeencode value to base64 string
base64Decodedecode base64 string to string
jsonifymarshal object
md5string md5 hash
toBytesconvert value to []byte
htmlEscapefor html safe
htmlUnescapeunescape html

Template Args

type TemplateArgs struct {
	To string
	ConvertResponse
	Response *RespHelper
}

type ConvertResponse struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Result  interface{} `json:"result"`
}

Internal templates

at templates dir

NameUsage
default template, retrun code,message, result
render-htmlrender data to html
binaryyou cloud use curl to download directly
use render-html
curl -X POST \
  http://IP:8080/v1/convert \
  -H 'accept-encoding: gzip' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
	"to" : "image",
	"converter":{
		"uri": "https://www.bing.com"
	},
	"template": "render-html"
}' --compressed -o bing.html
use binary
curl -X POST \
  http://IP:8080/v1/convert \
  -H 'accept-encoding: gzip' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
	"to" : "image",
	"converter":{
		"uri": "https://www.bing.com"
	},
	"template": "binary"
}' --compressed -o bing.jpg

Fetcher

fetcher is an external source input, sometimes we could not fetch data by url, or the wkthmltox could not access the url because of some auth options

Data fetcher

the request contain data

curl -X POST \
  http://127.0.0.1:8080/v1/convert \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
        "to" : "image",
        "fetcher" : {
        	"name": "data",
        	"params": {
		"data":"PGh0bWw+CiAgPGJvZHk+CiAgICAgICJIZWxsbyIgV29ybGQKICA8L2JvZHk+CjwvaHRtbD4="
        	}
        },
        "converter":{
        },
        "template": "binary"
}' -o data.jpg
> echo PGh0bWw+CiAgPGJvZHk+CiAgICAgICJIZWxsbyIgV29ybGQKICA8L2JvZHk+CjwvaHRtbD4= | base64 -D


<html>
  <body>
      "Hello" World
  </body>
</html>

params:

{
    "data":"base64string"
}

HTTP fetcher

Fetch data by http driver

curl -X POST \
  http://IP:8080/v1/convert \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
        "to" : "image",
        "fetcher" : {
                "name": "http",
                "params": {
                        "url":"https://github.com"
                }
        },
        "converter":{
        },
        "template": "render-html"
}' -o github.html

params:

{
    "url": "https://github.com",
    "method": "GET",
    "headers": {
        "content-type": "xxx"
    },
    "data": "base64string",
    "replace": {}
}

Code your own fetcher

step 1: Implement the following interface

type Fetcher interface {
	Fetch(FetchParams) ([]byte, error)
}

func NewDataFetcher(conf config.Configuration) (dataFetcher fetcher.Fetcher, err error) {
	dataFetcher = &DataFetcher{}
	return
}

step 2: Reigister your driver

func init() {
	err := fetcher.RegisterFetcher("data", NewDataFetcher)

	if err != nil {
		panic(err)
	}
}

step 3: import driver and rebuild

import (
	_ "github.com/gogap/go-wkhtmltox/wkhtmltox/fetcher/data"
	_ "github.com/gogap/go-wkhtmltox/wkhtmltox/fetcher/http"
)

make sure the register name is unique

Use this package as libary

Just import github.com/gogap/go-wkhtmltox/wkhtmltox

htmlToX, err := wkhtmltox.New(wkHtmlToXConf)
//...
//...
convData, err := htmlToX.Convert(fetcherOpts, convertOpts)