mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-11 18:02:14 +01:00
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package printer
|
|
|
|
import (
|
|
"context"
|
|
"os/exec"
|
|
|
|
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
|
"github.com/thecodingmachine/gotenberg/internal/pkg/timeout"
|
|
)
|
|
|
|
type merge struct {
|
|
ctx context.Context
|
|
fpaths []string
|
|
opts *MergeOptions
|
|
}
|
|
|
|
// MergeOptions helps customizing the
|
|
// merge printer behaviour.
|
|
type MergeOptions struct {
|
|
WaitTimeout float64
|
|
}
|
|
|
|
// NewMerge returns a merge printer.
|
|
func NewMerge(fpaths []string, opts *MergeOptions) Printer {
|
|
return &merge{
|
|
fpaths: fpaths,
|
|
opts: opts,
|
|
}
|
|
}
|
|
|
|
func (p *merge) Print(destination string) error {
|
|
const op string = "printer.merge.Print"
|
|
if p.ctx == nil {
|
|
ctx, cancel := timeout.Context(p.opts.WaitTimeout)
|
|
defer cancel()
|
|
p.ctx = ctx
|
|
}
|
|
resolver := func() error {
|
|
var cmdArgs []string
|
|
cmdArgs = append(cmdArgs, p.fpaths...)
|
|
cmdArgs = append(cmdArgs, "cat", "output", destination)
|
|
cmd := exec.CommandContext(p.ctx, "pdftk", cmdArgs...)
|
|
_, err := cmd.Output()
|
|
if err != nil {
|
|
return &standarderror.Error{Op: op, Err: err}
|
|
}
|
|
return nil
|
|
}
|
|
if err := resolver(); err != nil {
|
|
return timeout.Err(p.ctx, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Compile-time checks to ensure type implements desired interfaces.
|
|
var (
|
|
_ = Printer(new(merge))
|
|
)
|