0

我们尝试将页码添加到我们打印的 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 注入。使用该代码,结果如下所示:有一个页眉,因为我没有为此提供模板,但是即使我提供了一个模板来呈现页码,页脚也是空的

4

1 回答 1

0

找到了解决方案:

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)

我不明白为什么,但它有效。

于 2020-10-06T11:53:27.970 回答