Skip to content

Added libreoffice example #1472

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 2 commits into from
Nov 13, 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
133 changes: 133 additions & 0 deletions docs/guides/examples/libreoffice-pdf-conversion.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
title: "Convert documents to PDF using LibreOffice"
sidebarTitle: "LibreOffice PDF conversion"
description: "This example demonstrates how to convert documents to PDF using LibreOffice with Trigger.dev."
---

import LocalDevelopment from "/snippets/local-development-extensions.mdx";

## Prerequisites

- A project with [Trigger.dev initialized](/quick-start)
- [LibreOffice](https://www.libreoffice.org/download/libreoffice-fresh/) installed on your machine
- A [Cloudflare R2](https://developers.cloudflare.com) account and bucket

### Using our `aptGet` build extension to add the LibreOffice package

To deploy this task, you'll need to add LibreOffice to your project configuration, like this:

```ts trigger.config.ts
import { aptGet } from "@trigger.dev/build/extensions/core";
import { defineConfig } from "@trigger.dev/sdk/v3";

export default defineConfig({
project: "<project ref>",
// Your other config settings...
build: {
extensions: [
aptGet({
packages: ["libreoffice"],
}),
],
},
});
```

<Note>
[Build extensions](/config/config-file#extensions) allow you to hook into the build system and
customize the build process or the resulting bundle and container image (in the case of
deploying). You can use pre-built extensions or create your own.
</Note>

You'll also need to add `@trigger.dev/build` to your `package.json` file under `devDependencies` if you don't already have it there.

## Convert a document to PDF using LibreOffice and upload to R2

This task demonstrates how to use LibreOffice to convert a document (.doc or .docx) to PDF and upload the PDF to an R2 storage bucket.

### Key Features

- Fetches a document from a given URL
- Converts the document to PDF
- Uploads the PDF to R2 storage

### Task code

```ts trigger/libreoffice-pdf-convert.ts
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { task } from "@trigger.dev/sdk/v3";
import libreoffice from "libreoffice-convert";
import { promisify } from "node:util";
import path from "path";
import fs from "fs";

const convert = promisify(libreoffice.convert);

// Initialize S3 client
const s3Client = new S3Client({
// How to authenticate to R2: https://developers.cloudflare.com/r2/api/s3/tokens/
region: "auto",
endpoint: process.env.R2_ENDPOINT,
credentials: {
accessKeyId: process.env.R2_ACCESS_KEY_ID ?? "",
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY ?? "",
},
});

export const libreOfficePdfConvert = task({
id: "libreoffice-pdf-convert",
run: async (payload: { documentUrl: string }, { ctx }) => {
// Set LibreOffice path for production environment
if (ctx.environment.type !== "DEVELOPMENT") {
process.env.LIBREOFFICE_PATH = "/usr/bin/libreoffice";
}

try {
// Create temporary file paths
const inputPath = path.join(process.cwd(), `input_${Date.now()}.docx`);
const outputPath = path.join(process.cwd(), `output_${Date.now()}.pdf`);

// Download file from URL
const response = await fetch(payload.documentUrl);
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync(inputPath, buffer);

const inputFile = fs.readFileSync(inputPath);
// Convert to PDF using LibreOffice
const pdfBuffer = await convert(inputFile, ".pdf", undefined);
fs.writeFileSync(outputPath, pdfBuffer);

// Upload to R2
const key = `converted-pdfs/output_${Date.now()}.pdf`;
await s3Client.send(
new PutObjectCommand({
Bucket: process.env.R2_BUCKET,
Key: key,
Body: fs.readFileSync(outputPath),
})
);

// Cleanup temporary files
fs.unlinkSync(inputPath);
fs.unlinkSync(outputPath);

return { pdfLocation: key };
} catch (error) {
console.error("Error converting PDF:", error);
throw error;
}
},
});
```

### Testing your task

To test this task, use this payload structure:

```json
{
"documentUrl": "<a-document-url>" // Replace <a-document-url> with the URL of the document you want to convert
}
```

<LocalDevelopment packages={"libreoffice"} />
3 changes: 2 additions & 1 deletion docs/guides/introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ Tasks you can copy and paste to get started with Trigger.dev. They can all be ex
| [DALL·E 3 image generation](/guides/examples/dall-e3-generate-image) | Use OpenAI's GPT-4o and DALL·E 3 to generate an image and text. |
| [Deepgram audio transcription](/guides/examples/deepgram-transcribe-audio) | Transcribe audio using Deepgram's speech recognition API. |
| [Fal.ai image to cartoon](/guides/examples/fal-ai-image-to-cartoon) | Convert an image to a cartoon using Fal.ai, and upload the result to Cloudflare R2. |
| [Fal.ai with Realtime](/guides/examples/fal-ai-realtime) | Generate an image from a prompt using Fal.ai and show the progress of the task on the frontend using Realtime. |
| [Fal.ai with Realtime](/guides/examples/fal-ai-realtime) | Generate an image from a prompt using Fal.ai and show the progress of the task on the frontend using Realtime. |
| [FFmpeg video processing](/guides/examples/ffmpeg-video-processing) | Use FFmpeg to process a video in various ways and save it to Cloudflare R2. |
| [Firecrawl URL crawl](/guides/examples/firecrawl-url-crawl) | Learn how to use Firecrawl to crawl a URL and return LLM-ready markdown. |
| [LibreOffice PDF conversion](/guides/examples/libreoffice-pdf-conversion) | Convert a document to PDF using LibreOffice. |
| [OpenAI with retrying](/guides/examples/open-ai-with-retrying) | Create a reusable OpenAI task with custom retry options. |
| [PDF to image](/guides/examples/pdf-to-image) | Use `MuPDF` to turn a PDF into images and save them to Cloudflare R2. |
| [React to PDF](/guides/examples/react-pdf) | Use `react-pdf` to generate a PDF and save it to Cloudflare R2. |
Expand Down
1 change: 1 addition & 0 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
"guides/examples/fal-ai-realtime",
"guides/examples/ffmpeg-video-processing",
"guides/examples/firecrawl-url-crawl",
"guides/examples/libreoffice-pdf-conversion",
"guides/examples/open-ai-with-retrying",
"guides/examples/pdf-to-image",
"guides/examples/puppeteer",
Expand Down