pdfhandler

package module
v0.0.0-...-b71e47e Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2018 License: MIT Imports: 18 Imported by: 0

README

pdfhandler

Build Status Report Card

Simple http.Handler wrapping pdftk for filling form fields in pdfs.

Features

GET

Lists pdf files in the pdf path and their fields.

[
	{
		"filename": "myfile1.pdf",
		"fields": {"myfield": ""}
	}, 
	{
		"filename": "myfile2.pdf",
		"fields": {"other_field": ""}
	}
]
POST

Accepts either a json body {"filename": "file", "fields": {"fieldName": "field"}} of a single file or a json body list with the same structure. If a list is received and the Accept header is set to application/pdf the server returns a concatenated pdf. If the Accept header is set to application/zip the server returns a zip file containing the filled pdfs.

Installing

go get github.com/StefanKjartansson/pdfhandler
Usage
// main.go
package main

import (
  "os"	
  "net/http"
  "log"
  "github.com/gorilla/mux"
)

func main() {
	pdfFilePath := os.Getenv("PDF_PATH")
	if pdfFilePath == "" {
		log.Fatal("PDF_PATH is required")
	}
	router := mux.NewRouter()
	pdfHandler, err := pdfhandler.New(pdfFilePath)
	if err != nil {
		log.Fatal(err)
	}
	router.Handle("/pdf/", pdfHandler)
	http.Handle("/", router)
  	log.Fatal(http.ListenAndServe(":3001", nil))
}
Usage example

Fill in the fields of myfile1.pdf & myfile2.pdf, return a concatenated pdf.

curl \
    -H "Content-Type: application/json" \
    -H "Accept: application/pdf" \
    -H "X-Filename: myfile" \
    -X POST \
    -d '[{"filename": "myfile1.pdf","fields": {"myfield": "hello"}}, {"filename": "myfile2.pdf","fields": {"other_field": "world"}}]' \
    http://127.0.0.1:3001/pdf/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(w http.ResponseWriter, error string, code int)

func SetLogger

func SetLogger(l StdLogger)

Types

type PDF

type PDF struct {
	FileName string            `json:"filename"`
	Fields   map[string]string `json:"fields"`
	Content  string            `json:"content"`
}

type PDFHandler

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

func New

func New(path string) (*PDFHandler, error)

func (PDFHandler) ServeHTTP

func (p PDFHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

type StdLogger

type StdLogger interface {
	Debugf(string, ...interface{})
	Errorf(string, ...interface{})
}

Jump to

Keyboard shortcuts

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