Skip to content
Page Setup

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.

CallUnitFlips positioning to absolute
SetPageSize(sizes.A4)ISO page nameYes
SetPageWidth, SetPageHeightsizes.LengthYes
SetMargins(l, r, t, b)Points, as float64Yes
SetMarginLeft and its three siblingssizes.LengthNo
SetLandscapeOrientation()-No
SetPos(x, y)PointsYes

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 sizeLetter, 612 by 792 points
OrientationPortrait
Margins, positioning absolute10mm on all four sides
Margins, positioning relative1mm on all four sides
Wait before captureNone, capture at DOM loaded
Conversion timeout15 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

GuideCovers
Page Size and OrientationNamed ISO sizes, explicit dimensions, landscape, and how the page count follows.
MarginsThe four setters, the unit types, and two ways a margin is silently discarded.
Wait for RenderingWaiting for a selector or a fixed duration so script-drawn content is captured.
Last updated on