Scripts are not evalutated in header and footer HTML templates for PDFs.
See original GitHub issueSteps to reproduce
- Puppeteer version: 1.1.1
- Platform / OS version: Windows 10, version 1709, build 16299.248
- Node.js version: v7.9.0
What steps will reproduce the problem?
First, I setup a basic test page like this:
const browser = await puppeteer.launch({ignoreHTTPSErrors: true})
const page = await browser.newPage()
await page.setContent(`
<html>
<head>
<title>Test Page</title>
<style>
body{
background: yellow;
min-height: 6000px;
}
</style>
</head>
<body>
I'm an HTML page.
</body>
</html>
`)
const pdf = await page.pdf({
format: 'Letter',
printBackground: true,
headerTemplate: headerTemplate,
displayHeaderFooter: true,
margin: {
top: '1in',
right: '0in',
bottom: '1in',
left: '0in'
}
})
await browser.close()
The headerTemplate
looks like this:
const headerTemplate = `
<div style="font-size: 10px; display: flex; flex-direction: row; justify-content: space-between; width: 100%" id='template'>
<div class='pageNumber' id='num' style="font-size: 10px;">Page number</div>
<div class='date' style="font-size: 10px;"></div>
<div class='title' style="font-size: 10px;"></div>
<div class='url' style="font-size: 10px;"></div>
<div class='totalPages' style="font-size: 10px;"></div>
<script>
var pageNum = document.getElementById("num");
pageNum.remove()
var template = document.getElementById("template")
template.style.background = 'red';
</script>
</div>`
What is the expected result? The docs don’t mention whether or not script tags are supported in the header and footer templates. I would expect then that the code above would render a header with a red background and remove the page number node. (The code above was just for testing purposes).
What happens instead? The script appears not to evaluate. Instead the resulting PDF has no background, and a page number div.
If evaluating scripts in these templates is not supported, might I suggest updating the PDF section of the docs to state as much. Thanks for all the hard work the Puppeteer team has put into this project!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:57
- Comments:27
Top Results From Across the Web
Puppeteer Header and Footer not displayed on first page
NOTE headerTemplate and footerTemplate markup have the following limitations: Script tags inside templates are not evaluated.
Read more >Document Template: Dynamic Header and Footers
Solution: The HTML documents can have CSS scripts in them. We can use it to our advantage to introduce the dynamic header and...
Read more >Add headers, footers, and Bates numbering to PDFs
Follow these steps to add headers, footers, and Bates numbering to PDFs using Acrobat. They can include a date, page numbers, title, author, ......
Read more >Phantom pdf recipe - jsreport
The Phantom-pdf recipe is capable of rendering any HTML and JavaScript you ... The header and footer are evaluated as if they were...
Read more >Pandoc User's Guide
Produce output with an appropriate header and footer (e.g. a standalone HTML, LaTeX, TEI, or RTF file, not a fragment). This option is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
this is chromium source code print_header_footer_template_page.html
https://cs.chromium.org/chromium/src/components/printing/resources/print_header_footer_template_page.html
headerTemplate and footerTemplate content was used innerHTML method to render , so it can’t execute JavaScript, but we can use other method to execute javascript, we can use under code:
the result is js modify test color to green.
I hate so many trick hacks!I need the headerrTemplate/ footerTemplate can support function setting.