ecload/pkg/ecload/pdf.go

47 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 (
"io/ioutil"
"os"
"path"
"strings"
"github.com/jung-kurt/gofpdf"
)
// Concatenate all jpg files in a directory to a single pdf.
func ImgDirToPdf(dir string, output string) error {
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
pdf := gofpdf.New("P", "mm", "", "")
opt := gofpdf.ImageOptions{ImageType: "jpg", ReadDpi: true}
for _, f := range files {
if strings.HasSuffix(f.Name(), ".jpg") {
filepath := path.Join(dir, f.Name())
reader, err := os.Open(filepath)
if err != nil {
return err
}
pdf.RegisterImageOptionsReader(f.Name(), opt, reader)
info := pdf.RegisterImageOptions(f.Name(), opt)
pdf.AddPageFormat("P", gofpdf.SizeType{ Wd: info.Width(), Ht: info.Height() })
pdf.ImageOptions(f.Name(), 0, 0, info.Width(), info.Height(), false, opt, 0, "")
reader.Close()
}
}
return pdf.OutputFileAndClose(output)
}