forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.1 KiB
56 lines
1.1 KiB
3 months ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"pdfcpu/pkg/pdfcpu"
|
||
|
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func mergePDFs(pdfDir string) {
|
||
|
// 检查 pdfs 目录是否存在
|
||
|
_, err := os.Stat(pdfDir)
|
||
|
if err != nil {
|
||
|
// 如果不存在,则创建该目录
|
||
|
err = os.Mkdir(pdfDir, 0755)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建 pdfs 目录失败:", err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 打开 pdfs 目录
|
||
|
files, err := os.ReadDir(pdfDir)
|
||
|
if err != nil {
|
||
|
fmt.Println("读取 pdfs 目录失败:", err)
|
||
|
return
|
||
|
}
|
||
|
// 关闭 pdfs 目录
|
||
|
err = files.Close()
|
||
|
if err != nil {
|
||
|
fmt.Println("关闭 pdfs 目录失败:", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 获取当前时间
|
||
|
t := time.Now()
|
||
|
// 格式化时间为 yyyyMMddhhmmss
|
||
|
formattedTime := t.Format("20060102150405")
|
||
|
|
||
|
// 合并 PDF 文件
|
||
|
mergedPDFPath := fmt.Sprintf("%s/merged-%s.pdf", pdfDir, formattedTime)
|
||
|
err = pdfcpu.CombineFiles(pdfDir, mergedPDFPath)
|
||
|
if err != nil {
|
||
|
fmt.Println("合并 PDF 文件失败:", err)
|
||
|
return
|
||
|
}
|
||
|
fmt.Println("PDF 文件合并成功")
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
// 指定要合并 PDF 文件的目录路径
|
||
|
pdfDir := "path/to/your/pdfs/directory"
|
||
|
mergePDFs(pdfDir)
|
||
|
}
|