Giter VIP home page Giter VIP logo

go-wkhtmltox's Introduction

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

Field Values Usage
to image,pdf convert to
fetcher if is nil, converter.uri could not be empty, it will pass to wkhtmltox
fetcher.name fetcher name in app.conf
fetcher.params different fetcher driver has different options
converter the 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

Func usage
base64Encode encode value to base64 string
base64Decode decode base64 string to string
jsonify marshal object
md5 string md5 hash
toBytes convert value to []byte
htmlEscape for html safe
htmlUnescape unescape 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

Name Usage
default template, retrun code,message, result
render-html render data to html
binary you 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)

go-wkhtmltox's People

Contributors

sysadmind avatar xujinzheng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-wkhtmltox's Issues

How to escape space in GlobalOptions

Hi,
I use https://github.com/idocking/go-wkhtmltox docker container with wkhtmltopdf 0.12.5 (with patched qt).
I try to set GlobalOptions like this:

"converter": {
		"url": null,
		"extend": {
			"footer-left": "[date]",
			"footer-right": "Prepared [date] [page]/[topage]",
			"footer-font-size": "10",
			"margin-bottom": "20",
			"footer-spacing": "10"
		}
	},

I use space like in footer-right line I get error when convert to pdf:

open /tmp/go-wkhtmltox212187195/41f80aa7-be68-4437-9d43-27bb8f520780.pdf: no such file or directory

When I delete space in string line this "Prepared[date][page]/[topage]" everything work fine.

Any ideas how to escape space?

CORS/Restrict external access

Hello again,

Is it possible it implement any access control to the service? Such as CORS?
For example only let requests from http://www.example.com execute conversions?

Access-Control-Allow-Origin: http://www.example.com

Bit of a Docker beginner...

How to access wkhtmltopdf Global Options?

Is this possible through the API? If so an example would be much appreciated :)

Essentially I would like to be able to pass the --javascript-delay option for fetching slower or javascript-based sites.

Cheers

Quality not appended in ToImageOptions

I'm new to Go so I'm gessing, but it seems quality is not added to args in ToImageOptions.

The fact is that calling the API as README suggest doesn't work as espected:

{
    "to" : "image",
    "converter":{
      "format" : "png",
      "quality": "0",
      "uri": "https://sbit.io"
      
    },
  "template": "binary"
  }

But calling the API with extend works:

{
    "to" : "image",
    "converter":{
      "format" : "png",
      "extend": {"quality": "0" },
      "uri": "https://sbit.io"
      
    },
  "template": "binary"
  }

Please note that 0 is a must param for PNG images as indicated in wkhtmltopdf/wkhtmltopdf#2608

/cc @jonhattan @facine

Issue in converting html to pdf

I am using go-wkhtmltox package as a library instead of a service. I currently see issues when I try to convert html to pdf (html to image is working fine).

Sample Code

htmlToX, err := wkhtmltox.New(conf)
fetcherOpts := wkhtmltox.FetcherOptions{}

var convertOpts wkhtmltox.ConvertOptions
convertOpts = &wkhtmltox.ToPDFOptions{URI: "https://google.com"}

convData, err := htmlToX.Convert(fetcherOpts, convertOpts)

Error Details

open /var/folders/yb/xxxx/T/go-wkhtmltox539727929/79b4072e-e3ee-49a0-997d-e86ce49ab687.pdf: no such file or directory. The error points to this line in the codebase.

Any idea on why the error is thrown? or am I missing something?

More verbose output?

Any way to get more verbose output? Would be nice to see the standard out logged somewhere?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.