// pdfhandler makes a pdf file out of the information it is passed. func pdfhandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s %s\n", r.Method, r.URL.Path) fmt.Printf("%s\n", r.Header.Get("Accept")) header := w.Header() id := assignId(r) doc, err := DB.Fetch(id) if err != nil { web.Error(w, err.Error(), http.StatusNotFound) return } header.Set("Content-Type", "application/pdf") //header.Set("Content-Disposition", "attachment;filename=foo.pdf") pdf := textproc.MakePDFStreamTextObject(w, 8.5*72, 11*72) defer pdf.Close() props := textproc.TypesettingProps{} props.Fontname = doc.Font props.Fontsize = doc.FontSize.Points() props.Baselineskip = doc.BaselineSkip.Points() props.PageWidth = doc.PageWidth.Points() props.PageHeight = doc.PageHeight.Points() props.LeftMargin = doc.LeftMargin.Points() props.RightMargin = doc.RightMargin.Points() pdf.WriteAt(doc.Text, props, props.LeftMargin, doc.TopMargin.Points()+props.Fontsize) }
func writeDoc(w http.ResponseWriter, doc *document.Document) { w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(doc) if err != nil { web.Error(w, err.Error(), http.StatusNotFound) return } }
func deleteDocHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s %s\n", r.Method, r.URL.Path) id := assignId(r) err := DB.Delete(id) if err != nil { web.Error(w, err.Error(), http.StatusNotFound) return } }
// editHandler is the handler for showing document edit pages func editHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s %s\n", r.Method, r.URL.Path) fmt.Printf("%s\n", r.Header.Get("Accept")) templatePath := path.Join(TemplateDir, "main.html") contentPath := path.Join(TemplateDir, "content.html") templ, err := template.ParseFiles(templatePath, contentPath) header := w.Header() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } fonts := textproc.ListFontFamilies() sort.Strings(fonts) defaultDoc := document.DefaultDocument() defaultDocJSON, err := json.Marshal(defaultDoc) if err != nil { panic(err) } fontsJSON, err := json.Marshal(fonts) if err != nil { panic(err) } id := assignId(r) doc, err := DB.Fetch(id) if !id.IsNull() && err != nil { web.Error(w, err.Error(), http.StatusNotFound) return } docJSON, err := json.Marshal(doc) if err != nil { panic(err) } lengthREString := document.LengthREString() data := map[string]interface{}{"fonts": template.JS(fontsJSON), "doc": template.JS(docJSON), "defaultDoc": template.JS(defaultDocJSON), "lengthRE": lengthREString} header.Set("Content-Type", "text/html") err = templ.Execute(w, data) if err != nil { web.Error(w, err.Error(), http.StatusInternalServerError) return } }
func getDocHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s %s\n", r.Method, r.URL.Path) id := assignId(r) doc, err := DB.Fetch(id) if err != nil { web.Error(w, err.Error(), http.StatusNotFound) return } writeDoc(w, &doc) }
func postDocHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s %s\n", r.Method, r.URL.Path) doc := document.Document{} json.NewDecoder(r.Body).Decode(&doc) err := DB.Add(&doc) if err != nil { // Try to figure out what the error was web.Error(w, err.Error(), http.StatusInternalServerError) return } writeDoc(w, &doc) }