adding pageRanges formField

This commit is contained in:
Julien Neuhart
2019-10-04 17:49:26 +02:00
parent fa9963a737
commit 70dfea5876
4 changed files with 64 additions and 16 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/mafredri/cdp"
@@ -40,6 +41,7 @@ type ChromePrinterOptions struct {
MarginLeft float64
MarginRight float64
Landscape bool
PageRanges string
}
// DefaultChromePrinterOptions returns the default
@@ -58,6 +60,7 @@ func DefaultChromePrinterOptions(config conf.Config) ChromePrinterOptions {
MarginLeft: 1.0,
MarginRight: 1.0,
Landscape: false,
PageRanges: "",
}
}
@@ -142,23 +145,35 @@ func (p chromePrinter) Print(destination string) error {
} else {
p.logger.DebugOp(op, "no wait delay to apply, moving on...")
}
printToPdfArgs := page.NewPrintToPDFArgs().
SetPaperWidth(p.opts.PaperWidth).
SetPaperHeight(p.opts.PaperHeight).
SetMarginTop(p.opts.MarginTop).
SetMarginBottom(p.opts.MarginBottom).
SetMarginLeft(p.opts.MarginLeft).
SetMarginRight(p.opts.MarginRight).
SetLandscape(p.opts.Landscape).
SetDisplayHeaderFooter(true).
SetHeaderTemplate(p.opts.HeaderHTML).
SetFooterTemplate(p.opts.FooterHTML).
SetPrintBackground(true)
if p.opts.PageRanges != "" {
printToPdfArgs.SetPageRanges(p.opts.PageRanges)
}
// print the page to PDF.
// TODO catch page range error?
print, err := targetClient.Page.PrintToPDF(
ctx,
page.NewPrintToPDFArgs().
SetPaperWidth(p.opts.PaperWidth).
SetPaperHeight(p.opts.PaperHeight).
SetMarginTop(p.opts.MarginTop).
SetMarginBottom(p.opts.MarginBottom).
SetMarginLeft(p.opts.MarginLeft).
SetMarginRight(p.opts.MarginRight).
SetLandscape(p.opts.Landscape).
SetDisplayHeaderFooter(true).
SetHeaderTemplate(p.opts.HeaderHTML).
SetFooterTemplate(p.opts.FooterHTML).
SetPrintBackground(true),
printToPdfArgs,
)
if err != nil {
if strings.Contains(err.Error(), "Page range syntax error") {
return xerror.Invalid(
"",
fmt.Sprintf("'%s' is not a valid Google Chrome page ranges", p.opts.PageRanges),
err,
)
}
return err
}
if err := ioutil.WriteFile(destination, print.Data, 0644); err != nil {