This guide walks you through creating a comprehensive PDF report that combines content from all tabs in your ToolJet application. This is particularly valuable when building applications like Invoice Generators, Employee Records, Multi-section Reports etc.
First, add a button that will trigger the entire print workflow. You can place this button anywhere in your application, typically near the top of your tabs or in a toolbar.
Add an **On click** event with **Set variable** action. Set the key as `lastSelectedTab` and value as `{{components.tabs.currentTab}}`. This saves the user's current tab selection so we can return them to it after printing.
Add another **On click** event with **Run query** action and select the `viewTabs` query. This kicks off the tab iteration process. We'll create this query in the next step.
The `viewTabs` query manages the iteration process that cycles through each tab. It uses a variable called `tabIndex` to track which tab we're currently processing.
Add a **Query Success** event with **Control component** action. Select the `tabs` component, choose **Set current tab** action, and set the Id to `{{variables.tabIndex}}`. In the **Run only if** field, enter `{{parseInt(variables.tabIndex) < 4}}`. This event switches the visible tab to match the `tabIndex` we just set, and the condition ensures we only switch tabs while we're still within the tab range.
Add another **Query Success** event with **Run query** action. Select the `getTabsHTML` query, set **Debounce** to `100` milliseconds, and in the **Run only if** field, enter `{{parseInt(variables.tabIndex) < 4}}`. After switching to the tab, we need to give it a moment to render before capturing its content. The 100ms debounce ensures the tab is fully rendered. We'll create the `getTabsHTML` query in Step 3.
Add a third **Query Success** event with **Run query** action. Select the `printPDF` query and in the **Run only if** field, enter `{{parseInt(variables.tabIndex) === 4}}`. This only runs after we've finished iterating through all tabs (when `tabIndex` equals 4), triggering the PDF generation. We'll create the `printPDF` query in Step 4.
The `getTabsHTML` query captures the HTML content of the currently visible tab and stores it in an array. Each time it runs, it adds another tab's content to the collection.
Add a **Query Success** event with **Run query** action and select the `viewTabs` query. After capturing the current tab's HTML, this triggers `viewTabs` again to increment the index and process the next tab.
The `printPDF` query takes all the captured HTML from the `tabsHtml` array and generates a printable document. It opens a new browser window, injects the combined HTML along with all the application's styles, and triggers the print dialog.
This query opens a new browser window for the print preview and copies all CSS styles from your application to ensure the PDF looks correct. It sets the page orientation to landscape (you can change this to portrait if needed), writes all the captured tab HTML into the new window, and triggers the browser's print dialog.
Add a third **Query Success** event with **Control component** action. Select the `tabs1` component, choose **Set current tab** action, and set the Id to `{{variables.lastSelectedTab}}`. This returns the user to whichever tab they were viewing before clicking the print button.
You've successfully implemented a multi-tab PDF printing feature. Users can now generate comprehensive reports that include all tab content with a single click.