SebastiaanKlippert / go-wkhtmltopdf

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

Adding multiple pages #62

Closed deelawn closed 3 years ago

deelawn commented 3 years ago

When I call PDFGenerator.AddPage multiple times to add pages, only the first page is generated with content, followed by blank pages. Am I misunderstanding how this works? I'd expect it to generate all the pages with content. Looks like this code in wkhtmltopdf.go defines that behavior:

for _, page := range pdfg.pages {
    if page.Reader() != nil {
    cmd.Stdin = page.Reader()
    break
    }
}

If this is working as expected my next question would be, why support adding multiple pages? I admittedly don't know very much about wkhtmltopdf. If the behavior I'm looking for is possible, I can help implement it.

SebastiaanKlippert commented 3 years ago

Hi,

If you are calling AddPage multiple times with a PageReader object, then yes, this is by design and expected. The reason is that you can pass only one input stream to standard input, which pagereader is doing as described in the readme and docs.

why support adding multiple pages?

Because you can call AddPage multiple times with a Page object, which is just an input flag to wkhtmltopdf, a reference to a HTML file, on disk or on the web. Where PageReader is a direct input stream to wkhtmltopdf, so you don't need to access your disk or the internet.

If the behavior I'm looking for is possible, I can help implement it.

There are workarounds, but you can do that in the calling code. Thanks for the help though.

So to add multiple input HTML docs, you can either write them to disk first or you can just join them in the Go code first, see https://github.com/SebastiaanKlippert/go-wkhtmltopdf/issues/54 for an example of how to implement the last option using a buffer (or just add them together as strings).

deelawn commented 3 years ago

Thanks for the prompt response -- it makes more sense after I've looked into the wkhtmltopdf docs. I was able to come up with a workaround that suited my needs.