Example #1
0
// PdfToImage converts PDF to image using PdfToImageGm if available and the result is OK, then PdfToImageCairo.
func PdfToImage(w io.Writer, r io.Reader, contentType, size string) error {
	src := temp.NewMemorySlurper("PdfToImage-src-")
	defer src.Close()
	dst := temp.NewMemorySlurper("PdfToImage-dst-")
	defer dst.Close()

	var err error
	if err = PdfToImageCairo(dst, io.TeeReader(r, src), contentType, size); err == nil {
		_, err = io.Copy(w, dst)
		return err
	}
	logger.Error("msg", "PdfToImageCairo", "error", err)
	return PdfToImageGm(w, io.MultiReader(src, r), contentType, size)
}
Example #2
0
func runOne(fn string) ([]opener, error) {
	tmp := temp.NewMemorySlurper(filepath.Base(fn))
	defer func() { _ = tmp.Close() }()

	var errBuf bytes.Buffer
	cmd := exec.Command("./agostle", "mail2pdfzip", "--outimg=image/png", "--split=true", "-")
	cmd.Dir = parentDir
	cmd.Stderr = &errBuf
	cmd.Stdout = tmp
	fh, err := os.Open(fn)
	if err != nil {
		return nil, err
	}
	defer func() { _ = fh.Close() }()
	if strings.HasSuffix(fn, ".gz") {
		gz, err := gzip.NewReader(fh)
		if err != nil {
			return nil, err
		}
		defer func() { _ = gz.Close() }()
		cmd.Stdin = gz
	} else {
		cmd.Stdin = fh
	}
	Log.Info("running", "cmd", cmd.Args)
	if err = cmd.Run(); err != nil {
		Log.Error("Run", "cmd", cmd, "error", err, "stack", getStackTrace())
		return nil, fmt.Errorf("error running %s: %v {{%s}}", cmd.Args, err, errBuf.String())
	}
	Log.Info("finished OK", "cmd", cmd.Args)

	openers, err := getPNGs(tmp)
	if err != nil {
		return openers, err
	}
	return filterBlank(openers)
}