From a3e416ff683bd00676b4c38930431d8cc8112975 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Thu, 3 Jan 2019 10:06:56 +0100 Subject: [PATCH] adding PDFtk for merging PDF files < 1.7 --- LICENSE | 2 +- build/base/Dockerfile | 12 +++++++++++- internal/pkg/printer/printer.go | 16 +++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/LICENSE b/LICENSE index 4188a9bb..b756c5a5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 TheCodingMachine +Copyright (c) 2019 TheCodingMachine Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/build/base/Dockerfile b/build/base/Dockerfile index aa75a535..a7d8419e 100644 --- a/build/base/Dockerfile +++ b/build/base/Dockerfile @@ -43,4 +43,14 @@ RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add RUN pip3 install unoconv &&\ # https://github.com/nextcloud/docker/issues/380 mkdir -p /usr/share/man/man1mkdir -p /usr/share/man/man1 &&\ - apt-get -y install libreoffice \ No newline at end of file + apt-get -y install libreoffice + +# |-------------------------------------------------------------------------- +# | PDFtk +# |-------------------------------------------------------------------------- +# | +# | Installs PDFtk as an alternative to pdfcpu for merging PDFs. +# | https://github.com/thecodingmachine/gotenberg/issues/29 +# | + +RUN apt-get -y install pdftk \ No newline at end of file diff --git a/internal/pkg/printer/printer.go b/internal/pkg/printer/printer.go index a3dc0dff..77d20b5c 100644 --- a/internal/pkg/printer/printer.go +++ b/internal/pkg/printer/printer.go @@ -3,6 +3,7 @@ package printer import ( "fmt" "io/ioutil" + "os/exec" pdfcpuAPI "github.com/hhrutter/pdfcpu/pkg/api" pdfcpuLog "github.com/hhrutter/pdfcpu/pkg/log" @@ -23,9 +24,18 @@ type Printer interface { // Merge merges PDF files. func Merge(fpaths []string, destination string) error { - cmd := pdfcpuAPI.MergeCommand(fpaths, destination, pdfcpuConfig.NewDefaultConfiguration()) - _, err := pdfcpuAPI.Merge(cmd) - return err + cmdcpu := pdfcpuAPI.MergeCommand(fpaths, destination, pdfcpuConfig.NewDefaultConfiguration()) + _, err := pdfcpuAPI.Merge(cmdcpu) + if err == nil { + return nil + } + // if pdfcpu failed to merge PDF files... + // https://github.com/thecodingmachine/gotenberg/issues/29 + var cmdArgs []string + cmdArgs = append(cmdArgs, fpaths...) + cmdArgs = append(cmdArgs, "cat", "output", destination) + cmd := exec.Command("pdftk", cmdArgs...) + return cmd.Run() } func writeBytesToFile(dst string, b []byte) error {