首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在chromedp中,PrintToPDF函数的WithFooterTemplate方法的有效html是什么?

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

Stack Overflow用户
提问于 2020-10-06 17:29:41
回答 1查看 116关注 0票数 0

我们已经尝试将页码添加到打印的html页面。但是我们没有成功。现在,代码看起来像这样。

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
// 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在这里注入。使用该代码,结果看起来是这样的:有一个页眉,因为我没有为它提供模板,但是页脚是空的,即使我提供了一个模板来呈现页码

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-06 19:53:28

找到了解决方案:

代码语言:javascript
代码运行次数: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)

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64223010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档