Skip to content

Commit b674416

Browse files
harshilp24jnikhila
andauthored
fix: Generate PDF Dynamically (#2805)
## Description Provide a concise summary of the changes made in this pull request - ## Pull request type Check the appropriate box: - [ ] Review Fixes - [ ] Documentation Overhaul - [ ] Feature/Story - Link one or more Engineering Tickets * - [ ] A-Force - [ ] Error in documentation - [ ] Maintenance ## Documentation tickets Link to one or more documentation tickets: - ## Checklist From the below options, select the ones that are applicable: - [ ] Checked for Grammarly suggestions. - [ ] Adhered to the writing checklist. - [ ] Adhered to the media checklist. - [ ] Verified and updated cross-references or added redirect rules. - [ ] Tested the redirect rules on deploy preview. - [ ] Validated the modifications made to the content on the deploy preview. - [ ] Validated the CSS modifications on different screen sizes. --------- Co-authored-by: Nikhila Jain <99252011+jnikhila@users.noreply.github.com>
1 parent 6650711 commit b674416

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Generate PDF Dynamically
2+
3+
This page shows how to generate a PDF dynamically based on query data or user inputs and preview it in a Document Viewer widget.
4+
5+
1. In your Appsmith app, open the **Libraries** section from the sidebar, click the **+** icon, and add JavaScript library to generate the PDF.
6+
7+
<dd>
8+
9+
*Example:* Import the [jsPDF](https://raw.githack.com/MrRio/jsPDF/master/docs/index.html) library.
10+
11+
```javascript
12+
https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js
13+
```
14+
15+
To add tables and other UI components to the PDF, import the [jsPDF AutoTable](https://github.com/simonbengtsson/jsPDF-AutoTable) library *(an extension for `jsPDF`)*.
16+
17+
18+
<ZoomImage
19+
src="/img/jspdf-lib.png"
20+
alt=""
21+
caption=""
22+
/>
23+
24+
</dd>
25+
26+
2. To generate the PDF, get data from user inputs (widgets) or fetch it dynamically from a database or API using a query.
27+
28+
<dd>
29+
30+
*Example:* If you want to generate an offer letter, set up a form widget to capture the candidate’s information or create a query to retrieve details such as the candidate’s name, job title, start date, salary, and benefits.
31+
32+
</dd>
33+
34+
35+
3. Create a new **JSObject** from the *JS* tab to define the logic for generating the PDF using the JS library.
36+
37+
<dd>
38+
39+
*Example:* If you want to generate a offer letter based on the inputs provided by a user, you can create a JSObject to handle the logic and formatting of the PDF.
40+
41+
42+
```js
43+
export default {
44+
// Data object to store dynamic values
45+
data: {
46+
date: moment().format("Do MMM, YYYY"),
47+
firstName: firstName.text,
48+
lastName: lastName.text,
49+
email: email.text,
50+
jobTitle: jobTitle.text,
51+
salary: baseSalary.text,
52+
},
53+
54+
// Function to generate the offer letter PDF
55+
generateOfferPDF(data = this.data) {
56+
// Initialize a new PDF document
57+
const doc = new jspdf.jsPDF();
58+
59+
// Add the header
60+
doc.text("Offer Letter", 10, 10);
61+
doc.text(`Date: ${data.date}`, 10, 20);
62+
63+
// Add recipient details
64+
doc.text(`To: ${data.firstName} ${data.lastName}`, 10, 30);
65+
doc.text(`Email: ${data.email}`, 10, 40);
66+
67+
// Add the body of the offer letter
68+
doc.text(`Dear ${data.firstName},`, 10, 60);
69+
doc.text(`We are pleased to offer you the position of ${data.jobTitle}.`, 10, 70); // Job title information
70+
doc.text(`Your annual salary will be ${data.salary}.`, 10, 80); // Salary details
71+
72+
// Add acceptance section
73+
doc.text("If you accept this offer, please sign below:", 10, 100);
74+
doc.text("Signature: __________________________", 10, 110); // Signature placeholder
75+
doc.text("Date: __________________________", 10, 120);
76+
77+
// Return the PDF as a data URI string (can be used to preview/download the PDF)
78+
return doc.output("datauristring");
79+
},
80+
};
81+
```
82+
- The data object stores values from input fields, and the `generateOfferPDF` function uses these values to populate the PDF content.
83+
- The code dynamically generates an offer letter PDF by retrieving user inputs (name, email, job title, salary) and formatting them using `jspdf.jsPDF()`.
84+
- The final PDF is returned as a data URI, which can be displayed or downloaded.
85+
86+
</dd>
87+
88+
4. To preview the PDF, drag a Document Viewer widget and set its **Document link** property to:
89+
90+
<dd>
91+
92+
*Example:*
93+
94+
```javascript
95+
{{PDFUtils.generateOfferPDF()}}
96+
```
97+
98+
</dd>
99+
100+
101+
## See Also
102+
103+
- To send the generated PDF via email, connect to an SMTP datasource and create a Send Email query. For more information, see [How to Configure SMTP Datasource](/connect-data/how-to-guides/send-emails-using-the-SMTP-plugin#add-attachments).
104+
105+
- To download the PDF, use the [download()](/reference/appsmith-framework/widget-actions/download) function to save the file locally.
106+
107+

website/docs/connect-data/how-to-guides/send-emails-using-the-SMTP-plugin.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,27 @@ The provided function asynchronously fetches file data from a URL using an API r
178178

179179
</dd>
180180

181+
#### Using Base64 Data
182+
183+
<dd>
184+
185+
If you have a JSObject that returns the file data in Base64 format (e.g., a PDF or image), you can use it to send an attachment in your email. To do this, in the **Attachment(s)** field of the SMTP query, add the following code.
186+
187+
*Example*:
188+
189+
190+
```js
191+
[{
192+
"type": "application/pdf",
193+
"data": "{{PDFUtils.generateOfferPDF()}}",
194+
"name": "{{firstName.text}}.pdf",
195+
"dataFormat": "Base64"
196+
}
197+
]
198+
```
199+
200+
201+
</dd>
181202

182203

183204
## Setup email trigger

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ const sidebars = {
440440
'connect-data/how-to-guides/how-to-download-files-using-api',
441441
`core-concepts/writing-code/ui-actions`,
442442
'connect-data/how-to-guides/send-emails-using-the-SMTP-plugin',
443+
'build-apps/how-to-guides/generate-pdf-dynamically',
443444
`build-apps/how-to-guides/display-select-options-dynamically`,
444445
'build-apps/how-to-guides/add-remove-inputs-in-list',
445446
`build-apps/how-to-guides/navigate-between-pages`,

website/static/img/jspdf-guide.png

328 KB
Loading

website/static/img/jspdf-lib.png

308 KB
Loading
422 KB
Loading

0 commit comments

Comments
 (0)