Cannot add header to pdf
See original GitHub issueI want to generate a pdf file from HTML created dynamically in the code. I succeeded in adding a footer to all the pages, but cannot add header. I don’t know where’s the problem since the method is the same. Currently, I am developing it on a Console App. Here’s the code
public class PdfGenerator
{
private IConverter _converter;
public PdfGenerator()
{
this._converter = new SynchronizedConverter(new PdfTools());
}
public void GeneratePdfFile(string html)
{
var globalSettings = new GlobalSettings
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
DocumentTitle = "PDF Report",
Margins = { Top = 10 },
Out = @"..\..\..\employee-report.pdf"
};
var objectSettings = new ObjectSettings
{
PagesCount = true,
HtmlContent = html,
WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = @"..\..\..\htmlTemplate.css" },
HeaderSettings = { FontName = "Arial", FontSize = 9, Line = false, HtmUrl = @"..\..\..\header.html"},
FooterSettings = { FontName = "Arial", FontSize = 9, Line = false, HtmUrl = @"..\..\..\footer.html", Left = "" }
};
var pdf = new HtmlToPdfDocument()
{
GlobalSettings = globalSettings,
Objects = { objectSettings }
};
_converter.Convert(pdf);
}
}
Header html:
<div class="head">
<div class="left">
<img id="logo" src="logo.jpg">
</div>
<div class="right">
<h3><span>Professional Experience</span></h3>
</div>
</div>
Footer html:
<div class="footer">
<div id="footer-left">
Text<br>
Text
</div>
<div>
Text<br>
Text
</div>
</div>
I tried to render the footer as a header, just for testing purposes. When I tried to open the pdf in browser it returned an error: Microsoft Edge: “Couldn’t open PDF” Google Chrome: Just opens the pdf editor without any pages.
Adobe reader opens the document but the header is still missing.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
Error When Adding Header/Footer
Something in your PDF file is corrupt, and unfortunately, unless you remove the page that causes this problem (if you can actually remove...
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 >Can't add footer into PDF | Adobe Acrobat
Go to Document Properties and the program that created it should be listed there. Is the Add Header or Footer menu item greyed...
Read more >How to Add Header and Footer to PDF
When you uploaded the PDF file, select the "Edit" option in the top menu bar and choose "Header & Footer" from the submenu....
Read more >4 Easy Ways to Add Header and Footer to PDF
Step 2: Add Header and Footer to PDF Online Navigate to the "Header and Footer" option in the tool and select it to...
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 FreeTop 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
Top GitHub Comments
As I understand, it’s artificial restriction, but you can’t depend on in-memory html header, you should depend on existing, stored on disc, html file that will be a header:
I’ll appreciate if someone will show more reasonable example, but in my case dinkToPdf generating header far above the visible canvas, so I add negative spacing to make it visible. This part of code did the trick for me:
HeaderSettings = { ... Spacing = -40 },
Furthermore, header appears on top of document body, problem for future to configure it in reasonable way. And html itself looks like this:I had the same issue. Setting a specific height for the header (in the html) solved it for me.