xml2pdf Playground Source

Element reference

Every element and attribute xml2pdf understands. Unknown elements and attributes are errors, so a typo is never silently ignored.

Any attribute value can contain ${variables}, and most elements also accept the style attributes (font, size, color, align, …). Lengths are in points unless given a unit; see Units.

<document>

The root element. It must contain at least one <page>.

Children: <constants>, <resources>, <watermark>, <header> (at most one), <footer> (at most one), <page>, <for-each>.

AttributeValuesDefaultDescription
titletext–PDF metadata title, shown in viewer title bars.
authortext–PDF metadata author.
subjecttext–PDF metadata subject.
keywordstext–PDF metadata keywords.
page-sizeA3 A4 A5 Letter Legal Tabloid or WxHA4Default page size, e.g. 100mmx150mm. See Page setup.
orientationportrait landscapeportraitDefault orientation.
margin1–4 lengths0.5inDefault page margins (top right bottom left).
Style attributes: the base style for the whole document. The defaults are Helvetica, 11pt, black, left-aligned, line-height 1.2.
<document title="Invoice ${no}" author="Acme" page-size="A4" margin="20mm" font="helvetica" size="10">
  ...
</document>

<constants>

Declares named values that can be referenced as ${name}. It's allowed directly under <document>. Values from --data and --set override constants with the same name. See Variables.

<const>

AttributeValuesDefaultDescription
nameidentifierrequiredLetters, digits, _, - and ., starting with a letter or _. Must be unique.
valuetextelement textThe value. If it's omitted, the element's text content is used instead, which suits long values. It may reference other variables.
<constants>
  <const name="company" value="Acme Corp"/>
  <const name="disclaimer">All prices include VAT. ${company} reserves the right to change prices.</const>
</constants>

<resources>

Declares images and named styles that are referenced elsewhere by id. It's allowed directly under <document>. Resources can be used anywhere in the document, regardless of where they're declared.

<image> (resource)

AttributeValuesDefaultDescription
idnamerequiredThe name used by <img ref> and <watermark image>.
srcfile pathrequiredA PNG, JPEG or GIF file. Relative paths resolve against the XML file's directory (or --base-dir). A missing file is an error.

<style> (resource)

AttributeValuesDefaultDescription
idnamerequiredThe name used in style="...".
extendsstyle ids–One or more parent styles (space-separated) whose properties this style starts from. Cycles are errors.
Any style attributes. A style may also use style="..." to include other styles.
<resources>
  <image id="logo" src="images/logo.png"/>
  <style id="h1" size="20" bold="true" color="#1f4e9c" margin-bottom="8"/>
  <style id="h2" extends="h1" size="14"/>
</resources>

<page>

A page of content. Each <page> starts a new physical page, and its content continues onto more pages as needed, all with the same settings. See Layout & pagination.

Children: blocks (<text>, <img>, <table>, <spacer>, <page-break>), <for-each>, and loose inline text, which becomes a paragraph.

AttributeValuesDefaultDescription
page-sizeas on <document>document'sPage size for this page.
orientationportrait landscapedocument'sOrientation for this page.
margin1–4 lengthsdocument'sMargins for this page.
headertrue falsetrueShow the document header. With false, the space is given to the content.
footertrue falsetrueShow the document footer.
watermarktrue falsetrueShow watermarks.
Style attributes: inherited by all content on the page.
<page header="false" align="center">        <!-- cover page -->
  <spacer height="2in"/>
  <text size="28" bold="true">Annual Report</text>
</page>
<page orientation="landscape">...</page>

Content repeated at the top or bottom of pages, inside the margins. In their text, {page} and {pages} become the current page number and the total page count. See Headers & footers.

Children: the same as <page>. Absolutely positioned blocks inside them use page coordinates.

