Skip to content

Commit c1690bd

Browse files
authored
New React to PDF example task (#1300)
* Better code snippet to clarify your framework * New page for react-pdf * Added 3 more examples * New react to PDF example * Alphabeticalise the side menu * Removed github link and tweaked title
1 parent 9a7ad92 commit c1690bd

File tree

6 files changed

+200
-9
lines changed

6 files changed

+200
-9
lines changed

docs/examples/generate-image-with-dall-e3.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Generate an image using DALL·E 3"
3-
sidebarTitle: "Generate image using DALL·E"
3+
sidebarTitle: "DALL·E image generation"
44
description: "This example will show you how to generate an image using DALL·E 3 and text using GPT-4o with Trigger.dev."
55
---
66

@@ -64,8 +64,4 @@ function generateTextPrompt(theme: string, description: string): any {
6464
function generateImagePrompt(theme: string, description: string): any {
6565
return `Theme: ${theme}\n\nDescription: ${description}`;
6666
}
67-
```
68-
69-
## GitHub repo
70-
71-
View this example task [on GitHub](https://github.com/triggerdotdev/v3-test-projects/blob/main/website-examples/trigger/generateContent.ts).
67+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: "Call OpenAI with retrying"
3+
sidebarTitle: "OpenAI with retrying"
4+
description: "This example will show you how to call OpenAI with retrying using Trigger.dev."
5+
---
6+
7+
## Overview
8+
9+
Sometimes OpenAI calls can take a long time to complete, or they can fail. This task will retry if the API call fails completely or if the response is empty.
10+
11+
## Task code
12+
13+
```ts trigger/openai.ts
14+
import { task } from "@trigger.dev/sdk/v3";
15+
import OpenAI from "openai";
16+
17+
const openai = new OpenAI({
18+
apiKey: process.env.OPENAI_API_KEY,
19+
});
20+
21+
export const openaiTask = task({
22+
id: "openai-task",
23+
//specifying retry options overrides the defaults defined in your trigger.config file
24+
retry: {
25+
maxAttempts: 10,
26+
factor: 1.8,
27+
minTimeoutInMs: 500,
28+
maxTimeoutInMs: 30_000,
29+
randomize: false,
30+
},
31+
run: async (payload: { prompt: string }) => {
32+
//if this fails, it will throw an error and retry
33+
const chatCompletion = await openai.chat.completions.create({
34+
messages: [{ role: "user", content: payload.prompt }],
35+
model: "gpt-3.5-turbo",
36+
});
37+
38+
if (chatCompletion.choices[0]?.message.content === undefined) {
39+
//sometimes OpenAI returns an empty response, let's retry by throwing an error
40+
throw new Error("OpenAI call failed");
41+
}
42+
43+
return chatCompletion.choices[0].message.content;
44+
},
45+
});
46+
47+
```

docs/examples/react-pdf.mdx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: "Generate a PDF using react-pdf and save it to R2"
3+
sidebarTitle: "React to PDF"
4+
description: "This example will show you how to generate a PDF using Trigger.dev."
5+
---
6+
7+
## Overview
8+
9+
This example demonstrates how to use Trigger.dev to generate a PDF using `react-pdf` and save it to Cloudflare R2.
10+
11+
## Task code
12+
13+
```ts trigger/generateResumePDF.ts
14+
import { logger, task } from "@trigger.dev/sdk/v3";
15+
import { Document, Page, Text, View } from "@react-pdf/renderer";
16+
import { renderToBuffer } from "@react-pdf/renderer";
17+
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
18+
19+
// Initialize S3 client
20+
const s3Client = new S3Client({
21+
region: "auto",
22+
endpoint: process.env.S3_ENDPOINT,
23+
credentials: {
24+
accessKeyId: process.env.R2_ACCESS_KEY_ID ?? "",
25+
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY ?? "",
26+
},
27+
});
28+
29+
export const generateResumePDF = task({
30+
id: "generate-resume-pdf",
31+
run: async (payload: { text: string }) => {
32+
logger.log("Generating PDF resume", payload);
33+
34+
// Render the ResumeDocument component to a PDF buffer
35+
const pdfBuffer = await renderToBuffer(
36+
<Document>
37+
<Page size="A4">
38+
<View>
39+
<Text>{payload.text}</Text>
40+
</View>
41+
</Page>
42+
</Document>
43+
);
44+
45+
// Generate a unique filename
46+
const filename = `${payload.text
47+
.replace(/\s+/g, "-")
48+
.toLowerCase()}-${Date.now()}.pdf`;
49+
50+
// Upload to R2
51+
const s3Key = `resumes/${filename}`;
52+
const uploadParams = {
53+
Bucket: process.env.S3_BUCKET,
54+
Key: s3Key,
55+
Body: pdfBuffer,
56+
ContentType: "application/pdf",
57+
};
58+
59+
logger.log("Uploading to R2 with params", uploadParams);
60+
61+
// Upload the PDF to R2 and return the URL.
62+
await s3Client.send(new PutObjectCommand(uploadParams));
63+
const s3Url = `https://${process.env.S3_BUCKET}.s3.amazonaws.com/${s3Key}`;
64+
logger.log("PDF uploaded to R2", { url: s3Url });
65+
return { pdfUrl: s3Url };
66+
},
67+
});
68+
69+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: "Send a sequence of emails using Resend"
3+
sidebarTitle: "Resend email sequence"
4+
description: "This example will show you how to send a sequence of emails over several days using Resend with Trigger.dev."
5+
---
6+
7+
## Overview
8+
9+
Each email is wrapped in retry.onThrow. This will retry the block of code if an error is thrown. This is useful when you don’t want to retry the whole task, but just a part of it. The entire task will use the default retrying, so can also retry.
10+
11+
Additionally this task uses wait.for to wait for a certain amount of time before sending the next email. During the waiting time, the task will be paused and will not consume any resources.
12+
13+
## Task code
14+
15+
```ts trigger/email-sequence.ts
16+
import { Resend } from "resend";
17+
18+
const resend = new Resend(process.env.RESEND_ASP_KEY);
19+
20+
export const emailSequence = task({
21+
id: "email-sequence",
22+
run: async (payload: { userId: string; email: string; name: string }) => {
23+
console.log(`Start email sequence for user ${payload.userId}`, payload);
24+
25+
//send the first email immediately
26+
const firstEmailResult = await retry.onThrow(
27+
async ({ attempt }) => {
28+
const { data, error } = await resend.emails.send({
29+
30+
to: payload.email,
31+
subject: "Welcome to Trigger.dev",
32+
html: `<p>Hello ${payload.name},</p><p>Welcome to Trigger.dev</p>`,
33+
});
34+
35+
if (error) {
36+
//throwing an error will trigger a retry of this block
37+
throw error;
38+
}
39+
40+
return data;
41+
},
42+
{ maxAttempts: 3 }
43+
);
44+
45+
//then wait 3 days
46+
await wait.for({ days: 3 });
47+
48+
//send the second email
49+
const secondEmailResult = await retry.onThrow(
50+
async ({ attempt }) => {
51+
const { data, error } = await resend.emails.send({
52+
53+
to: payload.email,
54+
subject: "Some tips for you",
55+
html: `<p>Hello ${payload.name},</p><p>Here are some tips for you…</p>`,
56+
});
57+
58+
if (error) {
59+
//throwing an error will trigger a retry of this block
60+
throw error;
61+
}
62+
63+
return data;
64+
},
65+
{ maxAttempts: 3 }
66+
);
67+
68+
//etc...
69+
},
70+
});
71+
72+
```

docs/mint.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,12 @@
252252
},
253253
{
254254
"group": "Examples",
255-
"pages": ["examples/generate-image-with-dall-e3"]
255+
"pages": [
256+
"examples/generate-image-with-dall-e3",
257+
"examples/open-ai-with-retrying",
258+
"examples/react-pdf",
259+
"examples/resend-email-sequence"
260+
]
256261
}
257262
],
258263
"footerSocials": {

docs/snippets/cli-commands-develop.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ Install the concurrently package as a dev dependency:
9090
concurrently --raw --kill-others npm:dev:remix npm:dev:trigger
9191
```
9292

93-
Then add something like this in your package.json scripts. This assumes you're running Next.js so swap that bit out if you're not:
93+
Then add something like this in your package.json scripts:
9494

9595
```json
9696
"scripts": {
9797
"dev": "concurrently --raw --kill-others npm:dev:*",
98-
"dev:next": "next dev",
9998
"dev:trigger": "npx trigger.dev@beta dev",
99+
// Add your framework-specific dev script here, for example:
100+
// "dev:next": "next dev",
101+
// "dev:remix": "remix dev",
100102
//...
101103
}
102104
```

0 commit comments

Comments
 (0)