mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-17 20:52:14 +01:00
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 <gareth.judson@delwp.vic.gov.au>
This commit is contained in:
@@ -58,6 +58,14 @@ type Options struct {
|
|||||||
// Optional
|
// Optional
|
||||||
SinglePageSheets bool
|
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,
|
// PdfFormats allows to convert the resulting PDF to PDF/A-1b, PDF/A-2b,
|
||||||
// PDF/A-3b and PDF/UA.
|
// PDF/A-3b and PDF/UA.
|
||||||
// Optional.
|
// Optional.
|
||||||
|
|||||||
@@ -281,6 +281,14 @@ func (p *libreOfficeProcess) pdf(ctx context.Context, logger *zap.Logger, inputP
|
|||||||
args = append(args, "--export", "SinglePageSheets=true")
|
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 {
|
switch options.PdfFormats.PdfA {
|
||||||
case "":
|
case "":
|
||||||
case gotenberg.PdfA1b:
|
case gotenberg.PdfA1b:
|
||||||
|
|||||||
@@ -451,6 +451,64 @@ func TestLibreOfficeProcess_pdf(t *testing.T) {
|
|||||||
start: true,
|
start: true,
|
||||||
expectError: false,
|
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)",
|
scenario: "success (PDF/A-1b)",
|
||||||
libreOffice: newLibreOfficeProcess(
|
libreOffice: newLibreOfficeProcess(
|
||||||
|
|||||||
@@ -25,16 +25,18 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap
|
|||||||
|
|
||||||
// Let's get the data from the form and validate them.
|
// Let's get the data from the form and validate them.
|
||||||
var (
|
var (
|
||||||
inputPaths []string
|
inputPaths []string
|
||||||
landscape bool
|
landscape bool
|
||||||
nativePageRanges string
|
nativePageRanges string
|
||||||
exportFormFields bool
|
exportFormFields bool
|
||||||
singlePageSheets bool
|
singlePageSheets bool
|
||||||
pdfa string
|
losslessImageCompression bool
|
||||||
pdfua bool
|
reduceImageResolution bool
|
||||||
nativePdfFormats bool
|
pdfa string
|
||||||
merge bool
|
pdfua bool
|
||||||
metadata map[string]interface{}
|
nativePdfFormats bool
|
||||||
|
merge bool
|
||||||
|
metadata map[string]interface{}
|
||||||
)
|
)
|
||||||
|
|
||||||
err := ctx.FormData().
|
err := ctx.FormData().
|
||||||
@@ -43,6 +45,8 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap
|
|||||||
String("nativePageRanges", &nativePageRanges, "").
|
String("nativePageRanges", &nativePageRanges, "").
|
||||||
Bool("exportFormFields", &exportFormFields, true).
|
Bool("exportFormFields", &exportFormFields, true).
|
||||||
Bool("singlePageSheets", &singlePageSheets, false).
|
Bool("singlePageSheets", &singlePageSheets, false).
|
||||||
|
Bool("losslessImageCompression", &losslessImageCompression, false).
|
||||||
|
Bool("reduceImageResolution", &reduceImageResolution, true).
|
||||||
String("pdfa", &pdfa, "").
|
String("pdfa", &pdfa, "").
|
||||||
Bool("pdfua", &pdfua, false).
|
Bool("pdfua", &pdfua, false).
|
||||||
Bool("nativePdfFormats", &nativePdfFormats, true).
|
Bool("nativePdfFormats", &nativePdfFormats, true).
|
||||||
@@ -71,10 +75,12 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap
|
|||||||
for i, inputPath := range inputPaths {
|
for i, inputPath := range inputPaths {
|
||||||
outputPaths[i] = ctx.GeneratePath(".pdf")
|
outputPaths[i] = ctx.GeneratePath(".pdf")
|
||||||
options := libreofficeapi.Options{
|
options := libreofficeapi.Options{
|
||||||
Landscape: landscape,
|
Landscape: landscape,
|
||||||
PageRanges: nativePageRanges,
|
PageRanges: nativePageRanges,
|
||||||
ExportFormFields: exportFormFields,
|
ExportFormFields: exportFormFields,
|
||||||
SinglePageSheets: singlePageSheets,
|
SinglePageSheets: singlePageSheets,
|
||||||
|
LosslessImageCompression: losslessImageCompression,
|
||||||
|
ReduceImageResolution: reduceImageResolution,
|
||||||
}
|
}
|
||||||
|
|
||||||
if nativePdfFormats {
|
if nativePdfFormats {
|
||||||
|
|||||||
Reference in New Issue
Block a user