AttributeValuesDefaultDescription
heightlengthcontent heightFixed band height.
gaplength6Space between the band and the page content.
pagespage selectorallThe pages to show the band on. Its space is reserved on every page either way.
Style attributes, inherited from the document style.
<header pages="2-">
  <table widths="1*,1*" border="0" padding="0" size="8" color="gray">
    <row><cell>Quarterly report</cell><cell align="right">Q3 2026</cell></row>
  </table>
</header>
<footer><text align="center" size="8">Page {page} of {pages}</text></footer>

<watermark>

Text or an image drawn across pages, rotated and semi-transparent. You can declare any number of them. It needs exactly one of text (or text content), image or src. See Watermarks.

AttributeValuesDefaultDescription
texttext–Watermark text. It can also be given as the element's content, and may use {page} and {pages}.
imageimage id–An image resource.
srcfile path–An image file.
opacity0–10.15Transparency.
rotatedegrees45Counter-clockwise rotation around the center point.
layerfront backfrontDraw over or beneath the page content.
x, ylength or %page centerCenter point, measured from the page's top-left corner.
width, heightlength or %50% of page widthImage size. If only one is set, the aspect ratio is kept.
pagespage selectorallThe pages to draw on.
Style attributes for text watermarks. The defaults are size="60", bold="true" and a medium gray color.
<watermark text="DRAFT" size="100" color="#1f4e9c" opacity="0.12"/>
<watermark image="logo" width="60%" opacity="0.06" rotate="30" layer="back" pages="2-"/>

<text>

A paragraph. Whitespace is collapsed, lines wrap to the available width, and long words break at character boundaries. <p> is an alias.

Children: text and inline tags only.

AttributeValuesDefaultDescription
x, ylength or %–Absolute position of the paragraph's top-left corner. See absolute positioning.
widthlength or %available widthParagraph box width. In the flow it can only make the box narrower.
Style attributes, including align (left, center, right, justify), line-height, margin-top and margin-bottom.
<text style="body" align="justify" margin-bottom="8">
  Payment is due within <b>30 days</b>. Questions? Email <u>billing@example.com</u>.
</text>

Inline tags

These are allowed inside <text>, and anywhere loose inline text is allowed. They can be nested.

TagEffectAttributes
<b>, <strong>boldstyle attributes
<i>, <em>italicstyle attributes
<u>underlinestyle attributes
<span>no effect of its ownstyle attributes, e.g. <span color="red" size="14">
<br/>line breaknone

<img>

An image. <image> is an alias when used inside a page. It needs either ref or src. Images are never stretched beyond the available width or the content height; they're scaled down to fit instead.

AttributeValuesDefaultDescription
refimage id–An image declared in <resources>.
srcfile path–An image file (PNG, JPEG, GIF), relative to the XML file.
widthlength or %natural sizeDisplay width. If only width or only height is set, the aspect ratio is kept.
heightlength or %natural sizeDisplay height (a percentage is of the content height).
alignleft center rightinheritedHorizontal placement within the available width.
x, ylength or %–Absolute position of the top-left corner.
margin-top, margin-bottom and the other style attributes.

The natural size is the pixel size at 72 dpi, or at the DPI recorded in a PNG file.

<img ref="logo" width="45mm"/>
<img src="charts/q3.png" width="80%" align="center" margin-bottom="10"/>

<table>

A grid of rows and cells. It breaks across pages between rows, repeating header rows.

Children: <row> and <for-each> (generating rows).

