fade22015f
* Adds new features table to top of v4 upgrade guide * Adds wait idempotency to wait-until, wait-for, and wait-for-token pages * Adds new priority docs page and updates the v4 upgrade guide * Adds new task lifecycle hooks * Removes the message about requiring tasks to be exported * Adds new global lifecycle hooks section * Moves sections from upgrade guide into the table * Adds hidden task page * Improves the global lifecycle hooks section * Updates middleware and locals section * Adds new useWaitToken page to the react hooks section * Adds a new ai.tool section * Moves Docker (legacy) page into self-hosting section * Removes known issues from v4 upgrade guide * Replace “toolTask” with “ai.tool” in the Streams page example * Renames guide to “Migrating from v3” and adds redirect * Remove references to v4 * Removes changelog from migration guide * The installation guide now references `@latest update` * Changes all references from `/sdk/v3` to `/sdk` * Updates @v4-beta to @latest * Fixed broken link * Fixes broken link * Adds an upgrade to v4 using AI section * Fixes 2 broken links * Adds an entry for targetting preview branches * Updates the run statuses * Adds boolean helpers section to the runs and realtime pages * Updates the concurrency page * Updates the test page to include the new options * Adds SDK and curl options for the preview branch targeting * Updates new bulk actions page * Remove the releasing concurrency section * Got rid of some more @v4-beta mentions * Improved rate limit docs * Improved migrating docs * Removed commented sections of the docs * useWaitToken hook * Fixed the description * Fix for missing test image --------- Co-authored-by: Matt Aitken <matt@mattaitken.com> Co-authored-by: Dan <8297864+D-K-P@users.noreply.github.com>
103 lines
3.0 KiB
Plaintext
103 lines
3.0 KiB
Plaintext
---
|
|
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."
|
|
---
|
|
|
|
import LocalDevelopment from "/snippets/local-development-extensions.mdx";
|
|
|
|
## 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.
|
|
|
|
## Update your build configuration
|
|
|
|
To use this example, add these build settings below to your `trigger.config.ts` file. They ensure that the `mutool` and `curl` packages are installed when you deploy your task. You can learn more about this and see more build settings [here](/config/extensions/aptGet).
|
|
|
|
```ts trigger.config.ts
|
|
export default defineConfig({
|
|
project: "<project ref>",
|
|
// Your other config settings...
|
|
build: {
|
|
extensions: [aptGet({ packages: ["mupdf-tools", "curl"] })],
|
|
},
|
|
});
|
|
```
|
|
|
|
## Task code
|
|
|
|
```ts trigger/pdfToImage.ts
|
|
import { logger, task } from "@trigger.dev/sdk";
|
|
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"
|
|
}
|
|
```
|
|
|
|
<LocalDevelopment packages={"mupdf-tools from MuPDF"} />
|