Création d'une page tableau en lecture seule et possibilité d'exporter en PDF via wkhtmltopdf
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
||||
"gitlab.com/wpetit/goweb/static"
|
||||
)
|
||||
|
||||
// Mount endoints for server app
|
||||
func Mount(r *chi.Mux, config *config.Config) error {
|
||||
r.Route("/api/v1", func(r chi.Router) {
|
||||
r.Get("/projects/{projectID}", handleGetProject)
|
||||
@ -17,6 +18,10 @@ func Mount(r *chi.Mux, config *config.Config) error {
|
||||
r.Delete("/projects/{projectID}", handleDeleteProject)
|
||||
})
|
||||
|
||||
r.Route("/export", func(r chi.Router) {
|
||||
r.Get("/projects/{projectID}", handleExportProject)
|
||||
})
|
||||
|
||||
clientIndex := path.Join(config.HTTP.PublicDir, "index.html")
|
||||
|
||||
serveClientIndex := func(w http.ResponseWriter, r *http.Request) {
|
||||
@ -24,6 +29,7 @@ func Mount(r *chi.Mux, config *config.Config) error {
|
||||
}
|
||||
|
||||
r.Get("/p/*", serveClientIndex)
|
||||
r.Get("/pdf/*", serveClientIndex)
|
||||
|
||||
notFoundHandler := r.NotFoundHandler()
|
||||
r.Get("/*", static.Dir(config.HTTP.PublicDir, "", notFoundHandler))
|
||||
|
@ -2,14 +2,17 @@ package route
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
jsonpatch "gopkg.in/evanphx/json-patch.v4"
|
||||
|
||||
"forge.cadoles.com/wpetit/guesstimate/internal/config"
|
||||
"forge.cadoles.com/wpetit/guesstimate/internal/model"
|
||||
"forge.cadoles.com/wpetit/guesstimate/internal/storm"
|
||||
"github.com/SebastiaanKlippert/go-wkhtmltopdf"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/middleware/container"
|
||||
@ -74,6 +77,62 @@ func handleGetProject(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func handleExportProject(w http.ResponseWriter, r *http.Request) {
|
||||
ctn := container.Must(r.Context())
|
||||
cfg := config.Must(ctn)
|
||||
|
||||
projectID := getProjectID(r)
|
||||
|
||||
var (
|
||||
err error
|
||||
url string
|
||||
)
|
||||
|
||||
url = "http://" + string(cfg.Client.BaseURL) + string(cfg.Client.Address) + "/pdf/" + string(projectID)
|
||||
fmt.Println(url)
|
||||
|
||||
// Create new PDF generator
|
||||
pdfg, err := wkhtmltopdf.NewPDFGenerator()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Set global options
|
||||
pdfg.Dpi.Set(300)
|
||||
pdfg.Orientation.Set(wkhtmltopdf.OrientationPortrait)
|
||||
pdfg.Grayscale.Set(true)
|
||||
|
||||
// Create a new input page from an URL
|
||||
page := wkhtmltopdf.NewPage(url)
|
||||
|
||||
// Set options for this page
|
||||
page.FooterRight.Set("[page]")
|
||||
page.FooterFontSize.Set(10)
|
||||
page.Zoom.Set(0.95)
|
||||
|
||||
// Add to document
|
||||
pdfg.AddPage(page)
|
||||
|
||||
// Create PDF document in internal buffer
|
||||
err = pdfg.Create()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Write buffer contents to file on disk
|
||||
err = pdfg.WriteFile("./sample.pdf")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
rsp, err := writePDF(w, http.StatusOK, pdfg.Bytes())
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "could not write pdf response"))
|
||||
}
|
||||
|
||||
fmt.Println(rsp)
|
||||
}
|
||||
|
||||
type createRequest struct {
|
||||
Project *model.Project `json:"project"`
|
||||
}
|
||||
@ -246,3 +305,12 @@ func writeJSON(w http.ResponseWriter, statusCode int, data interface{}) error {
|
||||
|
||||
return encoder.Encode(data)
|
||||
}
|
||||
|
||||
func writePDF(w http.ResponseWriter, statusCode int, data []byte) (int, error) {
|
||||
w.Header().Set("Content-Disposition", "attachment; filename=foo.pdf")
|
||||
w.Header().Set("Content-Type", "application/pdf")
|
||||
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
return w.Write(data)
|
||||
}
|
||||
|
Reference in New Issue
Block a user