Example #1
0
func pdfMergeEP(ctx context.Context, request interface{}) (response interface{}, err error) {
	req, ok := request.(pdfMergeRequest)
	if !ok {
		return nil, errgo.Newf("awaited pdfMergeRequest, got %T", request)
	}
	defer func() {
		for _, f := range req.Inputs {
			f.Close()
		}
	}()

	if sortBeforeMerge && req.Sort != NoSort || !sortBeforeMerge && req.Sort != DoSort {
		logger.Info("msg", "sorting filenames, as requested", "ask", req.Sort, "config", sortBeforeMerge)
		sort.Sort(ByName(req.Inputs))
	}

	filenames := make([]string, len(req.Inputs))
	if !converter.LeaveTempFiles {
		defer func() {
			for _, fn := range filenames {
				if fn != "" {
					_ = os.Remove(fn)
				}
			}
		}()
	}
	for i, f := range req.Inputs {
		if filenames[i], err = readerToFile(f.ReadCloser, f.Filename); err != nil {
			return nil, fmt.Errorf("error saving %q: %s", f.Filename, err)
		}
		select {
		case <-ctx.Done():
			return nil, ctx.Err()
		default:
		}
	}

	dst, err := tempFilename("pdfmerge-")
	if err != nil {
		return nil, err
	}
	logger.Info("msg", "PdfMerge", "dst", dst, "inputs", fmt.Sprintf("%s", filenames))
	if err := converter.PdfMerge(ctx, dst, filenames...); err != nil {
		logger.Warn("msg", "PdfMerge", "dst", dst, "inputs", filenames, "error", err)
		return nil, err
	}
	f, err := os.Open(dst)
	if err != nil {
		return nil, err
	}
	_ = os.Remove(dst)
	logger.Debug("msg", "PdfMerge OK", "dst", dst)
	return f, nil
}
Example #2
0
func mergePdf(outfn string, inpfn []string, sortFiles bool) error {
	if sortFiles {
		sort.Strings(inpfn)
	}
	return converter.PdfMerge(context.Background(), outfn, inpfn...)
}