Created
January 27, 2017 03:21
-
-
Save novalagung/eff4f9ad46a11abd59dbad653270fe7c to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>test pdf iframe</title> | |
<style> | |
html, body { | |
height: 100%; | |
width: 100%; | |
} | |
iframe { | |
width: 600px; | |
height: 100%; | |
margin: 0px auto; | |
display: block; | |
} | |
</style> | |
</head> | |
<body> | |
<iframe src="/read-pdf" frameborder="0"></iframe> | |
</body> | |
</html> |
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 "net/http" | |
import "log" | |
import "bytes" | |
import "io/ioutil" | |
import "html/template" | |
func main() { | |
http.HandleFunc("/read-pdf", func(w http.ResponseWriter, r *http.Request) { | |
bts, err := ioutil.ReadFile("./dashboards.pdf") | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
return | |
} | |
w.Header().Set("Content-type", "application/pdf") | |
buf := bytes.NewBuffer(bts) | |
_, err = buf.WriteTo(w) | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
} | |
w.Write([]byte("PDF Generated")) | |
}) | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
tpl := template.New("view") | |
tpl, err := template.ParseFiles("./index.html") | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
} | |
err = tpl.Execute(w, "") | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
} | |
}) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment