How to Conditional PDF Content

Not every document should show the same content to every recipient. Conditional content lets you show or hide sections, swap languages, apply discount blocks, toggle compliance disclaimers, or personalize layouts — all from a single template driven by your data. SublimePDF's Handlebars-based templates support conditionals that keep your template library lean instead of maintaining dozens of near-identical templates.

Follow the step-by-step instructions below, then use the free tool directly — no registration or download required.

Open Tool →

How to Conditional PDF Content — Step by Step

1

Use basic if/else blocks

Wrap conditional sections in {{#if condition}}...{{else}}...{{/if}}. The condition is truthy when the value exists, is non-empty, and isn't false or 0. Example: {{#if isPremiumCustomer}}<div class='premium-badge'>Premium</div>{{/if}}.

2

Check for specific values with helpers

Use the built-in {{#ifEquals}} helper for value comparisons: {{#ifEquals paymentStatus 'overdue'}}<span class='overdue-alert'>Payment Overdue</span>{{/ifEquals}}. This is cleaner than nested if-else chains for multi-value checks.

3

Toggle entire page sections

Wrap large sections (appendices, terms and conditions, optional product pages) in conditional blocks. Use CSS with break-before: page inside the conditional block to start that section on a new page only when it's included.

4

Apply conditional CSS classes

Dynamically assign CSS classes based on data: <tr class="{{#if isOverdue}}row-overdue{{else}}row-normal{{/if}}">. This lets you change colors, fonts, and layouts without duplicating HTML structure.

5

Nest conditions for complex logic

Conditions can nest: {{#if hasDiscount}}{{#if isAnnualPlan}}Annual discount applied{{else}}Monthly discount applied{{/if}}{{/if}}. Keep nesting to two levels maximum — deeper nesting makes templates hard to maintain.

Pro Tips

  • 💡 Use {{#unless}} as a cleaner alternative to {{#if}} with negation — {{#unless isPaid}}Payment Required{{/unless}} reads better than an if-else with an empty if block.
  • 💡 For multi-language templates, use a top-level 'locale' variable with nested conditionals: {{#ifEquals locale 'fr'}}Facture{{else}}Invoice{{/ifEquals}}. Alternatively, pass already-translated strings in your data object for cleaner templates.
  • 💡 Conditional blocks that hide large sections (pages of content) still reduce the PDF page count, so your document dynamically adjusts length based on the data — no manual pagination needed.
  • 💡 Test every conditional branch by rendering the template with different data payloads — it's easy to miss a broken {{else}} path that's rarely triggered in production.

Privacy & Security

All processing happens directly in your browser. Your files are never uploaded to any server — they remain on your device throughout the entire process. SublimePDF uses WebAssembly technology for fast, secure, client-side processing.

Works Everywhere

This tool works on any modern browser — Chrome, Firefox, Safari, or Edge — on desktop, tablet, or mobile. No software to install. PDF is an open ISO standard supported by all major platforms.

How to Conditional PDF Content — FAQ

Can I show different content to different users with one template?
Yes. Pass user-specific data (role, plan, locale, permissions) in the data object and use {{#if}} and {{#ifEquals}} blocks to toggle sections. One template can produce wildly different PDFs depending on the input data.
How do I conditionally include an entire page?
Wrap the page content in {{#if showAppendix}}<div style='break-before: page;'>...appendix content...</div>{{/if}}. When showAppendix is false, the div isn't rendered and no extra page appears.
What counts as 'falsy' in Handlebars conditionals?
False, undefined, null, empty string (''), 0, and empty arrays ([]) are all falsy. Everything else is truthy, including the string 'false' — convert string booleans to actual booleans in your backend before passing them to the template.
Can I compare two variables to each other?
The built-in {{#ifEquals}} helper compares a variable to a literal value. For comparing two variables, register a custom helper via the 'helpers' parameter in your API request: { "helpers": { "ifVarEquals": "function(a, b, options) { return a === b ? options.fn(this) : options.inverse(this); }" } }.

Ready to get started?

Use SublimePDF's free tools right now.

Open Tool