Resources / Article

NetSuite advanced PDF templates with FreeMarker

Fix common FreeMarker issues in NetSuite Advanced PDF templates: IF/ELSE, dates, numbers, custom fields, page breaks, images, footers, GST invoices.

A NetSuite advanced PDF template is an HTML document with FreeMarker code inside it, and FreeMarker fills the placeholders with record data when the form prints. You write the conditions and loops yourself, using the field IDs that SuiteScript uses.

What FreeMarker does inside a NetSuite advanced PDF template

FreeMarker generates text from a template plus the data behind it, and NetSuite uses that library in advanced templates (Using FreeMarker to Include NetSuite Data in Advanced Templates). You place a value on the page with an interpolation, a FreeMarker expression that prints a record value in the output, written like ${record.entity}. A scriptable template refers to records, fields and sublists by the identifiers SuiteScript also uses, and the Records Browser gives each identifier in its Internal ID column (FreeMarker Syntax).

Scriptable templates combine HTML with FreeMarker code, and that code supports conditions, loops, lists, variables and expressions such as string operations and arithmetic calculations (HTML and FreeMarker for Scriptable Templates). The FreeMarker build running in your account is listed on the Third-Party Products Used in Advanced Printing page (Using FreeMarker to Include NetSuite Data in Advanced Templates). That version varies by account, so check a directive in the template preview before you depend on it in production.

From a NetSuite record to a printed advanced PDFA record supplies its fields by the identifiers SuiteScript uses. An advanced template holds HTML with FreeMarker interpolations, conditions and loops. FreeMarker fills the placeholders when the form prints, giving the PDF. A footer template holds its own content, with page numbers from the BFO tags pagenumber and totalpages.Record: fields, sublists and records, named by theidentifiers SuiteScript usesAdvanced template: HTML with FreeMarker code. Aninterpolation like ${record.entity} places a value;conditions, loops and expressions shape itBodyFreeMarker fills theplaceholders when theform printsFooterOwn footer template,content inside div tags.Page numbers from BFOtags pagenumber andtotalpagesPrinted PDF
How a record becomes a printed advanced PDF: the template holds HTML and FreeMarker, and the footer sits in its own template.

How do I write IF/ELSE conditions?

Write the condition as a standard FreeMarker directive, since conditions are one of the constructs scriptable templates support (HTML and FreeMarker for Scriptable Templates). A typical block picks between two tax registration fields:

<#if record.entity.country == "IN">
  <p>GSTIN: ${record.entity.custentity_gstin}</p>
<#else>
  <p>Tax registration: ${record.entity.vatregnumber}</p>
</#if>

Every ID above is a placeholder. Copy the real ones from your own account before you save the template.

How do I find the field ID for a custom field?

Turn on Show Internal IDs at Home > Set Preferences, on the General subtab in the Defaults area (FreeMarker Syntax). Click a field label in the interface and a popup shows the help text together with the field ID. The Records Browser lists the same IDs in its Internal ID column. FreeMarker code accepts camel case or lower case.

For a field that sits on a transaction line, loop the item list:

<#list record.item as item>
  <tr><td>${item.custcol_hsn_code}</td></tr>
</#list>

Lists and loops are among the constructs scriptable templates support (HTML and FreeMarker for Scriptable Templates).

How do I format dates and numbers?

FreeMarker expressions include string operations and arithmetic calculations, so you reshape a value where it prints (HTML and FreeMarker for Scriptable Templates). Build the printed figure in the template and leave the stored field untouched. Keep the arithmetic simple and check the preview, because the digits a customer sees depend on the locale and currency settings on the transaction.

How do I limit the characters a field prints?

Trim the string inside the interpolation with a FreeMarker string operation (HTML and FreeMarker for Scriptable Templates). A description field that runs long on a packing slip is the usual case. Do the trimming in the template, so the full text stays on the record for searches and reports.

How do I add page breaks and images?

The WYSIWYG toolbar in the template editor adds fields, images and tables, and it also adds printing elements like page breaks, page numbers, headers and footers (WYSIWYG Editing in the Template Editor). The Image and Table buttons open a popup where you set properties for the new element, and the Styles toolbar carries options for styling and alignment. Elements in an advanced PDF/HTML template cannot be absolutely positioned. When you need exact placement, use Transaction Layouts instead of an advanced template.

How do I keep a footer at the bottom of every page?

Build a footer template and put its content inside <div></div> tags (Creating the Dunning PDF Footer Template). You can print company information and subsidiary information in the footer, since both records are available there. Page numbers come from the BFO tags pagenumber and totalpages, which apply to PDF template types and are rendered by BFO.

How do I print a different layout for GST invoices?

Dunning letter PDF templates are XML files you upload to the file cabinet: a body template, plus an optional header and an optional footer, with sample files in SuiteBundles > Bundle 392827 > Templates > PDF Templates (Creating Dunning Letter PDF Templates). One header file can serve several template records. A footer template can carry company and subsidiary information (Creating the Dunning PDF Footer Template). Branch between layouts with a FreeMarker condition on the subsidiary field of the transaction, and keep the tax registration fields on the subsidiary record so the printed GSTIN matches the entity that issued the invoice. For the subsidiary structure behind India GST details, see NetSuite subsidiaries for India GSTINs.

Example

A customer buys goods worth ₹1,50,000. GST is 18%, split evenly between CGST at 9% and SGST at 9%, so the invoice total is ₹1,77,000. The template derives the split from the taxable amount:

<#assign taxable = record.amount>
<#assign cgst = taxable * 0.09>
<#assign sgst = taxable * 0.09>
<p>Taxable value: ${taxable}</p>
<p>CGST 9%: ${cgst}</p>
<p>SGST 9%: ${sgst}</p>
<p>Total: ${taxable + cgst + sgst}</p>

Arithmetic calculations are part of the FreeMarker expressions scriptable templates accept (HTML and FreeMarker for Scriptable Templates). Your tax rates, field IDs and rounding rules come from your own setup.

Trade-offs

A template split across separate files adds uploads to manage, and one header file can be shared by several template records (Creating Dunning Letter PDF Templates). Elements in an advanced PDF/HTML template cannot be absolutely positioned, so pixel-level placement needs Transaction Layouts (WYSIWYG Editing in the Template Editor). BFO tags for page numbers and total pages apply to PDF template types (Creating the Dunning PDF Footer Template). Where a figure needs a specific shape, the work sits in the template and you carry it through every layout that prints that record type.

Common mistakes

FAQ

Can I limit how many characters print from a field?
String operations are available in scriptable templates (HTML and FreeMarker for Scriptable Templates).

Where do I find the field ID for a custom field?
The Records Browser lists it in the Internal ID column, and Show Internal IDs in your preferences makes it visible in the interface (FreeMarker Syntax).

How do I add page numbers to a footer?
Use the BFO tags pagenumber and totalpages inside the footer template (Creating the Dunning PDF Footer Template).

Does every template need its own header and footer file?
No. One header file can serve several template records (Creating Dunning Letter PDF Templates).

mark. is the AI ERP expert for implementation consultants. Try it free.

Sources

  1. docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/bridgehead_3785741842.html
  2. docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_1556902584.html
  3. docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4047162299.html
  4. docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4417694364.html
  5. docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4454197641.html
  6. docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/subsect_163732171457.html

Have mark. check this against the client's own files

mark. reads the client's files and sets each point against them, with the source beside it.

Book a working session

Twenty minutes on one client folder. Your 21-day trial opens on the call.

Start your free trial

Three questions, and Mark opens an account for you on NetSuite.

Which ERP do your clients run?

Your account opens within one working day.