package main
import (
"github.com/signintech/gopdf"
"github.com/phpdave11/gofpdi"
)
func main() {
pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}})
// the pdf files for merging
var pdfFiles = []string{"nameOfPdfFile1", "nameOfPdfFile2"}
importer := gofpdi.NewImporter()
// looping through pdf files that will be merged
for _, pdfFile := range pdfFiles {
// setting current pdf file for getting its page count
importer.SetSourceFile(pdfFile)
// For getting how many pages pdf file has
totalPagesOfPdf := importer.GetNumPages()
// this loop for adding empty pages its amount same as pdf's page amount
// (because we need add page to import that page and added page amount should be
// same as the imported pdf's page amount)
// then importing individual page, then configuring the imported individual page
// if a page successfully imported and configured then loop var will be increased by 1
// to import next page
for i := 0; i < totalPagesOfPdf; i++ {
pdf.AddPage()
importedPage := pdf.ImportPage(pdfFile, i + 1, "/MediaBox")
pdf.UseImportedTemplate(importedPage, 0, 0, 595.28, 841.89)
}
}
// generating pdf
pdf.WritePdf("merged.pdf")
}
1条答案
按热度按时间fnvucqvd1#
我们可以使用GoPdf.ImportPage方法合并任何数量的PDF文件,并从另一个库中获得一个小的帮助,但我不知道如何裁剪合并的PDF以使其大小合适。但目前版本的Gopdf的GoPdf.ImportPage方法不稳定,因此它可以在从PDF导入页面时给予,其页数非常多。合并的PDF文件的大小不小。示例当我将1 MB的PDF文件与1.6 MB的PDF文件合并时,我得到了一个PDF文件,其大小约为26 MB。合并后的PDF文件丢失了一些功能,如可单击对象(例如:链接)不再是可点击的。