From 669a35afef554f290663eff21237e1420f180738 Mon Sep 17 00:00:00 2001 From: garethjudson Date: Thu, 13 Jun 2024 18:28:36 +1000 Subject: [PATCH] feat(libreoffice): add image options (#898) * feat: libre office image options * refactor: PR comment changes * fix: change lossLessImageCompression default to false --------- Co-authored-by: Gareth Judson --- pkg/modules/libreoffice/api/api.go | 8 +++ pkg/modules/libreoffice/api/libreoffice.go | 8 +++ .../libreoffice/api/libreoffice_test.go | 58 +++++++++++++++++++ pkg/modules/libreoffice/routes.go | 34 ++++++----- 4 files changed, 94 insertions(+), 14 deletions(-) diff --git a/pkg/modules/libreoffice/api/api.go b/pkg/modules/libreoffice/api/api.go index af9f54b0..44273f53 100644 --- a/pkg/modules/libreoffice/api/api.go +++ b/pkg/modules/libreoffice/api/api.go @@ -58,6 +58,14 @@ type Options struct { // Optional SinglePageSheets bool + // LosslessImageCompression allows turning lossless compression on or off to tweak image conversion performance. + // Optional + LosslessImageCompression bool + + // ReduceImageResolution allows turning on or off image resolution reduction to tweak image conversion performance. + // Optional + ReduceImageResolution bool + // PdfFormats allows to convert the resulting PDF to PDF/A-1b, PDF/A-2b, // PDF/A-3b and PDF/UA. // Optional. diff --git a/pkg/modules/libreoffice/api/libreoffice.go b/pkg/modules/libreoffice/api/libreoffice.go index 804cfa3f..bc4627bf 100644 --- a/pkg/modules/libreoffice/api/libreoffice.go +++ b/pkg/modules/libreoffice/api/libreoffice.go @@ -281,6 +281,14 @@ func (p *libreOfficeProcess) pdf(ctx context.Context, logger *zap.Logger, inputP args = append(args, "--export", "SinglePageSheets=true") } + if options.LosslessImageCompression { + args = append(args, "--export", "UseLosslessCompression=true") + } + + if !options.ReduceImageResolution { + args = append(args, "--export", "ReduceImageResolution=false") + } + switch options.PdfFormats.PdfA { case "": case gotenberg.PdfA1b: diff --git a/pkg/modules/libreoffice/api/libreoffice_test.go b/pkg/modules/libreoffice/api/libreoffice_test.go index ea091e70..5f6d9de9 100644 --- a/pkg/modules/libreoffice/api/libreoffice_test.go +++ b/pkg/modules/libreoffice/api/libreoffice_test.go @@ -451,6 +451,64 @@ func TestLibreOfficeProcess_pdf(t *testing.T) { start: true, expectError: false, }, + { + scenario: "success LosslessImageCompression", + libreOffice: newLibreOfficeProcess( + libreOfficeArguments{ + binPath: os.Getenv("LIBREOFFICE_BIN_PATH"), + unoBinPath: os.Getenv("UNOCONVERTER_BIN_PATH"), + startTimeout: 5 * time.Second, + }, + ), + fs: func() *gotenberg.FileSystem { + fs := gotenberg.NewFileSystem() + + err := os.MkdirAll(fs.WorkingDirPath(), 0o755) + if err != nil { + t.Fatalf(fmt.Sprintf("expected no error but got: %v", err)) + } + + err = os.WriteFile(fmt.Sprintf("%s/document.txt", fs.WorkingDirPath()), []byte("LosslessImageCompression"), 0o755) + if err != nil { + t.Fatalf("expected no error but got: %v", err) + } + + return fs + }(), + options: Options{LosslessImageCompression: true}, + cancelledCtx: false, + start: true, + expectError: false, + }, + { + scenario: "success ReduceImageResolution", + libreOffice: newLibreOfficeProcess( + libreOfficeArguments{ + binPath: os.Getenv("LIBREOFFICE_BIN_PATH"), + unoBinPath: os.Getenv("UNOCONVERTER_BIN_PATH"), + startTimeout: 5 * time.Second, + }, + ), + fs: func() *gotenberg.FileSystem { + fs := gotenberg.NewFileSystem() + + err := os.MkdirAll(fs.WorkingDirPath(), 0o755) + if err != nil { + t.Fatalf(fmt.Sprintf("expected no error but got: %v", err)) + } + + err = os.WriteFile(fmt.Sprintf("%s/document.txt", fs.WorkingDirPath()), []byte("ReduceImageResolution"), 0o755) + if err != nil { + t.Fatalf("expected no error but got: %v", err) + } + + return fs + }(), + options: Options{ReduceImageResolution: false}, + cancelledCtx: false, + start: true, + expectError: false, + }, { scenario: "success (PDF/A-1b)", libreOffice: newLibreOfficeProcess( diff --git a/pkg/modules/libreoffice/routes.go b/pkg/modules/libreoffice/routes.go index 25dfef4d..8c2f9864 100644 --- a/pkg/modules/libreoffice/routes.go +++ b/pkg/modules/libreoffice/routes.go @@ -25,16 +25,18 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap // Let's get the data from the form and validate them. var ( - inputPaths []string - landscape bool - nativePageRanges string - exportFormFields bool - singlePageSheets bool - pdfa string - pdfua bool - nativePdfFormats bool - merge bool - metadata map[string]interface{} + inputPaths []string + landscape bool + nativePageRanges string + exportFormFields bool + singlePageSheets bool + losslessImageCompression bool + reduceImageResolution bool + pdfa string + pdfua bool + nativePdfFormats bool + merge bool + metadata map[string]interface{} ) err := ctx.FormData(). @@ -43,6 +45,8 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap String("nativePageRanges", &nativePageRanges, ""). Bool("exportFormFields", &exportFormFields, true). Bool("singlePageSheets", &singlePageSheets, false). + Bool("losslessImageCompression", &losslessImageCompression, false). + Bool("reduceImageResolution", &reduceImageResolution, true). String("pdfa", &pdfa, ""). Bool("pdfua", &pdfua, false). Bool("nativePdfFormats", &nativePdfFormats, true). @@ -71,10 +75,12 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap for i, inputPath := range inputPaths { outputPaths[i] = ctx.GeneratePath(".pdf") options := libreofficeapi.Options{ - Landscape: landscape, - PageRanges: nativePageRanges, - ExportFormFields: exportFormFields, - SinglePageSheets: singlePageSheets, + Landscape: landscape, + PageRanges: nativePageRanges, + ExportFormFields: exportFormFields, + SinglePageSheets: singlePageSheets, + LosslessImageCompression: losslessImageCompression, + ReduceImageResolution: reduceImageResolution, } if nativePdfFormats {