Skip to content

Commit

Permalink
Add PDF support (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
glupmjoed committed Jan 23, 2024
1 parent 486067a commit 4ef8c3b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
8 changes: 8 additions & 0 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ func outerHTML(out *[]string) chromedp.ActionFunc {
}
}

func printToPDF(buf *[]byte) chromedp.ActionFunc {
return func(ctx context.Context) error {
var err error
*buf, _, err = page.PrintToPDF().Do(ctx)
return err
}
}

func removeElements(sel string) chromedp.ActionFunc {
return func(ctx context.Context) error {
cmd := fmt.Sprintf("document.querySelectorAll('%s').forEach(e => e.remove());", sel)
Expand Down
37 changes: 25 additions & 12 deletions cmd/decap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ func browseHandler(w http.ResponseWriter, req *http.Request) {

// execute query

err_status := http.StatusInternalServerError
var res *decap.Result
res, err = dec.Execute()
if err != nil {
// TODO: Propagate HTTP status properly
status := http.StatusInternalServerError
msg := fmt.Sprintf("%s: %s", http.StatusText(status), err)
http.Error(w, msg, status)
msg := fmt.Sprintf("%s: %s", http.StatusText(err_status), err)
http.Error(w, msg, err_status)
return
}

Expand All @@ -121,21 +121,34 @@ func browseHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(res)
if err != nil {
status := http.StatusInternalServerError
msg := fmt.Sprintf("%s: %s", http.StatusText(status), "Couldn't encode response")
http.Error(w, msg, status)
return
msg := fmt.Sprintf("%s: %s", http.StatusText(err_status), "Couldn't encode response")
http.Error(w, msg, err_status)
}
return
case "pdf":
w.Header().Set("Content-Type", "application/pdf")
_, err = w.Write(res.PDFBuffer())
if err != nil {
msg := fmt.Sprintf("%s: %s",
http.StatusText(err_status), "Couldn't write response bytes")
http.Error(w, msg, err_status)
}
return
case "png":
w.Header().Set("Content-Type", "image/png")
_, err := w.Write(res.ImgBuffer())
_, err = w.Write(res.ImgBuffer())
if err != nil {
status := http.StatusInternalServerError
msg := fmt.Sprintf("%s: %s",
http.StatusText(status), "Couldn't write response bytes")
http.Error(w, msg, status)
return
http.StatusText(err_status), "Couldn't write response bytes")
http.Error(w, msg, err_status)
}
return
default:
fmt.Fprintf(os.Stderr, `unknown results type "%s"`, res.Type())
msg := fmt.Sprintf(`%s: Unknown result type "%s"`,
http.StatusText(err_status), res.Type())
http.Error(w, msg, err_status)
return
}
}

Expand Down
19 changes: 17 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,28 @@ type Result struct {
TabID string `json:"tab_id"`
WindowID string `json:"window_id"`
img []byte
pdf []byte
}

func (res *Result) Type() string {
if len(res.img) != 0 {
switch {
case len(res.pdf) != 0:
return "pdf"
case len(res.img) != 0:
return "png"
default:
return "json"
}
return "json"
}

func (res *Result) ImgBuffer() []byte {
return res.img
}

func (res *Result) PDFBuffer() []byte {
return res.pdf
}

type QueryBlock struct {
Actions []Action `json:"actions"`
Repeat *int `json:"repeat"`
Expand Down Expand Up @@ -416,6 +425,12 @@ func (r *Request) parseAction(xa Action) error {
}
r.appendActions(outerHTML(&r.res.Out[r.pos]))

case "print_to_pdf":
if err = xa.MustArgCount(0); err != nil {
return err
}
r.appendActions(printToPDF(&r.res.pdf))

case "remove":
if len(xa.Args()) == 0 {
return fmt.Errorf("remove: expected at least one argument")
Expand Down

0 comments on commit 4ef8c3b

Please sign in to comment.