chromedp中PrintToPDF函数的WithFooterTemplate方法的有效html是什么?

问题描述

我们试图将页码添加到我们打印的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。 有了该代码,结果看起来像这样:有一个页眉,因为我没有为此提供模板,但是即使提供了用于渲染页码的模板,页脚也为空

解决方法

找到了解决方案:

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)

我不明白为什么,但是可以。