[BUG] Header and Footer Templates don't work when generating a PDF
See original GitHub issueContext:
- Playwright Version: 1.22.0
- Operating System: Windows 11
- .NET version: .NET 6
- Browser: Chromium
Code Snippet
using System.Threading.Tasks;
using Microsoft.Playwright;
class Program
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions()
{
Headless = true
});
string url = "https://en.wikipedia.org/wiki/Microsoft";
var page = await browser.NewPageAsync();
await page.GotoAsync(url, new PageGotoOptions()
{
WaitUntil = WaitUntilState.NetworkIdle
});
var pdfOptions = new PagePdfOptions()
{
DisplayHeaderFooter = true,
HeaderTemplate = "<span style=\"font-size: 20px;color:#000000;\">HEADER</span>",
FooterTemplate = "<span style=\"font-size: 20px;color:#000000;\">FOOTER</span>",
PrintBackground = true,
Path = "file.pdf"
};
await page.PdfAsync(pdfOptions);
}
}
Describe the bug
The header and footer are not visible in the generated document. I’ve tried including HTML tags in the template, and all kinds of style teqniques, but I have never been able to get the header and footer to display for any webpage.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7
Top Results From Across the Web
Issue about header and footer - Google Cloud Community
Everything works fine, but I have a problem with header and footer. ... I may advise not using header and footer with PDF...
Read more >Puppeteer Header and Footer not displayed on first page
According to the Puppeteer Documentation: page.pdf(options). options <Object> Options object which might have the following properties:.
Read more >Headers disappear in PDF - Microsoft Community
Here, headers and footers are retained when converting a Word document to PDF via File | Save As, as long as headers and...
Read more >Fix Word documents missing header and footer areas - YouTube
Has Word stopped displaying the white space between the end of one page and the start of the next? In this video, I...
Read more >Creating Header and Footer Templates for Word in ... - YouTube
If you need to reuse your headers and footers from one document on others, save them to your Building Blocks Organizer!
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
If it can help anyone, here are my findings about the header and footer template. Some of it is apparently undocumented.
I used the following documentation: https://playwright.dev/python/docs/api/class-page#page-pdf
margin
argument, or setprefer_css_page_size=True
with margins defined in the@page
CSS rule (I did the latter).<head><style></style></head>
. SVG images can be used by directly including the SVG code. Images can be used by using base64 encoding.@page
. You have to set the margins in the CSS included in the templates itself. I found that to center the header/footer properly, you can specify for examplemargin-left: 0.5in;
andwidth: calc(100% - 1in);
, which should work when your document’s body already has 0.5 inch margins left and right.So, I would say, header and footer do work when generating PDF, but the documentation misses out on a lot of stuff and they are not easy to get to work properly.
Here is how I went about the problem. I used the python API. Also, I used the Jinja project to template the header and footer templates.
For example, something along the lines of:
header_template.html
footer_template.html
and the CSS – defines footer left, center or right elements:
PDF creation:
@DaveNatalieTripArc Unfortunately, I don’t know the answer to that question, but I suspect it’s not possible in the current state. It seems like it’s either with or without for all pages.