Skip to content

Commit 1679a18

Browse files
authored
Docs/pdf to image (#1330)
* Added pdf-to-image example * Copy tweak * Squashed commit of the following: commit 64862db Author: Niels <[email protected]> Date: Thu Sep 19 16:25:17 2024 +0200 Update dotEnv.ts to ignore OTEL_EXPORTER_OTLP_ENDPOINT as well (#1307) * Update dotEnv.ts * Create four-buttons-run.md --------- Co-authored-by: Eric Allam <[email protected]> commit c65d482 Author: Thibaut Cuchet <[email protected]> Date: Thu Sep 19 16:22:57 2024 +0200 Feat: Extension puppeteer (#1323) * feat: add puppeteer extension * chore: update package and config * feat: add puppeteer task * Create little-donkeys-protect.md --------- Co-authored-by: Eric Allam <[email protected]> commit b4be736 Author: Eric Allam <[email protected]> Date: Thu Sep 19 15:21:03 2024 +0100 prismaExtension fixes for #1325 and #1327 * Copy tweak
1 parent 64862db commit 1679a18

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

docs/examples/intro.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ description: "Learn how to use Trigger.dev with these practical task examples."
99
| [DALL·E 3 image generation](/examples/dall-e3-generate-image) | Use OpenAI's GPT-4o and DALL·E 3 to generate an image and text. |
1010
| [FFmpeg video processing](/examples/ffmpeg-video-processing) | Use FFmpeg to process a video in various ways and save it to Cloudflare R2. |
1111
| [OpenAI with retrying](/examples/open-ai-with-retrying) | Create a reusable OpenAI task with custom retry options. |
12+
| [PDF to image](/examples/pdf-to-image) | Use `MuPDF` to turn a PDF into images and save them to Cloudflare R2. |
1213
| [React to PDF](/examples/react-pdf) | Use `react-pdf` to generate a PDF and save it to Cloudflare R2. |
1314
| [Resend email sequence](/examples/resend-email-sequence) | Send a sequence of emails over several days using Resend with Trigger.dev. |
1415
| [Sharp image processing](/examples/sharp-image-processing) | Use Sharp to process an image and save it to Cloudflare R2. |

docs/examples/pdf-to-image.mdx

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
title: "Turn a PDF into an image using MuPDF"
3+
sidebarTitle: "PDF to image"
4+
description: "This example will show you how to turn a PDF into an image using MuPDF and Trigger.dev."
5+
---
6+
7+
## Overview
8+
9+
This example demonstrates how to use Trigger.dev to turn a PDF into a series of images using MuPDF and upload them to Cloudflare R2.
10+
11+
## Task code
12+
13+
```ts trigger/pdfToImage.ts
14+
import { logger, task } from "@trigger.dev/sdk/v3";
15+
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
16+
import { execSync } from "child_process";
17+
import fs from "fs";
18+
import path from "path";
19+
20+
// Initialize S3 client
21+
const s3Client = new S3Client({
22+
region: "auto",
23+
endpoint: process.env.S3_ENDPOINT,
24+
credentials: {
25+
accessKeyId: process.env.R2_ACCESS_KEY_ID ?? "",
26+
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY ?? "",
27+
},
28+
});
29+
30+
export const pdfToImage = task({
31+
id: "pdf-to-image",
32+
run: async (payload: { pdfUrl: string; documentId: string }) => {
33+
logger.log("Converting PDF to images", payload);
34+
35+
const pdfPath = `/tmp/${payload.documentId}.pdf`;
36+
const outputDir = `/tmp/${payload.documentId}`;
37+
38+
// Download PDF and convert to images using MuPDF
39+
execSync(`curl -s -o ${pdfPath} ${payload.pdfUrl}`);
40+
fs.mkdirSync(outputDir, { recursive: true });
41+
execSync(`mutool convert -o ${outputDir}/page-%d.png ${pdfPath}`);
42+
43+
// Upload images to R2
44+
const uploadedUrls = [];
45+
for (const file of fs.readdirSync(outputDir)) {
46+
const s3Key = `images/${payload.documentId}/${file}`;
47+
const uploadParams = {
48+
Bucket: process.env.S3_BUCKET,
49+
Key: s3Key,
50+
Body: fs.readFileSync(path.join(outputDir, file)),
51+
ContentType: "image/png",
52+
};
53+
54+
logger.log("Uploading to R2", uploadParams);
55+
56+
await s3Client.send(new PutObjectCommand(uploadParams));
57+
const s3Url = `https://${process.env.S3_BUCKET}.r2.cloudflarestorage.com/${s3Key}`;
58+
uploadedUrls.push(s3Url);
59+
logger.log("Image uploaded to R2", { url: s3Url });
60+
}
61+
62+
// Clean up
63+
fs.rmSync(outputDir, { recursive: true, force: true });
64+
fs.unlinkSync(pdfPath);
65+
66+
logger.log("All images uploaded to R2", { urls: uploadedUrls });
67+
68+
return {
69+
imageUrls: uploadedUrls,
70+
};
71+
},
72+
});
73+
```
74+
75+
## Testing your task
76+
77+
To test this task in the dashboard, you can use the following payload:
78+
79+
```json
80+
{
81+
"pdfUrl": "https://pdfobject.com/pdf/sample.pdf",
82+
"documentId": "unique-document-id"
83+
}
84+
```

docs/mint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
"examples/dall-e3-generate-image",
278278
"examples/ffmpeg-video-processing",
279279
"examples/open-ai-with-retrying",
280+
"examples/pdf-to-image",
280281
"examples/sharp-image-processing",
281282
"examples/react-pdf",
282283
"examples/resend-email-sequence",

0 commit comments

Comments
 (0)