SebastiaanKlippert / go-wkhtmltopdf

Golang commandline wrapper for wkhtmltopdf
MIT License
1.06k stars 146 forks source link

Cant generate two html pages in one pdfGenerator #91

Closed rosehsu47 closed 2 years ago

rosehsu47 commented 2 years ago

I used pdfGenerator.AddPage() to add 2 different pages both generated from html (p.1 & p.3), but the second html page (p.3) of the result file is always empty.

This is my code.


func main() {
    pdfGenerator := DefaultPdfGenerator()

    log.Println("Page1")
    html := `<html><div style="color: #f00;">Hi<h1>yoyoyo</h1></div></html>`
    pdfGenerator.AddPage(NewPage(html))

    log.Println("Page2")
    page := wkhtmltopdf.NewPage("https://godoc.org/github.com/SebastiaanKlippert/go-wkhtmltopdf")
    pdfGenerator.AddPage(page)

    log.Println("Page3")
    pdfGenerator.AddPage(NewPage("<html>Hi</html>"))

    err := pdfGenerator.Create()
    if err != nil {
        log.Println("Create: ", err)
    }

    fileName := "test123.pdf"
    err = pdfGenerator.WriteFile(fileName)
    if err != nil {
        log.Println("WriteFile: ", err)
    }

    log.Println("Done")
}

functions

func DefaultPdfGenerator() *wkhtmltopdf.PDFGenerator {

    pdfGenerator, err := wkhtmltopdf.NewPDFGenerator()

    if err != nil {
        log.Warn("NewPDFGenerator: ", err)
    }

    pdfGenerator.Dpi.Set(600)
    pdfGenerator.NoCollate.Set(false)
    pdfGenerator.PageSize.Set(wkhtmltopdf.PageSizeA4)
    // pdfGenerator.MarginBottom.Set(20)

    return pdfGenerator
}

func NewPage(html string) wkhtmltopdf.PageProvider {
    page := wkhtmltopdf.NewPageReader(strings.NewReader(html))

    page.FooterRight.Set("[page]")
    page.FooterFontSize.Set(8)

    return page
}
SebastiaanKlippert commented 2 years ago

Hi, see https://github.com/SebastiaanKlippert/go-wkhtmltopdf/issues/74 https://github.com/SebastiaanKlippert/go-wkhtmltopdf/issues/62 and others.

You can only add one page from memory because of how wkhtmltopdf is designed, it uses stdin to pass the content.

In most cases you won't need it, you can just join the HTML pages before you pass them in, see https://github.com/SebastiaanKlippert/go-wkhtmltopdf/issues/54 for an example.

rosehsu47 commented 2 years ago

Thank you for the reply! I will go check those issues 👍