mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-17 20:52:14 +01:00
fix(qpdf): allow warnings (#1135)
* Optionally allow warnings in QPDF operations * warnings are not errors by default * Apply suggestions from code review Co-authored-by: Julien Neuhart <neuhart.julien@gmail.com> * fix some merge artifacts * follow the args convention --------- Co-authored-by: Julien Neuhart <neuhart.julien@gmail.com>
This commit is contained in:
@@ -22,7 +22,8 @@ func init() {
|
|||||||
// QPdf abstracts the CLI tool QPDF and implements the [gotenberg.PdfEngine]
|
// QPdf abstracts the CLI tool QPDF and implements the [gotenberg.PdfEngine]
|
||||||
// interface.
|
// interface.
|
||||||
type QPdf struct {
|
type QPdf struct {
|
||||||
binPath string
|
binPath string
|
||||||
|
globalArgs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Descriptor returns a [QPdf]'s module descriptor.
|
// Descriptor returns a [QPdf]'s module descriptor.
|
||||||
@@ -41,6 +42,8 @@ func (engine *QPdf) Provision(ctx *gotenberg.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
engine.binPath = binPath
|
engine.binPath = binPath
|
||||||
|
// Warnings should not cause errors.
|
||||||
|
engine.globalArgs = []string{"--warning-exit-0"}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -88,7 +91,10 @@ func (engine *QPdf) Split(ctx context.Context, logger *zap.Logger, mode gotenber
|
|||||||
if !mode.Unify {
|
if !mode.Unify {
|
||||||
return nil, fmt.Errorf("split PDFs using mode '%s' without unify with QPDF: %w", mode.Mode, gotenberg.ErrPdfSplitModeNotSupported)
|
return nil, fmt.Errorf("split PDFs using mode '%s' without unify with QPDF: %w", mode.Mode, gotenberg.ErrPdfSplitModeNotSupported)
|
||||||
}
|
}
|
||||||
args = append(args, inputPath, "--pages", ".", mode.Span, "--", outputPath)
|
args = append(args, inputPath)
|
||||||
|
args = append(args, engine.globalArgs...)
|
||||||
|
args = append(args, "--pages", ".", mode.Span)
|
||||||
|
args = append(args, "--", outputPath)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("split PDFs using mode '%s' with QPDF: %w", mode.Mode, gotenberg.ErrPdfSplitModeNotSupported)
|
return nil, fmt.Errorf("split PDFs using mode '%s' with QPDF: %w", mode.Mode, gotenberg.ErrPdfSplitModeNotSupported)
|
||||||
}
|
}
|
||||||
@@ -110,6 +116,7 @@ func (engine *QPdf) Split(ctx context.Context, logger *zap.Logger, mode gotenber
|
|||||||
func (engine *QPdf) Merge(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
func (engine *QPdf) Merge(ctx context.Context, logger *zap.Logger, inputPaths []string, outputPath string) error {
|
||||||
var args []string
|
var args []string
|
||||||
args = append(args, "--empty")
|
args = append(args, "--empty")
|
||||||
|
args = append(args, engine.globalArgs...)
|
||||||
args = append(args, "--pages")
|
args = append(args, "--pages")
|
||||||
args = append(args, inputPaths...)
|
args = append(args, inputPaths...)
|
||||||
args = append(args, "--", outputPath)
|
args = append(args, "--", outputPath)
|
||||||
@@ -131,10 +138,11 @@ func (engine *QPdf) Merge(ctx context.Context, logger *zap.Logger, inputPaths []
|
|||||||
// original annotations.
|
// original annotations.
|
||||||
func (engine *QPdf) Flatten(ctx context.Context, logger *zap.Logger, inputPath string) error {
|
func (engine *QPdf) Flatten(ctx context.Context, logger *zap.Logger, inputPath string) error {
|
||||||
var args []string
|
var args []string
|
||||||
|
args = append(args, inputPath)
|
||||||
args = append(args, "--generate-appearances")
|
args = append(args, "--generate-appearances")
|
||||||
args = append(args, "--flatten-annotations=all")
|
args = append(args, "--flatten-annotations=all")
|
||||||
args = append(args, "--replace-input")
|
args = append(args, "--replace-input")
|
||||||
args = append(args, inputPath)
|
args = append(args, engine.globalArgs...)
|
||||||
|
|
||||||
cmd, err := gotenberg.CommandContext(ctx, logger, engine.binPath, args...)
|
cmd, err := gotenberg.CommandContext(ctx, logger, engine.binPath, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -154,6 +154,15 @@ func TestQPdf_Merge(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
scenario: "success even with warnings",
|
||||||
|
ctx: context.TODO(),
|
||||||
|
inputPaths: []string{
|
||||||
|
"/tests/test/testdata/pdfengines/sample1.pdf",
|
||||||
|
"/tests/test/testdata/pdfengines/sample5.pdf",
|
||||||
|
},
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.scenario, func(t *testing.T) {
|
t.Run(tc.scenario, func(t *testing.T) {
|
||||||
engine := new(QPdf)
|
engine := new(QPdf)
|
||||||
@@ -236,6 +245,14 @@ func TestQPdf_Split(t *testing.T) {
|
|||||||
expectError: false,
|
expectError: false,
|
||||||
expectOutputPathsCount: 1,
|
expectOutputPathsCount: 1,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
scenario: "success even with warnings",
|
||||||
|
ctx: context.TODO(),
|
||||||
|
mode: gotenberg.SplitMode{Mode: gotenberg.SplitModePages, Span: "1-2", Unify: true},
|
||||||
|
inputPath: "/tests/test/testdata/pdfengines/sample5.pdf",
|
||||||
|
expectError: false,
|
||||||
|
expectOutputPathsCount: 1,
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.scenario, func(t *testing.T) {
|
t.Run(tc.scenario, func(t *testing.T) {
|
||||||
engine := new(QPdf)
|
engine := new(QPdf)
|
||||||
@@ -304,6 +321,13 @@ func TestQPdf_Flatten(t *testing.T) {
|
|||||||
createCopy: true,
|
createCopy: true,
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
scenario: "success even with warnings",
|
||||||
|
ctx: context.TODO(),
|
||||||
|
inputPath: "/tests/test/testdata/pdfengines/sample5.pdf",
|
||||||
|
createCopy: true,
|
||||||
|
expectError: false,
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.scenario, func(t *testing.T) {
|
t.Run(tc.scenario, func(t *testing.T) {
|
||||||
engine := new(QPdf)
|
engine := new(QPdf)
|
||||||
|
|||||||
BIN
test/testdata/pdfengines/sample5.pdf
vendored
Normal file
BIN
test/testdata/pdfengines/sample5.pdf
vendored
Normal file
Binary file not shown.
Reference in New Issue
Block a user