Created
May 30, 2015 16:13
-
-
Save mettledrum/9d28264c28dd44cee2f1 to your computer and use it in GitHub Desktop.
wkhtmltopdf usage for PDF rendering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>{{.Stuff}} is really neato!</h2> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"os" | |
"text/template" | |
"github.com/gorilla/context" | |
"github.com/gorilla/mux" | |
pdf "github.com/terryh/gopdf" | |
) | |
func main() { | |
r := mux.NewRouter() | |
r.HandleFunc("/coolstuff", GetCoolStuff).Methods("GET") | |
http.Handle("/", r) | |
log.Println("listening on 0.0.0.0:8080") | |
err := http.ListenAndServe(":8080", context.ClearHandler(http.DefaultServeMux)) | |
log.Println(err) | |
} | |
type Thingy struct { | |
Stuff string | |
} | |
func GetCoolStuff(w http.ResponseWriter, r *http.Request) { | |
// get models | |
th := &Thingy{ | |
Stuff: "pandabear", | |
} | |
// open file handle | |
f, err := os.Create("/tmp/panda.html") // + something unique for user | |
if err != nil { | |
log.Fatal("error making tmp file: ", err) | |
} | |
defer f.Close() | |
// write template to file | |
t, err := template.ParseFiles("agreement.html") | |
if err != nil { | |
log.Fatal("error making template: ", err) | |
} | |
t.Execute(f, th) | |
// return PDF | |
p, err := pdf.Url2pdf("/tmp/panda.html") | |
w.Header().Set("Content-Type", "application/pdf") | |
w.Write(p) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment