Giter Site home page Giter Site logo

go-pandoc's Introduction

go-pandoc

Run as a service

Run at local

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

Run at docker

docker pull idocking/go-pandoc:latest
docker run -it -d -p 8080:8080 idocking/go-pandoc:latest ./go-pandoc 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"
			}
		}
	}

	pandoc {

		verbose     = false
		trace       = false
		dump-args   = false
		ignore-args = false

        enable-filter = false
		enable-lua-filter = false

		safe-dir = "/app"

		fetchers {
			http {
				driver = http
				options {}
			}

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

q

API

{
    "fetcher": {
        "name": "data",
        "params": {
            "data": "base64String"
        }
    },
    "converter": {
        "from": "markdown",
        "to": "pdf",
        "standalone": true,
        "variable": {
            "CJKmainfont": "Source Han Sans SC",
            "mainfont": "Source Han Sans SC",
            "sansfont": "Source Han Sans SC",
            "geometry:margin": "1cm",
            "subject": "gsjbxx"
        },
        "metadata":{
	    	"A":["A","B","C"],
	    	"D":["Hello", "World"]
	    },
        "template": "/app/data/docs.template"
    },
    "template": "binary"
}

The font Source Han Sans SC could download from https://github.com/adobe-fonts/source-han-sans/releases/tag/2.000R

Request Args

Field Values Usage
fetcher if is nil, converter.uri could not be empty, it will pass to pandoc
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

{
  "from":"markdown",
  "to": "pdf",
  "pdf_engine": "xelatex"
   ...
}

use pandoc --help command to list options

Use curl

curl -X POST \
  http://IP:8080/v1/convert \
  -H 'accept-encoding: gzip' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
	"fetcher": {
		"name": "data",
		"params": {
			"data": "IyMjIEhlbGxvCgo+IEdvLVBhbmRvYw=="
		}
	},
	"converter":{
		"from": "markdown",
	    "to" : "pdf",
	    "standalone": true,
	    "variable":{
	    	"CJKmainfont":"Source Han Sans SC",
	    	"mainfont":"Source Han Sans SC",
	    	"sansfont": "Source Han Sans SC",
	    	"geometry:margin":"1cm",
	    	"subject":"gsjbxx"
	    },
	    "template": "/app/data/docs.template"
	},
	"template": "binary"
}' --compressed -o test.pdf

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

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

{
	"converter":{
		...
	},
	"template": "render-html"
}

the response is

<html>
	<body>
	     	<img src="data:application/pdf;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 {
	From string
	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 '{
	"converter":{
		...
	},
	"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 '{
	"converter":{
		...
	},
	"template": "binary"
}' --compressed -o test.pdf

Fetcher

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

Data fetcher

the request contain data

curl -X POST \
  http://IP:8080/v1/convert \
  -H 'accept-encoding: gzip' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
	"fetcher": {
		"name": "data",
		"params": {
			"data": "IyMjIEhlbGxvCgo+IEdvLVBhbmRvYw=="
		}
	},
	"converter":{
		"from": "markdown",
	    "to" : "pdf",
	    "standalone": true,
	    "variable":{
	    	"CJKmainfont":"Source Han Sans SC",
	    	"mainfont":"Source Han Sans SC",
	    	"sansfont": "Source Han Sans SC",
	    	"geometry:margin":"1cm",
	    	"subject":"gsjbxx"
	    },
	    "template": "/app/data/docs.template"
	},
	"template": "binary"
}' --compressed -o test.pdf
> echo IyMjIEhlbGxvCgo+IEdvLVBhbmRvYw== | base64 -D


### Hello

> Go-Pandoc

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 '{
	    "fetcher": {
	        "name": "http",
	        "params": {
	            "url": "https://raw.githubusercontent.com/golang/go/master/README.md"
	        }
	    },
	    "converter": {
	        "from": "markdown",
	        "to": "pdf",
	        "standalone": true,
	        "template": "/app/data/docs.template",
	        "variable": {
	            "CJKmainfont": "Source Han Sans SC",
	            "mainfont": "Source Han Sans SC",
	            "sansfont": "Source Han Sans SC",
	            "geometry:margin": "1cm",
	            "subject": "gsjbxx"
	        }
	    },
	    "template": "render-html"
}' -o golang-readme.html

