我们已经尝试将页码添加到打印的html页面。但是我们没有成功。现在,代码看起来像这样。
chromedp.ActionFunc(func(ctx context.Context) error {
var err error
buf, _, err = page.PrintToPDF().
WithFooterTemplate("<span class=pageNumber></span>").
WithDisplayHeaderFooter(true).
Do(ctx)
return err
}),
下面是该方法的源代码。在这里找到的https://github.com/chromedp/cdproto/blob/master/page/page.go#L812
// WithHeaderTemplate HTML template for the print header. Should be valid
// HTML markup with following classes used to inject printing values into them:
// - date: formatted print date - title: document title - url: document location
// - pageNumber: current page number - totalPages: total pages in the document
// For example, <span class=title></span> would generate span containing the
// title.
func (p PrintToPDFParams) WithHeaderTemplate(headerTemplate string) *PrintToPDFParams {
p.HeaderTemplate = headerTemplate
return &p
}
由于某些原因,我们找不到任何有效的html在这里注入。使用该代码,结果看起来是这样的:有一个页眉,因为我没有为它提供模板,但是页脚是空的,即使我提供了一个模板来呈现页码
发布于 2020-10-06 11:53:28
找到了解决方案:
buf, _, err = page.PrintToPDF().
WithDisplayHeaderFooter(true).
WithHeaderTemplate(`<span></span>`).
WithFooterTemplate(`<span style="font-size: 10px"><span class="pageNumber"></span>/<span class="totalPages"></span></span>`).
Do(ctx)
我不明白为什么,但它是有效的。
https://stackoverflow.com/questions/64223010
复制