Skip to content

Docs/pdf to image #1330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/examples/intro.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description: "Learn how to use Trigger.dev with these practical task examples."
| [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. |
| [FFmpeg video processing](/examples/ffmpeg-video-processing) | Use FFmpeg to process a video in various ways and save it to Cloudflare R2. |
| [OpenAI with retrying](/examples/open-ai-with-retrying) | Create a reusable OpenAI task with custom retry options. |
| [PDF to image](/examples/pdf-to-image) | Use `MuPDF` to turn a PDF into images and save them to Cloudflare R2. |
| [React to PDF](/examples/react-pdf) | Use `react-pdf` to generate a PDF and save it to Cloudflare R2. |
| [Resend email sequence](/examples/resend-email-sequence) | Send a sequence of emails over several days using Resend with Trigger.dev. |
| [Sharp image processing](/examples/sharp-image-processing) | Use Sharp to process an image and save it to Cloudflare R2. |
Expand Down
84 changes: 84 additions & 0 deletions docs/examples/pdf-to-image.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
title: "Turn a PDF into an image using MuPDF"
sidebarTitle: "PDF to image"
description: "This example will show you how to turn a PDF into an image using MuPDF and Trigger.dev."
---

## Overview

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.

## Task code

```ts trigger/pdfToImage.ts
import { logger, task } from "@trigger.dev/sdk/v3";
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { execSync } from "child_process";
import fs from "fs";
import path from "path";

// Initialize S3 client
const s3Client = new S3Client({
region: "auto",
endpoint: process.env.S3_ENDPOINT,
credentials: {
accessKeyId: process.env.R2_ACCESS_KEY_ID ?? "",
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY ?? "",
},
});

export const pdfToImage = task({
id: "pdf-to-image",
run: async (payload: { pdfUrl: string; documentId: string }) => {
logger.log("Converting PDF to images", payload);

const pdfPath = `/tmp/${payload.documentId}.pdf`;
const outputDir = `/tmp/${payload.documentId}`;

// Download PDF and convert to images using MuPDF
execSync(`curl -s -o ${pdfPath} ${payload.pdfUrl}`);
fs.mkdirSync(outputDir, { recursive: true });
execSync(`mutool convert -o ${outputDir}/page-%d.png ${pdfPath}`);

// Upload images to R2
const uploadedUrls = [];
for (const file of fs.readdirSync(outputDir)) {
const s3Key = `images/${payload.documentId}/${file}`;
const uploadParams = {
Bucket: process.env.S3_BUCKET,
Key: s3Key,
Body: fs.readFileSync(path.join(outputDir, file)),
ContentType: "image/png",
};

logger.log("Uploading to R2", uploadParams);

await s3Client.send(new PutObjectCommand(uploadParams));
const s3Url = `https://${process.env.S3_BUCKET}.r2.cloudflarestorage.com/${s3Key}`;
uploadedUrls.push(s3Url);
logger.log("Image uploaded to R2", { url: s3Url });
}

// Clean up
fs.rmSync(outputDir, { recursive: true, force: true });
fs.unlinkSync(pdfPath);

logger.log("All images uploaded to R2", { urls: uploadedUrls });

return {
imageUrls: uploadedUrls,
};
},
});
```

## Testing your task

To test this task in the dashboard, you can use the following payload:

```json
{
"pdfUrl": "https://pdfobject.com/pdf/sample.pdf",
"documentId": "unique-document-id"
}
```
1 change: 1 addition & 0 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
"examples/dall-e3-generate-image",
"examples/ffmpeg-video-processing",
"examples/open-ai-with-retrying",
"examples/pdf-to-image",
"examples/sharp-image-processing",
"examples/react-pdf",
"examples/resend-email-sequence",
Expand Down
Loading