PDF templates
What it does
A PDF document prints with one of two layouts. Standard prints a fixed page with your text above and below the answers. HTML prints a page you write yourself: the whole body, the page footer and extra CSS. This page covers the HTML layout, what WebForms lets through to the PDF, and what code can and can't change in the built-in layouts. The document editor is in the form's settings, under Print: click a document's name to open it.
Layouts and slots
The layouts and their editable areas (slots) are declared in etc/webforms_pdf_templates.xml of the WebForms
module. A saved document records the layout (template_ref) and the contract version (template_contract) it was
written against.
| Layout | template_ref | Contract | Slot code | Admin label | Slot type |
|---|---|---|---|---|---|
| Standard | standard | 1 | intro | Text Before Answers | rich |
outro | Text After Answers | rich | |||
footer_note | Footer Note | rich | |||
| HTML | html | 0 | body | Document | html |
footer | Page Footer | html | |||
css | Extra CSS | css |
Switching the layout keeps the text of the other layout's slots on the document, so switching back loses nothing.
Before you start
- A form with at least one PDF document. See PDF documents.
- An admin role with Edit Form, under Manage Forms in the role's resources. It covers changing Layout, writing the slots and Open PDF. See Roles and permissions.
- For pictures: the image files in
pub/media/webforms/pdf/on the server.
Steps
-
Go to Content → WebForms → Manage Forms and click the form's name.
-
Click Print, then click the name of the document, for example Office Copy.
-
In Layout, select HTML. The Document, Page Footer and Extra CSS boxes appear. A document whose Document box is empty starts from the built-in starter page (the module's
view/base/templates/pdf/starter/html-v1.html), which looks like the Standard layout. -
Write the page in Document. Use merge tags for submission data, and
{{var all_fields}}where the answers table goes:<table class="pdf-brandbar"><tr><td class="pdf-brand-name">{{var store:name}}</td><td class="pdf-doc-type">Fabric Sample Request</td></tr></table><h2 class="ns-title">{{var form:name}} {{var reference}}</h2><p>Requested by {{var field:your_name}} on {{var submission:date}}.</p><p>Fabric: <strong>{{var field:fabric}}</strong></p>{{if field:message}}<p class="ns-note">Note from the customer: {{var field:message}}</p>{{else}}<p class="ns-note">No note.</p>{{/if}}{{var all_fields}}<p style="page-break-before: always">Packing slip for the sample team.</p> -
If you want your own footer on every page, write it in Page Footer. Leave it empty for the built-in footer (store name, date, reference and page numbers):
<p class="ns-foot">Northstar Supply · Page {{var PAGENO}} of {{var nbpg}}</p> -
If you want your own styles, write plain CSS rules in Extra CSS. They are added after the built-in stylesheet:
.ns-title { color: #1f4e79; font-size: 18pt; margin-bottom: 4mm; }.ns-note { color: #555555; font-style: italic; }.ns-foot { color: #777777; } -
Check the preview on the right. It refreshes as you type and fills the merge tags with sample values.
-
Click Open PDF to render the real file over sample answers. The arrow beside it offers Open as Customer and Open as Staff. Nothing is saved.
-
Click Save.
Merge tags
A PDF has no event behind it, so the names are a fixed list (MageMe\WebForms\Model\Pdf\Render\PdfVariableVocabulary),
plus the variables that installed add-ons contribute.
| Tag | Prints | Where it may stand |
|---|---|---|
{{var form:name}} | The form's name | Text, in Document and Page Footer |
{{var reference}} | The submission's reference number | Text |
{{var submission:id}}, {{var submission:date}} | The submission's id and date | Text |
{{var store:name}} | The store view's name | Text |
{{var customer:name}}, {{var customer:email}} | The submitter | Text |
{{var status:label}}, {{var approval:label}} | The status and the approval state when the PDF is made | Text |
{{var field:<code>}} | One answer, by field code, or by field id when the field has no code. Empty for a field with Hide in Submissions switched on, so {{if}} on it takes the {{else}} branch | Text |
{{var all_fields}} | The answers table for the audience (staff or customer), without the fields that have Hide in Submissions switched on | Text of Document only |
{{var store:logo}} | The store logo | Only as an image src |
{{var PAGENO}}, {{var nbpg}} | This page's number and the page count | Text of Page Footer only |
Besides var, a slot understands {{if name}}…{{else}}…{{/if}}, {{depend name}}…{{/depend}} and
{{trans "text"}}. Nothing else of Magento's template language runs in a PDF: no {{block}}, {{layout}} or
{{template}}. A merge tag may not stand inside an attribute, except {{var store:logo}} in src.
What HTML prints
The slots go through an allowlist sanitizer (MageMe\WebForms\Model\Pdf\Render\PdfHtmlSanitizer) before
rendering, and the editor checks them in the preview and on Save.
| Tags kept | p, br, span, div, strong, b, em, i, u, s, small, sup, sub, h1–h6, ul, ol, li, a, blockquote, pre, code, hr, img, table, thead, tbody, tfoot, caption, tr, th, td |
| Attributes kept | On any tag: style, class, dir, lang, align. Also href and title on a; src, alt, width and height on img; width, cellpadding, cellspacing, border and align on table; width, align, valign, colspan and rowspan on td and th |
| Refused: the document can't be previewed or saved | script, style, iframe, object, embed, svg, form, meta, link, base, applet, video, audio |
| Removed when it prints | Event handlers and every other attribute not listed, links other than http, https and mailto, images from anywhere but pub/media/webforms/pdf/ |
Inline style | Only text, colour, font, border, padding, margin, width, height, alignment, list and page-break properties, with plain values: a colour, a length, a keyword or a quoted font name. No url() and no other functions |
What CSS prints
Extra CSS keeps plain rules only (MageMe\WebForms\Model\Pdf\Render\CssAllowlist): a selector made of
element names, classes, ids, attribute selectors and combinators, and the declarations an inline style allows.
These are removed: @import, @media and every other at-rule, nested rules, url() and page-break
declarations. Save stores the cleaned stylesheet, so after you reopen the document the box shows what was
kept. To force a page break, put page-break-before: always in an inline style on the element.
Images
Upload image files (PNG, JPEG or GIF, up to 2 MB and 16 megapixels each) to pub/media/webforms/pdf/. In src,
name the file by its path under the media folder, for example webforms/pdf/northstar-logo.png, or by its full
media URL. WebForms reads the file from disk. The PDF renderer makes no network requests, so any other image is
dropped.
What you should see
The example above, opened with Open PDF, gives a two-page file named after the form and the date.
Page 1 has the store name and Fabric Sample Request in the brand bar, the title in 18 pt dark blue, the
requester's name and fabric, the note in italic grey, then the answers table. Page 2 holds only the packing-slip
line, because of its inline page break. Each page ends with "Northstar Supply · Page 1 of 2" (and "2 of 2") in
grey. A version of the CSS with an @import, an @media rule, a url() background and a page break inside the
.ns-note rule prints the same: those parts are dropped and the rest applies.
When a slot breaks a rule, the preview shows a message that names the slot instead of the page, and Open PDF returns the same message. Save doesn't store the document and shows no message, so check the preview before you save, and reopen the document to confirm. For example:
The PDF document is not valid: pdf_document.contents[0].slots.body may not contain <script>.
Limits
- Size limits. Each slot and the finished file are checked against the limits in PDF Documents settings. Two more limits have no setting: a slot may nest 20 levels deep and force 40 page breaks.
- No scripts and no remote content. The renderer is mPDF with an HTTP client that refuses every request. Web fonts, remote images and remote stylesheets never load. Fonts come from mPDF's own set.
- Pictures that visitors upload don't print. Only files in
pub/media/webforms/pdf/and the drawing of a Signature field do. - No layout of your own in code. The registry in
webforms_pdf_templates.xmlmerges across modules, so a module can declare anothertemplateid with its own slots. The editor's Layout still offers only Standard and HTML, and the renderer (MageMe\WebForms\Model\Pdf\Render\PdfComposer) gives onlyhtmlits own page: every other id prints the Standard layout and uses only itsintro,outroandfooter_noteslots. - The Standard layout comes from module files. Its page is
view/base/templates/pdf/standard.phtml(withpdf/brandbar.phtml), and both layouts use the stylesheetview/base/web/css/pdf/style.less. These resolve in the theme of the request that makes the PDF: the admin theme for Print on a submission and for the editor's preview and Open PDF, the store's frontend theme for the customer's download and for email attachments. A copy in your storefront theme therefore doesn't reach the staff PDF. To restyle a document, use Extra CSS in the HTML layout.
Troubleshooting
- "… may not contain <script>." (or another tag): remove the tag. The list is under What HTML prints.
- "{{var PAGENO}} is a page counter and belongs in the page footer." Move the page counters to Page Footer. The same mistake also reports ""PAGENO" is not available to this notification."
- ""customer:phone" is not available to this notification." The name isn't in the
list of merge tags. Use
{{var field:<code>}}for an answer. - "{{var form:name}} may not be used inside the "title" attribute; write it in the text." Move the tag out of the attribute.
- A picture is missing from the PDF. The file isn't in
pub/media/webforms/pdf/, orsrcpoints somewhere else. - A CSS rule has no effect. Reopen the document: Extra CSS shows what was kept.
What's next
- PDF documents: who gets which document, conditions and the Standard layout.
- Attachments and PDFs in emails: send the document with a notification.
- REST: PDF documents: write the same slots through the API.