Page Setup
Page geometry is set on the unihtml.Document before it is converted, and it is
passed through to Chrome’s print parameters rather than applied afterwards. The
practical consequence is that the page size decides how the HTML reflows: a
narrower sheet means more pages, not a shrunken copy of the same layout.
if err := doc.SetPageSize(sizes.A4); err != nil {
return err
}
doc.SetLandscapeOrientation()
doc.SetMargins(20, 20, 20, 20)Timing belongs here too. The server captures the page once the DOM has loaded, which is early enough that anything drawn by JavaScript is usually not there yet.
Which setters take effect, and when
The page geometry below applies on GetPdfPages and Document.WriteToFile, and is
ignored on the c.Draw path, where the creator’s own page governs.
SetPos is the exception. It sets where the rendered block lands rather than the page
geometry, so it is the one call in this table that does change c.Draw output. Everything
else in the table is discarded there.
| Call | Unit | Flips positioning to absolute |
|---|---|---|
SetPageSize(sizes.A4) | ISO page name | Yes |
SetPageWidth, SetPageHeight | sizes.Length | Yes |
SetMargins(l, r, t, b) | Points, as float64 | Yes |
SetMarginLeft and its three siblings | sizes.Length | No |
SetLandscapeOrientation() | - | No |
SetPos(x, y) | Points | Yes |
That last column is the trap. A document starts in relative positioning, and while
it is relative all four margins are overwritten with 1mm just before the request
goes out. So a program that calls only SetMarginLeft gets 1mm, not the value it
asked for, and no error. Something that flips positioning to absolute has to come
first.
SetPageSize also wins over SetPageWidth and SetPageHeight regardless of the
order you call them in, because the server applies the named size last and it
overwrites the explicit dimensions.
Defaults
| Default | |
|---|---|
| Page size | Letter, 612 by 792 points |
| Orientation | Portrait |
| Margins, positioning absolute | 10mm on all four sides |
| Margins, positioning relative | 1mm on all four sides |
| Wait before capture | None, capture at DOM loaded |
| Conversion timeout | 15 seconds |
Letter rather than A4 catches people out, and nothing warns you: the document just comes back 8.5 by 11 inches.
Where to look
| Guide | Covers |
|---|---|
| Page Size and Orientation | Named ISO sizes, explicit dimensions, landscape, and how the page count follows. |
| Margins | The four setters, the unit types, and two ways a margin is silently discarded. |
| Wait for Rendering | Waiting for a selector or a fixed duration so script-drawn content is captured. |