From d6fe895f6eb2da85773104bfd5889a6e7893dd00 Mon Sep 17 00:00:00 2001 From: maniack Date: Mon, 17 Jun 2024 09:31:42 +0300 Subject: [PATCH] feat(libreoffice): add exportNotesInMargin form field (#904) * ExportNotesInMargin allows to export comments in margin https://help.libreoffice.org/latest/en-US/text/shared/guide/pdf_params.html * Resolve requested changes Move ExportNotesInMargin above LosslessImageCompression Remove overhead ExportNotes=true --- pkg/modules/libreoffice/api/api.go | 4 +++ pkg/modules/libreoffice/api/libreoffice.go | 4 +++ .../libreoffice/api/libreoffice_test.go | 29 +++++++++++++++++++ pkg/modules/libreoffice/routes.go | 3 ++ 4 files changed, 40 insertions(+) diff --git a/pkg/modules/libreoffice/api/api.go b/pkg/modules/libreoffice/api/api.go index aa81c7ba..1becfba0 100644 --- a/pkg/modules/libreoffice/api/api.go +++ b/pkg/modules/libreoffice/api/api.go @@ -59,6 +59,10 @@ type Options struct { // Optional SinglePageSheets bool + // ExportNotesInMargin allows to export comments in margin. + // Optional + ExportNotesInMargin bool + // LosslessImageCompression allows turning lossless compression on or off // to tweak image conversion performance. // Optional diff --git a/pkg/modules/libreoffice/api/libreoffice.go b/pkg/modules/libreoffice/api/libreoffice.go index bc4627bf..1f8205d7 100644 --- a/pkg/modules/libreoffice/api/libreoffice.go +++ b/pkg/modules/libreoffice/api/libreoffice.go @@ -281,6 +281,10 @@ func (p *libreOfficeProcess) pdf(ctx context.Context, logger *zap.Logger, inputP args = append(args, "--export", "SinglePageSheets=true") } + if options.ExportNotesInMargin { + args = append(args, "--export", "ExportNotesInMargin=true") + } + if options.LosslessImageCompression { args = append(args, "--export", "UseLosslessCompression=true") } diff --git a/pkg/modules/libreoffice/api/libreoffice_test.go b/pkg/modules/libreoffice/api/libreoffice_test.go index 5f6d9de9..1c2a2639 100644 --- a/pkg/modules/libreoffice/api/libreoffice_test.go +++ b/pkg/modules/libreoffice/api/libreoffice_test.go @@ -451,6 +451,35 @@ func TestLibreOfficeProcess_pdf(t *testing.T) { start: true, expectError: false, }, + { + scenario: "success ExportNotesInMargin", + 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("ExportNotesInMargin"), 0o755) + if err != nil { + t.Fatalf("expected no error but got: %v", err) + } + + return fs + }(), + options: Options{ExportNotesInMargin: true}, + cancelledCtx: false, + start: true, + expectError: false, + }, { scenario: "success LosslessImageCompression", libreOffice: newLibreOfficeProcess( diff --git a/pkg/modules/libreoffice/routes.go b/pkg/modules/libreoffice/routes.go index 8c2f9864..8441b477 100644 --- a/pkg/modules/libreoffice/routes.go +++ b/pkg/modules/libreoffice/routes.go @@ -30,6 +30,7 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap nativePageRanges string exportFormFields bool singlePageSheets bool + exportNotesInMargin bool losslessImageCompression bool reduceImageResolution bool pdfa string @@ -45,6 +46,7 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap String("nativePageRanges", &nativePageRanges, ""). Bool("exportFormFields", &exportFormFields, true). Bool("singlePageSheets", &singlePageSheets, false). + Bool("exportNotesInMargin", &exportNotesInMargin, false). Bool("losslessImageCompression", &losslessImageCompression, false). Bool("reduceImageResolution", &reduceImageResolution, true). String("pdfa", &pdfa, ""). @@ -79,6 +81,7 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap PageRanges: nativePageRanges, ExportFormFields: exportFormFields, SinglePageSheets: singlePageSheets, + ExportNotesInMargin: exportNotesInMargin, LosslessImageCompression: losslessImageCompression, ReduceImageResolution: reduceImageResolution, }