PDF-Ausgabe hinzugefügt

This commit is contained in:
Sven Schmalle 2019-12-13 17:19:13 +01:00
parent f2308d25d6
commit e9a1b087b8
3 changed files with 41 additions and 4 deletions

View File

@ -24,6 +24,7 @@ Eine `config.json` könnte z.B. wie folgt aussehen:
Zum kompilieren folgende Go-Abhängigkeiten installieren:
- `go get github.com/gorilla/mux`
- `go get gopkg.in/src-d/go-git.v4`
- 'go get github.com/mandolyte/mdtopdf'
Die Build-Scripte liegen im Unterordner `build`

33
main.go
View File

@ -15,6 +15,7 @@ import (
"time"
"github.com/gorilla/mux"
"github.com/mandolyte/mdtopdf"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
@ -64,7 +65,8 @@ func main() {
}
router := mux.NewRouter()
router.HandleFunc("/p/{pagename:.*}", getRawPage).Methods("GET")
router.HandleFunc("/_api/md/{pagename:.*}", getRawPage).Methods("GET")
router.HandleFunc("/_api/pdf/{pagename:.*}", getPDFPage).Methods("GET")
router.HandleFunc("/{pagename:.*}", getHTMLPage).Methods("GET")
router.HandleFunc("/{pagename:.*}", postHTMLPage).Methods("POST")
@ -72,6 +74,35 @@ func main() {
}
func getPDFPage(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
pPageName := params["pagename"] + ".md"
// Hack für anzeige der start.md wenn / aufgerufen wird
if pPageName == ".md" {
pPageName = "start.md"
}
content, err := ioutil.ReadFile(path.Join(config.DataPath, pPageName))
check(err)
pdfRender := mdtopdf.NewPdfRenderer("", "", "./tmp.pdf", "")
// get the unicode translator
tr := pdfRender.Pdf.UnicodeTranslatorFromDescriptor("cp1252")
pdfRender.Normal = mdtopdf.Styler{Font: "Arial", Style: "",
Size: 8, Spacing: 1,
TextColor: mdtopdf.Color{0, 0, 0},
FillColor: mdtopdf.Color{255, 255, 255}}
err = pdfRender.Process([]byte(tr(string(content))))
check(err)
pdfcontent, err := ioutil.ReadFile("./tmp.pdf")
check(err)
w.Header().Set("Content-Type", "application/pdf")
w.Write(pdfcontent)
}
func getHTMLPage(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)

View File

@ -118,6 +118,7 @@
<li id="btnEditpage"><a href="#" onclick="EditPage()">Edit</a></li>
<li id="btnPreviewpage"><a href="#" onclick="PreviewPage()">Preview</a></li>
<li id="btnSavepage"><a href="#" onclick="SavePage()">Save</a></li>
<li id="btnPDFgen"><a href="#" onclick="PDFGen()">PDF</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
@ -198,7 +199,7 @@
$.ajax({
method: "GET",
contentType:'application/json; charset=utf-8',
url: '/p/'+window.location.pathname,
url: '/_api/md/'+window.location.pathname,
dataType: "json",
data: "",
success: function(content){
@ -213,7 +214,7 @@
$.ajax({
method: "GET",
contentType:'application/json; charset=utf-8',
url: '/p/sidebar',
url: '/_api/md/sidebar',
dataType: "json",
data: "",
success: function(content){
@ -235,7 +236,7 @@
$.ajax({
method: "GET",
contentType:'application/json; charset=utf-8',
url: '/p/'+window.location.pathname,
url: '/_api/md/'+window.location.pathname,
dataType: "json",
data: "",
success: function(content){
@ -279,6 +280,10 @@
});
}
function PDFGen() {
// Öffnet die PDF über die API einfach in einem neuen Fenster
window.open('/_api/pdf'+window.location.pathname);
}
</script>
<script>