|
| 1 | +--- |
| 2 | +title: Generate a PDF from Twenty |
| 3 | +description: Create a workflow to generate and attach a PDF (such as a quote) to a record. |
| 4 | +--- |
| 5 | + |
| 6 | +Automatically generate or fetch a PDF and attach it to a record in Twenty. This is commonly used to create quotes, invoices, or reports that are linked to Companies, Opportunities, or other objects. |
| 7 | + |
| 8 | +## نظرة عامة |
| 9 | + |
| 10 | +This workflow uses a **Manual Trigger** so users can generate a PDF on demand for any selected record. A **Serverless Function** handles: |
| 11 | + |
| 12 | +1. Downloading the PDF from a URL (from a PDF generation service) |
| 13 | +2. Uploading the file to Twenty |
| 14 | +3. Creating an Attachment linked to the record |
| 15 | + |
| 16 | +## المتطلبات الأساسية |
| 17 | + |
| 18 | +Before setting up the workflow: |
| 19 | + |
| 20 | +1. **Create an API Key**: Go to **Settings → APIs** and create a new API key. You'll need this token for the serverless function. |
| 21 | +2. **Set up a PDF generation service** (optional): If you want to dynamically generate PDFs (e.g., quotes), use a service like Carbone, PDFMonkey, or DocuSeal to create the PDF and get a download URL. |
| 22 | + |
| 23 | +## إعداد خطوة بخطوة |
| 24 | + |
| 25 | +### الخطوة 1: تهيئة المشغّل |
| 26 | + |
| 27 | +1. Go to **Workflows** and create a new workflow |
| 28 | +2. Select **Manual Trigger** |
| 29 | +3. Choose the object you want to attach PDFs to (e.g., **Company** or **Opportunity**) |
| 30 | + |
| 31 | +<Tip> |
| 32 | + With a Manual Trigger, users can run this workflow using a button that appears on the top right once a record is selected, to generate and attach a PDF. |
| 33 | +</Tip> |
| 34 | + |
| 35 | +### Step 2: Add a Serverless Function |
| 36 | + |
| 37 | +1. Add a **Serverless Function** action |
| 38 | +2. Create a new function with the code below |
| 39 | +3. Configure the input parameters |
| 40 | + |
| 41 | +#### Input Parameters |
| 42 | + |
| 43 | +| Parameter | القيمة | |
| 44 | +| ----------- | ----------------------- | |
| 45 | +| `companyId` | `{{trigger.object.id}}` | |
| 46 | + |
| 47 | +<Note> |
| 48 | + If attaching to a different object (Person, Opportunity, etc.), rename the parameter accordingly (e.g., `personId`, `opportunityId`) and update the serverless function. |
| 49 | +</Note> |
| 50 | + |
| 51 | +#### Serverless Function Code |
| 52 | + |
| 53 | +```typescript |
| 54 | +export const main = async ( |
| 55 | + params: { companyId: string }, |
| 56 | +) => { |
| 57 | + const { companyId } = params; |
| 58 | + |
| 59 | + // Replace with your Twenty GraphQL endpoint |
| 60 | + // Cloud: https://api.twenty.com/graphql |
| 61 | + // Self-hosted: https://your-domain.com/graphql |
| 62 | + const graphqlEndpoint = 'https://api.twenty.com/graphql'; |
| 63 | + |
| 64 | + // Replace with your API key from Settings → APIs |
| 65 | + const authToken = 'YOUR_API_KEY'; |
| 66 | + |
| 67 | + // Replace with your PDF URL |
| 68 | + // This could be from a PDF generation service or a static URL |
| 69 | + const pdfUrl = 'https://your-pdf-service.com/generated-quote.pdf'; |
| 70 | + const filename = 'quote.pdf'; |
| 71 | + |
| 72 | + // Step 1: Download the PDF file |
| 73 | + const pdfResponse = await fetch(pdfUrl); |
| 74 | + |
| 75 | + if (!pdfResponse.ok) { |
| 76 | + throw new Error(`Failed to download PDF: ${pdfResponse.status}`); |
| 77 | + } |
| 78 | + |
| 79 | + const pdfBlob = await pdfResponse.blob(); |
| 80 | + const pdfFile = new File([pdfBlob], filename, { type: 'application/pdf' }); |
| 81 | + |
| 82 | + // Step 2: Upload the file via GraphQL multipart upload |
| 83 | + const uploadMutation = ` |
| 84 | + mutation UploadFile($file: Upload!, $fileFolder: FileFolder) { |
| 85 | + uploadFile(file: $file, fileFolder: $fileFolder) { |
| 86 | + path |
| 87 | + } |
| 88 | + } |
| 89 | + `; |
| 90 | + |
| 91 | + const uploadForm = new FormData(); |
| 92 | + uploadForm.append('operations', JSON.stringify({ |
| 93 | + query: uploadMutation, |
| 94 | + variables: { file: null, fileFolder: 'Attachment' }, |
| 95 | + })); |
| 96 | + uploadForm.append('map', JSON.stringify({ '0': ['variables.file'] })); |
| 97 | + uploadForm.append('0', pdfFile); |
| 98 | + |
| 99 | + const uploadResponse = await fetch(graphqlEndpoint, { |
| 100 | + method: 'POST', |
| 101 | + headers: { Authorization: `Bearer ${authToken}` }, |
| 102 | + body: uploadForm, |
| 103 | + }); |
| 104 | + |
| 105 | + const uploadResult = await uploadResponse.json(); |
| 106 | + |
| 107 | + if (uploadResult.errors?.length) { |
| 108 | + throw new Error(`Upload failed: ${uploadResult.errors[0].message}`); |
| 109 | + } |
| 110 | + |
| 111 | + const filePath = uploadResult.data?.uploadFile?.path; |
| 112 | + |
| 113 | + if (!filePath) { |
| 114 | + throw new Error('No file path returned from upload'); |
| 115 | + } |
| 116 | + |
| 117 | + // Step 3: Create the attachment linked to the company |
| 118 | + const attachmentMutation = ` |
| 119 | + mutation CreateAttachment($data: AttachmentCreateInput!) { |
| 120 | + createAttachment(data: $data) { |
| 121 | + id |
| 122 | + name |
| 123 | + } |
| 124 | + } |
| 125 | + `; |
| 126 | + |
| 127 | + const attachmentResponse = await fetch(graphqlEndpoint, { |
| 128 | + method: 'POST', |
| 129 | + headers: { |
| 130 | + Authorization: `Bearer ${authToken}`, |
| 131 | + 'Content-Type': 'application/json', |
| 132 | + }, |
| 133 | + body: JSON.stringify({ |
| 134 | + query: attachmentMutation, |
| 135 | + variables: { |
| 136 | + data: { |
| 137 | + name: filename, |
| 138 | + fullPath: filePath, |
| 139 | + companyId, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }), |
| 143 | + }); |
| 144 | + |
| 145 | + const attachmentResult = await attachmentResponse.json(); |
| 146 | + |
| 147 | + if (attachmentResult.errors?.length) { |
| 148 | + throw new Error(`Attachment creation failed: ${attachmentResult.errors[0].message}`); |
| 149 | + } |
| 150 | + |
| 151 | + return attachmentResult.data?.createAttachment; |
| 152 | +}; |
| 153 | +``` |
| 154 | + |
| 155 | +### Step 3: Customize for Your Use Case |
| 156 | + |
| 157 | +#### To attach to a different object |
| 158 | + |
| 159 | +Replace `companyId` with the appropriate field: |
| 160 | + |
| 161 | +| كائن | اسم الحقل | |
| 162 | +| ---------- | -------------------- | |
| 163 | +| الشركة | `companyId` | |
| 164 | +| شخص | `personId` | |
| 165 | +| الفرصة | `opportunityId` | |
| 166 | +| كائن مخصّص | `yourCustomObjectId` | |
| 167 | + |
| 168 | +Update both the function parameter and the `variables.data` object in the attachment mutation. |
| 169 | + |
| 170 | +#### To use a dynamic PDF URL |
| 171 | + |
| 172 | +If using a PDF generation service, you can: |
| 173 | + |
| 174 | +1. First make an HTTP Request action to generate the PDF |
| 175 | +2. Pass the returned PDF URL to the serverless function as a parameter |
| 176 | + |
| 177 | +```typescript |
| 178 | +export const main = async ( |
| 179 | + params: { companyId: string; pdfUrl: string; filename: string }, |
| 180 | +) => { |
| 181 | + const { companyId, pdfUrl, filename } = params; |
| 182 | + // ... rest of the function |
| 183 | +}; |
| 184 | +``` |
| 185 | + |
| 186 | +### الخطوة 4: الاختبار والتفعيل |
| 187 | + |
| 188 | +1. Save the workflow |
| 189 | +2. Navigate to a Company record |
| 190 | +3. Click the **⋮** menu and select your workflow |
| 191 | +4. Check the **Attachments** section on the record to verify the PDF was attached |
| 192 | +5. فعّل سير العمل |
| 193 | + |
| 194 | +## Combining with PDF Generation Services |
| 195 | + |
| 196 | +For creating dynamic quotes or invoices: |
| 197 | + |
| 198 | +### Example: Generate Quote → Attach PDF |
| 199 | + |
| 200 | +| الخطوة | الإجراء | الغرض | |
| 201 | +| ------ | ------------------------ | ---------------------------------------- | |
| 202 | +| 1 | Manual Trigger (Company) | User initiates on a record | |
| 203 | +| 2 | البحث عن سجل | Get Opportunity or line item details | |
| 204 | +| 3 | طلب HTTP | Call PDF generation API with record data | |
| 205 | +| 4 | Serverless Function | Download and attach the generated PDF | |
| 206 | + |
| 207 | +### Popular PDF Generation Services |
| 208 | + |
| 209 | +* **Carbone** - Template-based document generation |
| 210 | +* **PDFMonkey** - Dynamic PDF creation from templates |
| 211 | +* **DocuSeal** - Document automation platform |
| 212 | +* **Documint** - API-first document generation |
| 213 | + |
| 214 | +Each service provides an API that returns a PDF URL, which you can then pass to the serverless function. |
| 215 | + |
| 216 | +## استكشاف الأخطاء وإصلاحها |
| 217 | + |
| 218 | +| المشكلة | الحل | |
| 219 | +| ---------------------------- | ---------------------------------------------------------- | |
| 220 | +| "Failed to download PDF" | Check the PDF URL is accessible and returns a valid PDF | |
| 221 | +| "Upload failed" | Verify your API key is valid and has write permissions | |
| 222 | +| "Attachment creation failed" | Ensure the object ID field name matches your target object | |
| 223 | + |
| 224 | +## ذات صلة |
| 225 | + |
| 226 | +* [مشغلات سير العمل](/l/ar/user-guide/workflows/capabilities/workflow-triggers) |
| 227 | +* [Serverless Functions](/l/ar/user-guide/workflows/capabilities/workflow-actions#serverless-function) |
| 228 | +* [Generate a Quote or Invoice from Twenty](/l/ar/user-guide/workflows/how-tos/connect-to-other-tools/generate-quote-or-invoice-from-twenty) |
0 commit comments