All Projects → gogap → Go Pandoc

gogap / Go Pandoc

Licence: apache-2.0
Run pandoc as a service

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Go Pandoc

Marker
🖊 A gtk3 markdown editor
Stars: ✭ 644 (+2046.67%)
Mutual labels:  pandoc
Phd thesis markdown
Template for writing a PhD thesis in Markdown
Stars: ✭ 898 (+2893.33%)
Mutual labels:  pandoc
Appserver
A multithreaded application server for PHP, written in PHP.
Stars: ✭ 930 (+3000%)
Mutual labels:  service
Vim Pandoc
pandoc integration and utilities for vim
Stars: ✭ 734 (+2346.67%)
Mutual labels:  pandoc
Ultima
An open source, Infrastructure-as-Code cloud platform with built-in CI and local development environment.
Stars: ✭ 18 (-40%)
Mutual labels:  service
Readteractive
Tool for writing and generating interactive books.
Stars: ✭ 23 (-23.33%)
Mutual labels:  pandoc
Polyfill Service
Automatic polyfill service.
Stars: ✭ 5,585 (+18516.67%)
Mutual labels:  service
Servenet
Service Classification based on Service Description
Stars: ✭ 21 (-30%)
Mutual labels:  service
Crisscross
A Markdown-centric template engine for batch offline document generation.
Stars: ✭ 18 (-40%)
Mutual labels:  pandoc
Remit
RabbitMQ-backed microservices supporting RPC, pubsub, automatic service discovery and scaling with no code changes.
Stars: ✭ 24 (-20%)
Mutual labels:  service
Ipfs Deploy
Zero-Config CLI to Deploy Static Websites to IPFS
Stars: ✭ 740 (+2366.67%)
Mutual labels:  service
Electron Crash Report Service
Aggregate crash reports for Electron apps
Stars: ✭ 17 (-43.33%)
Mutual labels:  service
Logmonitor
Monitoring log files on windows systems.
Stars: ✭ 23 (-23.33%)
Mutual labels:  service
Ipban
IPBan Monitors failed logins and bad behavior and bans ip addresses on Windows and Linux. Highly configurable, lean and powerful. Learn more at -->
Stars: ✭ 652 (+2073.33%)
Mutual labels:  service
Mdviewer
Minimalistic Markdown viewer/converter with built-in Css stylesheets support.
Stars: ✭ 26 (-13.33%)
Mutual labels:  pandoc
Cabin
🌲 Cabin is the best JavaScript and Node.js logging service and logging npm package
Stars: ✭ 622 (+1973.33%)
Mutual labels:  service
Easy Pandoc Templates
A collection of portable pandoc templates with no dependencies
Stars: ✭ 23 (-23.33%)
Mutual labels:  pandoc
Ipmonitor
IP Monitor is a simple application which monitors your public IP address for changes and lets you set different kinds of notifications such as email, audio, pop up or executing a command
Stars: ✭ 28 (-6.67%)
Mutual labels:  service
Bingo
基于golang开发的高性能,高并发分布式框架。
Stars: ✭ 9 (-70%)
Mutual labels:  service
Tank
A very high performance distributed log service
Stars: ✭ 927 (+2990%)
Mutual labels:  service

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].