Skip to content
Page Size and Orientation

Page Size and Orientation

The page size decides how the HTML reflows, so it is a layout choice rather than a scaling one. Ask for A5 and the same content spreads over more, narrower pages; it does not shrink to fit.

if err := doc.SetPageSize(sizes.A5); err != nil {
    return err
}
doc.SetLandscapeOrientation()

SetPageSize returns an error for a value outside the enum, which is the only way these calls fail. SetLandscapeOrientation has no counterpart: portrait is the default and there is no SetPortraitOrientation to switch back, so build the document the way you want it rather than toggling.

Named sizes or explicit dimensions

ApproachCallNotes
ISO nameSetPageSize(sizes.A4)A0 to A10, B0 to B10, and sizes.Letter.
ExplicitSetPageWidth, SetPageHeightTakes any sizes.Length, so a non-standard sheet is possible.

Mixing the two does not do what the call order suggests. The server applies the named size after the explicit width and height and overwrites them, so SetPageSize wins either way:

doc.SetPageWidth(sizes.Millimeter(100))   // discarded
doc.SetPageHeight(sizes.Millimeter(100))  // discarded
doc.SetPageSize(sizes.A5)                 // this is what you get

Pick one. Use explicit dimensions only for a sheet the enum does not name, such as a label or a receipt.

sizes.Undefined is the zero value and means “send nothing”, which leaves the default in place.

The default is Letter

With no page calls at all you get 612 by 792 points, US Letter. A4 is not the default even where the machine’s locale is metric, and nothing reports the choice, so set it explicitly if the output has to be A4.

Limitations

None of this applies on the c.Draw path. Drawing the document into a creator.Creator renders it at the creator’s own page dimensions, and worse, calling SetPageSize first flips the document into absolute positioning, which pins the rendered block to the top-left corner of the creator’s page. If you need control over the sheet, take the pages with GetPdfPages or use Document.WriteToFile. See PDF output.

Explicit dimensions round to whole units on the way to the server, because lengths are serialized with no decimal places. sizes.Millimeter(150.6) is sent as 151mm.

CSS @page rules in the markup do not override what you set here. The size comes from the print parameters, not from the stylesheet.

Run the example

directory_pages.go converts the data directory on A5 in landscape, takes the rendered pages with GetPdfPages, and adds them to a creator one at a time. The content is the same page the asset directory guide uses; the smaller sheet is why it now needs two pages instead of one.

git clone https://github.com/unidoc/unihtml-examples.git
cd unihtml-examples/directory
go run directory_pages.go localhost:8080

If this is your first time using UniHTML, follow the getting started guide to create an API key and set up your development environment.

View the full source

Sample output

First A5 landscape page

Second A5 landscape page

Last updated on