Beispiel #1
0
// 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)
}
Beispiel #2
0
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
	}
}
Beispiel #3
0
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
	}
}
Beispiel #4
0
// 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
	}
}
Beispiel #5
0
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)
}
Beispiel #6
0
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)
}