chromedp 中 PrintToPDF 函数的 WithFooterTemplate 方法的有效 html 是什么?
What is a valid html for WithFooterTemplate method of PrintToPDF function in chromedp?
我们已尝试将页码添加到打印的 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)
我不明白为什么,但它有效。
我们已尝试将页码添加到打印的 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)
我不明白为什么,但它有效。