SebastiaanKlippert / go-wkhtmltopdf

Golang commandline wrapper for wkhtmltopdf
MIT License
1.05k stars 144 forks source link

If the PDF generated from my HTML file has a cover and the generated directory is right in front of the cover, how can I make the generated directory on the second page? I tried to generate the cover as a separate HTML and the content as another HTML, but there was an issue with the PDF as both the table of contents and content were missing, #123

Open yiqiui opened 4 months ago

yiqiui commented 4 months ago

If the PDF generated from my HTML file has a cover and the generated directory is right in front of the cover, how can I make the generated directory on the second page? I tried to generate the cover as a separate HTML and the content as another HTML, but there was an issue with the PDF as both the table of contents and content were missing,

//// 配置报告封面
//coverFile, err := templates.Templates.ReadFile("configure_report_cover.html")
//if err != nil {
//  panic(err)
//}
//// 添加页面
//coverPage := wkhtmltopdf.NewPageReader(bytes.NewReader(coverFile))
//coverPage.HeaderLeft.Set("Clinflash IRT  配置报告 ")
//coverPage.FooterLeft.Set("项目编号(项目环境)")
//coverPage.FooterRight.Set("[page]/[toPage]")
//coverPage.FooterFontSize.Set(10)
//coverPage.Zoom.Set(1)
//pdfg.AddPage(coverPage)

// 添加目录页面
pdfg.TOC.Include = true
pdfg.TOC.TocHeaderText.Set("目录")

// 配置报告内容
contentFile, err := templates.Templates.ReadFile("configure_report_content.html")
if err != nil {
    panic(err)
}
// 添加页面
contentPage := wkhtmltopdf.NewPageReader(bytes.NewReader(contentFile))

// 设置页眉和页脚选项
contentPage.HeaderLeft.Set("Clinflash IRT  配置报告 ")
contentPage.FooterLeft.Set("项目编号(项目环境)")
contentPage.FooterRight.Set("[page]/[toPage]")
contentPage.FooterFontSize.Set(10)
contentPage.Zoom.Set(1)
pdfg.AddPage(contentPage)

// 生成 PDF 文件
err = pdfg.Create()
if err != nil {
    panic(err)
}
SebastiaanKlippert commented 3 months ago

Hi, thanks for you comment.

I don't fully understand what you are trying to do, I believe there are two questions here. One is that you have a problem with you TOC not appearing correctly if you also have a cover page? And I also see you have trouble combining two input pages.

The second question I can answer, it looks like you are trying to add multiple pages as "Reader" in this example code. If you uncomment the first section, the second AddPage call will indeed not work. See https://github.com/SebastiaanKlippert/go-wkhtmltopdf/issues/54 for more information. There is also a workaround there that might work for you. Basically you need to make sure you have combined your pages into one HTML document first, before you add it to wkhtmltopdf.

I am not sure why your TOC is not working though, maybe you can share an example output of that.