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

@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/phayes/freeport"
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
@@ -26,6 +27,7 @@ type officePrinter struct {
type OfficePrinterOptions struct {
WaitTimeout float64
Landscape bool
PageRanges string
}
// DefaultOfficePrinterOptions returns the default
@@ -34,6 +36,7 @@ func DefaultOfficePrinterOptions(config conf.Config) OfficePrinterOptions {
return OfficePrinterOptions{
WaitTimeout: config.DefaultWaitTimeout(),
Landscape: false,
PageRanges: "",
}
}
@@ -59,7 +62,7 @@ func (p officePrinter) Print(destination string) error {
baseFilename := xrand.Get()
tmpDest := fmt.Sprintf("%s/%d%s.pdf", dirPath, i, baseFilename)
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
}
p.logger.DebugfOp(op, "'%s.pdf' created", baseFilename)
@@ -85,9 +88,12 @@ func (p officePrinter) Print(destination string) error {
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"
resolver := func() error {
hasPageRanges := func() bool {
return p.opts.PageRanges != "" && len(p.fpaths) == 1
}
port, err := freeport.GetFreePort()
if err != nil {
return err
@@ -100,11 +106,24 @@ func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string,
"--format",
"pdf",
}
if opts.Landscape {
if p.opts.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)
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 {
return xerror.New(op, err)