AttributeValuesDefaultDescription
widthscomma-separated listequal columnsColumn widths. N* takes a share of the space left after fixed columns (* = 1*), a length is fixed, and % is a percentage of the table width. Example: widths="30mm,2*,1*,15%". Without it, the column count comes from the widest row.
widthlength or %100%Table width.
borderlength0.5Cell border line width. 0 means no borders.
border-colorcolorblackBorder color.
padding1–4 lengths4Cell padding (top right bottom left).
header-bgcolor–Background of header rows.
stripecolor–Background of every second body row.
repeat-headertrue falsetrueRepeat header rows at the top of each continuation page.
x, ylength or %–Absolute position (the table then doesn't paginate).
Style attributes, inherited by all cells.
<table widths="4*,1*,1.5*" border="0.5" border-color="#bbb" padding="5 6"
       header-bg="#1f4e9c" stripe="#f2f5fa" size="10">
  <row header="true" color="white">
    <cell>Description</cell><cell align="right">Qty</cell><cell align="right">Amount</cell>
  </row>
  <row><cell>Consulting</cell><cell align="right">12</cell><cell align="right">$1,800.00</cell></row>
  <row bold="true"><cell colspan="2" align="right">Total</cell><cell align="right">$1,800.00</cell></row>
</table>

<row>

Children: <cell> and <for-each>. A row with fewer cells than columns is padded with empty cells; a row with more is an error.

AttributeValuesDefaultDescription
headertrue falsefalseA header row: bold by default, uses header-bg, is kept with the next row, and repeats after page breaks.
heightlengthcontentMinimum row height.
bgcolor–Row background, overriding header-bg and stripe.
Style attributes, inherited by the row's cells.

<cell>

Children: inline text, or blocks (<text>, <img>, nested <table>, <spacer>), and <for-each>. Blocks inside cells can't use x/y.

AttributeValuesDefaultDescription
colspannumber ≥ 11Number of columns this cell spans.
valigntop middle bottomtopVertical alignment within the row.
bgcolor–Cell background, overriding the row's.
Style attributes, e.g. align="right" for numbers.
<cell valign="middle">
  <text bold="true">Globex Ltd.</text>
  <text size="8" color="gray">Customer since 2019</text>
</cell>

<spacer>

Vertical empty space. It's dropped if it would land at the top of a continuation page.

AttributeValuesDefaultDescription
heightlengthrequiredAmount of space.
<spacer height="10mm"/>

<page-break>

Forces the following content onto a new page. It has no attributes. A break on a page that has no content yet does nothing, so it never produces blank pages.

<text style="h1">Chapter 1</text> ...
<page-break/>
<text style="h1">Chapter 2</text> ...

<for-each>

Repeats its content once per element of an array. It can appear anywhere: as a child of <document> (to generate pages), <page>, <table> (rows), <row> (cells), <cell>, <text> (inline content), or another loop. See Loops.

AttributeValuesDefaultDescription
itemsvariable pathrequiredThe array to iterate: items, ${items}, order.lines or ${order.lines}. It must be an array, either from the JSON data or a Go slice.
varnameitemVariable holding the current element: ${item} or ${item.field}.
indexname–Variable holding the 0-based position.
numbername–Variable holding the 1-based position.
<for-each items="${items}" var="item" number="n">
  <row><cell>${n}</cell><cell>${item.name}</cell><cell align="right">${item.price}</cell></row>
</for-each>

Style attributes

These styling attributes are accepted on <document>, <page>, <header>, <footer>, <watermark>, all blocks, table rows and cells, inline tags, and <style> resources. See Styles for how they cascade.

AttributeValuesInheritedDescription
stylestyle ids–Named styles to apply, space-separated, left to right. The element's own attributes win.
fonthelvetica times courier zapfdingbatsyesFont family. Aliases: arial/sans/sans-serif, serif/times-roman/times new roman, monospace/courier new. See Fonts.
sizelengthyesFont size. font-size is an alias.
boldtrue falseyesBold weight.
italictrue falseyesItalic style.
underlinetrue falseyesUnderline.
colorcoloryesText color.
alignleft center right justifyyesHorizontal alignment of text and images.
line-heightnumberyesLine height as a multiple of the font size (default 1.2).
margin-toplengthnoSpace above a block.
margin-bottomlengthnoSpace below a block.
bgcolornoBackground color (table rows and cells).

Booleans accept true/false, yes/no and 1/0.