|
| 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 | +``` |
0 commit comments