A service quote form where the PDF content changes based on what services the customer selects. Only chosen services appear in the quote, with dynamic filenames that include the customer name and date.
Real-world use case: A customer selects house cleaning and window washing but not carpet cleaning. The PDF shows ONLY those two services with prices - no empty sections for unselected services.
if/else in the PDF builderEvery code snippet below is fully editable! Try checking/unchecking different services in the form and watch the PDF update with only the selected items.
Use the play button to run different snippets, or the refresh button to restore the original example.
Let's make the filename dynamic by including today's date. This helps organize quotes chronologically.
new Date() to get the current date. The toISOString().split('T')[0] pattern gives you a clean YYYY-MM-DD format perfect for filenames. Make filenames even more useful by including the customer name. We need to sanitize it to remove special characters.
Now the magic! Use if statements to only add services that were actually selected. No more empty sections!
form.checkbox('cleaning')?.value() to get whether a checkbox is checked. Wrap each service in an if statement - it only appears in the PDF if selected. if checks if ANY service is selected before creating the section. This prevents an empty "Selected Services" section. Add up the prices of selected services to show a total estimate. Store prices in an object for easy reference.
prices object so they're easy to update. Loop through selected services to calculate the total, then format it as currency. The "Special Instructions" field is optional. Only show it in the PDF if the customer actually wrote something.
?.trim() to remove whitespace, then check if (notes.length > 0) before creating the section. This prevents empty sections from appearing. Time to polish! Add a formatted quote date, service count, footer with validity period, and terms & conditions.
toLocaleDateString() with options to format the date nicely for display: "January 7, 2025" is much friendlier than "2025-01-07". .filter(Boolean).length on an array of checkbox values. Display this alongside the total for transparency. You've mastered dynamic content! In the next tutorial, you'll learn how to create multi-page documents with page breaks and organized sections.
Continue to Multi-Page DocumentsKey concepts learned:
new Date() and customer dataif statementscheckbox('id')?.value()