// readerToFile copies the reader to a temp file and returns its name or error func readerToFile(r io.Reader, prefix string) (filename string, err error) { dfh, e := ioutil.TempFile("", "agostle-"+baseName(prefix)+"-") if e != nil { err = e return } if sfh, ok := r.(*os.File); ok { filename = dfh.Name() _ = dfh.Close() _ = os.Remove(filename) err = temp.LinkOrCopy(sfh.Name(), filename) return } if _, err = io.Copy(dfh, r); err == nil { filename = dfh.Name() } _ = dfh.Close() return }
// PdfMerge merges pdf files into destfn func PdfMerge(ctx context.Context, destfn string, filenames ...string) error { if len(filenames) == 0 { return fmt.Errorf("filenames required!") } else if len(filenames) == 1 { return temp.LinkOrCopy(filenames[0], destfn) } var cmd *exec.Cmd args := append(make([]string, 0, len(filenames)+3), filenames...) pdfunite := popplerOk["pdfunite"] if pdfunite != "" { args = append(args, destfn) cmd = exec.Command(pdfunite, args...) } else { args = append(append(append(args, "cat"), "output"), destfn) cmd = exec.Command(*ConfPdftk, args...) } logger.Debug("msg", "PdfMerge calling", "command", fmt.Sprintf("%s", cmd.Args)) return runWithContext(ctx, cmd) }