package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"path"

	"github.com/jung-kurt/gofpdf"
)

// Hello 测试
func Hello() {
	pdf := gofpdf.New("P", "mm", "A4", "")
	pdf.AddPage()
	pdf.SetFont("Arial", "B", 16)
	pdf.Cell(40, 10, "Hello, world")
	err := pdf.OutputFileAndClose("hello.pdf")
	if err != nil {
		log.Fatalf("output failed,err:%s", err)
	}
}

// SingleImg2Pdf 单张png图片转pdf
func SingleImg2Pdf(pathStr string) {
	pdf := gofpdf.New("P", "mm", "A4", "")
	pdf.AddPage()
	pdf.SetFont("Arial", "", 11)
	pdf.Image(pathStr, 10, 10, pdf.GetPageSizeStr("A4").Wd-20, 0, false, "", 0, "")
	// pdf.Text(50, 20, "logo.png")
	fileNameAll := path.Base(pathStr)
	fileSuffix := path.Ext(pathStr)
	fileStr := fmt.Sprintf("./pdfs/%s.pdf", fileNameAll[0:len(fileNameAll)-len(fileSuffix)])
	err := pdf.OutputFileAndClose(fileStr)
	if err != nil {
		log.Fatalf("output failed,err:%s", err)
	}
}

func main() {
	// Hello()
	// SingleImg2Pdf("./images/22.jpg")
	dirName := "./images"
	files, err := ioutil.ReadDir(dirName)
	if err != nil {
		panic(err)
	}
	for _, file := range files {
		fileNameAll := fmt.Sprintf("%s/%s", dirName, file.Name())
		// fmt.Println(fileNameAll)
		SingleImg2Pdf(fileNameAll)
	}
}

  说明: 找出当前目录下的images里面的所有图片,逐个转成PDF文件