ecload/pkg/ecload/pdf.go

52 lines
1.1 KiB
Go

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package ecload
import (
"image"
_ "image/jpeg"
"io/ioutil"
"os"
"path"
"github.com/jung-kurt/gofpdf"
)
// Concatenate all jpg files in a directory to a single pdf.
func ImgsToPdf(dir string, output string) error {
pdf := gofpdf.New("P", "mm", "", "")
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, f := range files {
filepath := path.Join(dir, f.Name())
reader, err := os.Open(filepath)
if err != nil {
return err
}
img, _, err := image.DecodeConfig(reader)
if err != nil {
//return err
continue
}
pdf.AddPageFormat("P", gofpdf.SizeType{Wd: float64(img.Width), Ht: float64(img.Height)})
opt := gofpdf.ImageOptions{ImageType: "jpg", ReadDpi: true}
pdf.RegisterImageOptionsReader(f.Name(), opt, reader)
pdf.ImageOptions(f.Name(), 0, 0, 0, 0, false, opt, 0, "")
reader.Close()
}
return pdf.OutputFileAndClose(output)
}