mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-18 21:22:15 +01:00
adding pageRanges formField
This commit is contained in:
@@ -48,6 +48,10 @@ func chromePrinterOptions(r resource.Resource, config conf.Config) (printer.Chro
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return printer.ChromePrinterOptions{}, err
|
return printer.ChromePrinterOptions{}, err
|
||||||
}
|
}
|
||||||
|
pageRanges, err := r.StringArg(resource.PageRangesArgKey, "")
|
||||||
|
if err != nil {
|
||||||
|
return printer.ChromePrinterOptions{}, err
|
||||||
|
}
|
||||||
return printer.ChromePrinterOptions{
|
return printer.ChromePrinterOptions{
|
||||||
WaitTimeout: waitTimeout,
|
WaitTimeout: waitTimeout,
|
||||||
WaitDelay: waitDelay,
|
WaitDelay: waitDelay,
|
||||||
@@ -60,6 +64,7 @@ func chromePrinterOptions(r resource.Resource, config conf.Config) (printer.Chro
|
|||||||
MarginLeft: marginLeft,
|
MarginLeft: marginLeft,
|
||||||
MarginRight: marginRight,
|
MarginRight: marginRight,
|
||||||
Landscape: landscape,
|
Landscape: landscape,
|
||||||
|
PageRanges: pageRanges,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
opts, err := resolver()
|
opts, err := resolver()
|
||||||
@@ -80,9 +85,14 @@ func officePrinterOptions(r resource.Resource, config conf.Config) (printer.Offi
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return printer.OfficePrinterOptions{}, err
|
return printer.OfficePrinterOptions{}, err
|
||||||
}
|
}
|
||||||
|
pageRanges, err := r.StringArg(resource.PageRangesArgKey, "")
|
||||||
|
if err != nil {
|
||||||
|
return printer.OfficePrinterOptions{}, err
|
||||||
|
}
|
||||||
return printer.OfficePrinterOptions{
|
return printer.OfficePrinterOptions{
|
||||||
WaitTimeout: waitTimeout,
|
WaitTimeout: waitTimeout,
|
||||||
Landscape: landscape,
|
Landscape: landscape,
|
||||||
|
PageRanges: pageRanges,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
opts, err := resolver()
|
opts, err := resolver()
|
||||||
|
|||||||
@@ -51,6 +51,9 @@ const (
|
|||||||
// LandscapeArgKey is the key
|
// LandscapeArgKey is the key
|
||||||
// of the argument "landscape".
|
// of the argument "landscape".
|
||||||
LandscapeArgKey ArgKey = "landscape"
|
LandscapeArgKey ArgKey = "landscape"
|
||||||
|
// PageRangesArgKey is the key
|
||||||
|
// of the argument "pageRanges".
|
||||||
|
PageRangesArgKey ArgKey = "pageRanges"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -73,6 +76,7 @@ func ArgKeys() []ArgKey {
|
|||||||
MarginLeftArgKey,
|
MarginLeftArgKey,
|
||||||
MarginRightArgKey,
|
MarginRightArgKey,
|
||||||
LandscapeArgKey,
|
LandscapeArgKey,
|
||||||
|
PageRangesArgKey,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mafredri/cdp"
|
"github.com/mafredri/cdp"
|
||||||
@@ -40,6 +41,7 @@ type ChromePrinterOptions struct {
|
|||||||
MarginLeft float64
|
MarginLeft float64
|
||||||
MarginRight float64
|
MarginRight float64
|
||||||
Landscape bool
|
Landscape bool
|
||||||
|
PageRanges string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultChromePrinterOptions returns the default
|
// DefaultChromePrinterOptions returns the default
|
||||||
@@ -58,6 +60,7 @@ func DefaultChromePrinterOptions(config conf.Config) ChromePrinterOptions {
|
|||||||
MarginLeft: 1.0,
|
MarginLeft: 1.0,
|
||||||
MarginRight: 1.0,
|
MarginRight: 1.0,
|
||||||
Landscape: false,
|
Landscape: false,
|
||||||
|
PageRanges: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,10 +145,7 @@ func (p chromePrinter) Print(destination string) error {
|
|||||||
} else {
|
} else {
|
||||||
p.logger.DebugOp(op, "no wait delay to apply, moving on...")
|
p.logger.DebugOp(op, "no wait delay to apply, moving on...")
|
||||||
}
|
}
|
||||||
// print the page to PDF.
|
printToPdfArgs := page.NewPrintToPDFArgs().
|
||||||
print, err := targetClient.Page.PrintToPDF(
|
|
||||||
ctx,
|
|
||||||
page.NewPrintToPDFArgs().
|
|
||||||
SetPaperWidth(p.opts.PaperWidth).
|
SetPaperWidth(p.opts.PaperWidth).
|
||||||
SetPaperHeight(p.opts.PaperHeight).
|
SetPaperHeight(p.opts.PaperHeight).
|
||||||
SetMarginTop(p.opts.MarginTop).
|
SetMarginTop(p.opts.MarginTop).
|
||||||
@@ -156,9 +156,24 @@ func (p chromePrinter) Print(destination string) error {
|
|||||||
SetDisplayHeaderFooter(true).
|
SetDisplayHeaderFooter(true).
|
||||||
SetHeaderTemplate(p.opts.HeaderHTML).
|
SetHeaderTemplate(p.opts.HeaderHTML).
|
||||||
SetFooterTemplate(p.opts.FooterHTML).
|
SetFooterTemplate(p.opts.FooterHTML).
|
||||||
SetPrintBackground(true),
|
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,
|
||||||
|
printToPdfArgs,
|
||||||
)
|
)
|
||||||
if err != nil {
|
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
|
return err
|
||||||
}
|
}
|
||||||
if err := ioutil.WriteFile(destination, print.Data, 0644); err != nil {
|
if err := ioutil.WriteFile(destination, print.Data, 0644); err != nil {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/phayes/freeport"
|
"github.com/phayes/freeport"
|
||||||
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
|
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
|
||||||
@@ -26,6 +27,7 @@ type officePrinter struct {
|
|||||||
type OfficePrinterOptions struct {
|
type OfficePrinterOptions struct {
|
||||||
WaitTimeout float64
|
WaitTimeout float64
|
||||||
Landscape bool
|
Landscape bool
|
||||||
|
PageRanges string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultOfficePrinterOptions returns the default
|
// DefaultOfficePrinterOptions returns the default
|
||||||
@@ -34,6 +36,7 @@ func DefaultOfficePrinterOptions(config conf.Config) OfficePrinterOptions {
|
|||||||
return OfficePrinterOptions{
|
return OfficePrinterOptions{
|
||||||
WaitTimeout: config.DefaultWaitTimeout(),
|
WaitTimeout: config.DefaultWaitTimeout(),
|
||||||
Landscape: false,
|
Landscape: false,
|
||||||
|
PageRanges: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +62,7 @@ func (p officePrinter) Print(destination string) error {
|
|||||||
baseFilename := xrand.Get()
|
baseFilename := xrand.Get()
|
||||||
tmpDest := fmt.Sprintf("%s/%d%s.pdf", dirPath, i, baseFilename)
|
tmpDest := fmt.Sprintf("%s/%d%s.pdf", dirPath, i, baseFilename)
|
||||||
p.logger.DebugfOp(op, "converting '%s' to PDF...", fpath)
|
p.logger.DebugfOp(op, "converting '%s' to PDF...", fpath)
|
||||||
if err := unoconv(ctx, p.logger, fpath, tmpDest, p.opts); err != nil {
|
if err := p.unoconv(ctx, fpath, tmpDest); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.logger.DebugfOp(op, "'%s.pdf' created", baseFilename)
|
p.logger.DebugfOp(op, "'%s.pdf' created", baseFilename)
|
||||||
@@ -85,9 +88,12 @@ func (p officePrinter) Print(destination string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string, opts OfficePrinterOptions) error {
|
func (p officePrinter) unoconv(ctx context.Context, fpath, destination string) error {
|
||||||
const op string = "printer.unoconv"
|
const op string = "printer.unoconv"
|
||||||
resolver := func() error {
|
resolver := func() error {
|
||||||
|
hasPageRanges := func() bool {
|
||||||
|
return p.opts.PageRanges != "" && len(p.fpaths) == 1
|
||||||
|
}
|
||||||
port, err := freeport.GetFreePort()
|
port, err := freeport.GetFreePort()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -100,11 +106,24 @@ func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string,
|
|||||||
"--format",
|
"--format",
|
||||||
"pdf",
|
"pdf",
|
||||||
}
|
}
|
||||||
if opts.Landscape {
|
if p.opts.Landscape {
|
||||||
args = append(args, "--printer", "PaperOrientation=landscape")
|
args = append(args, "--printer", "PaperOrientation=landscape")
|
||||||
}
|
}
|
||||||
|
if hasPageRanges() {
|
||||||
|
args = append(args, "--export", fmt.Sprintf("PageRange=%s", p.opts.PageRanges))
|
||||||
|
}
|
||||||
args = append(args, "--output", destination, fpath)
|
args = append(args, "--output", destination, fpath)
|
||||||
return xexec.Run(ctx, logger, "unoconv", args...)
|
if err := xexec.Run(ctx, p.logger, "unoconv", args...); err != nil {
|
||||||
|
if hasPageRanges() && strings.Contains(err.Error(), "exit status 5") {
|
||||||
|
return xerror.Invalid(
|
||||||
|
"",
|
||||||
|
fmt.Sprintf("'%s' is not a valid LibreOffice page ranges", p.opts.PageRanges),
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
if err := resolver(); err != nil {
|
if err := resolver(); err != nil {
|
||||||
return xerror.New(op, err)
|
return xerror.New(op, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user