@page rules
EggPdf reads CSS @page rules embedded in your HTML to configure the output page.
Place the rule inside a <style> block in the document <head>:
@page {
size: A4; /* page size */
margin: 2cm; /* all margins */
}
Any CSS you put inside @page { } applies to every page of the document.
EggPdf uses this as the single source of truth for page geometry — there are no separate
API options for size or margins.
Page sizes
The following named sizes are supported. The default when no @page rule is present is A4.
| Name | Dimensions |
|---|---|
A3 | 297 × 420 mm |
A4 | 210 × 297 mm |
A5 | 148 × 210 mm |
Letter | 8.5 × 11 in |
Legal | 8.5 × 14 in |
Tabloid | 11 × 17 in |
Landscape orientation
Add the landscape keyword after the size name to rotate the page:
@page {
size: A4 landscape;
margin: 1.5cm;
}
Use portrait to make the orientation explicit, though it is the default when no keyword is given.
Custom dimensions
Any CSS length unit works for size. Provide width then height:
@page {
size: 200mm 120mm; /* width height */
margin: 10mm;
}Supported units: px, mm, cm, in, pt.
Margins
All standard CSS margin shorthands are accepted inside @page:
/* Uniform — 2cm on every side */
@page { margin: 2cm; }
/* Vertical | Horizontal */
@page { margin: 2cm 1.5cm; }
/* Individual sides */
@page {
margin-top: 25mm;
margin-bottom: 25mm;
margin-left: 20mm;
margin-right: 20mm;
}Loading external resources
When rendering from an HTML file that references external stylesheets or images via relative
paths, pass the directory containing the file as basePath. EggPdf will resolve
<link rel="stylesheet"> and <img src="..."> relative to that
directory:
using EggPdf;
string html = File.ReadAllText("invoice.html");
string baseDir = Path.GetDirectoryName(Path.GetFullPath("invoice.html"))!;
byte[] pdf = HtmlToPdf.Render(html, basePath: baseDir);
File.WriteAllBytes("invoice.pdf", pdf);basePath, only inline <style> blocks and
data URIs
are available. External href and src paths will be skipped silently.
Supported image formats when loading from disk: PNG, JPEG, GIF, and BMP. Images can also be embedded directly as data URIs without needing a basePath.