if the source contain image urls, it will not display correct, the image resource should be base64 format like:

### Title

- content

#### Examle Image: 
![](data:image/png;base64,iVBORw.......)

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-pandoc/pandoc/fetcher/data"
	_ "github.com/gogap/go-pandoc/pandoc/fetcher/http"
)

make sure the register name is unique

Use this package as libary

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

pdoc, err := pandoc.New(conf)
//...
//...
convData, err := pdoc.Convert(fetcherOpts, convertOpts)

QA

How could I pass the FILE type args

  1. use url http:// or https://
  2. use file://, the file should be in SafeDir
  3. use data:image/jpeg;base64,/9j/4AAQSkZJRgABA

go-pandoc's People

Contributors

rollingdrops 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  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

go-pandoc's Issues

Pictures

How I get a pictures from a url to my PDF?

LaTex

\documentclass{article}
\usepackage{graphicx} % includegraphics command is implemented here
\begin{document}

\write18{wget http://www.example.com/path/to/image.png}
\includegraphics{image.png}

\end{document}

I use MD to PDF with xelatex

please help

Security issues

Hi. I was looking at this library for a project of mine and came across a remote code execution vulnerability. Do you have an email where I can send the issue details? Otherwise I can add the details in here, but the vulnerability info will be public.

Example is not working

Hi,

Below example mentioned is not working with latest pandoc version.

~ » curl -X POST \                                                    ve9u@ve9u
  http://localhost:8080/v1/convert \
  -H 'accept-encoding: gzip' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -d '{
        "fetcher": {
                "name": "data",
                "params": {
                        "data": "IyMjIEhlbGxvCgo+IEdvLVBhbmRvYw=="
                }
        },
        "converter":{
                "from": "markdown",
            "to" : "pdf",
            "standalone": true,
            "variable":{
                "CJKmainfont":"Source Han Sans SC",
                "mainfont":"Source Han Sans SC",
                "sansfont": "Source Han Sans SC",
                "geometry:margin":"1cm",
                "subject":"gsjbxx"
            },
            "template": "/app/data/docs.template"
        },
        "template": "binary"
}' --compressed -o test.pdf
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   542  100    86  100   456   6142  32571 --:--:-- --:--:-- --:--:-- 38714
------------------------------------------------------------
~ » cat test.pdf                                                      ve9u@ve9u
Unknown option -m.
Unknown option -a.
Try pandoc --help for more information.
------------------------------------------------------------
~ » pandoc -v                                                                                                                                                                                            ve9u@ve9u
pandoc 2.5
Compiled with pandoc-types 1.17.5.4, texmath 0.11.1.2, skylighting 0.7.4
Default user data directory: /home/ve9u/.pandoc
Copyright (C) 2006-2018 John MacFarlane
Web:  http://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.
------------------------------------------------------------

Thanks,
Venu.

Not working on windows?

I try to go get the package and i get the errors:

....\github.com\gogap\go-pandoc\pandoc\command.go:17:3: unknown field 'Setpgid' in struct literal of type syscall.SysProcAttr
....\github.com\gogap\go-pandoc\pandoc\command.go:18:3: unknown field 'Pgid' in struct literal of type syscall.SysProcAttr

Is windows unsupported?

build & cli

Error:


 cannot use cli.Command literal (type cli.Command) as type *cli.Command in slice literal
../github.com/gogap/go-pandoc/main.go:37:19: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
        cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
../github.com/gogap/go-pandoc/main.go:42:19: cannot use cli.StringFlag literal (type cli.StringFlag) as type cli.Flag in slice literal:
        cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)

What's my problem?

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.