Skip to content

Commit 925c463

Browse files
authored
Added libreoffice example (#1472)
* Added Libreoffice example docs * typo
1 parent e1bd6b3 commit 925c463

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: "Convert documents to PDF using LibreOffice"
3+
sidebarTitle: "LibreOffice PDF conversion"
4+
description: "This example demonstrates how to convert documents to PDF using LibreOffice with Trigger.dev."
5+
---
6+
7+
import LocalDevelopment from "/snippets/local-development-extensions.mdx";
8+
9+
## Prerequisites
10+
11+
- A project with [Trigger.dev initialized](/quick-start)
12+
- [LibreOffice](https://www.libreoffice.org/download/libreoffice-fresh/) installed on your machine
13+
- A [Cloudflare R2](https://developers.cloudflare.com) account and bucket
14+
15+
### Using our `aptGet` build extension to add the LibreOffice package
16+
17+
To deploy this task, you'll need to add LibreOffice to your project configuration, like this:
18+
19+
```ts trigger.config.ts
20+
import { aptGet } from "@trigger.dev/build/extensions/core";
21+
import { defineConfig } from "@trigger.dev/sdk/v3";
22+
23+
export default defineConfig({
24+
project: "<project ref>",
25+
// Your other config settings...
26+
build: {
27+
extensions: [
28+
aptGet({
29+
packages: ["libreoffice"],
30+
}),
31+
],
32+
},
33+
});
34+
```
35+
36+
<Note>
37+
[Build extensions](/config/config-file#extensions) allow you to hook into the build system and
38+
customize the build process or the resulting bundle and container image (in the case of
39+
deploying). You can use pre-built extensions or create your own.
40+
</Note>
41+
42+
You'll also need to add `@trigger.dev/build` to your `package.json` file under `devDependencies` if you don't already have it there.
43+
44+
## Convert a document to PDF using LibreOffice and upload to R2
45+
46+
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.
47+
48+
### Key Features
49+
50+
- Fetches a document from a given URL
51+
- Converts the document to PDF
52+
- Uploads the PDF to R2 storage
53+
54+
### Task code
55+
56+
```ts trigger/libreoffice-pdf-convert.ts
57+
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
58+
import { task } from "@trigger.dev/sdk/v3";
59+
import libreoffice from "libreoffice-convert";
60+
import { promisify } from "node:util";
61+
import path from "path";
62+
import fs from "fs";
63+
64+
const convert = promisify(libreoffice.convert);
65+
66+
// Initialize S3 client
67+
const s3Client = new S3Client({
68+
// How to authenticate to R2: https://developers.cloudflare.com/r2/api/s3/tokens/
69+
region: "auto",
70+
endpoint: process.env.R2_ENDPOINT,
71+
credentials: {
72+
accessKeyId: process.env.R2_ACCESS_KEY_ID ?? "",
73+
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY ?? "",
74+
},
75+
});
76+
77+
export const libreOfficePdfConvert = task({
78+
id: "libreoffice-pdf-convert",
79+
run: async (payload: { documentUrl: string }, { ctx }) => {
80+
// Set LibreOffice path for production environment
81+
if (ctx.environment.type !== "DEVELOPMENT") {
82+
process.env.LIBREOFFICE_PATH = "/usr/bin/libreoffice";
83+
}
84+
85+
try {
86+
// Create temporary file paths
87+
const inputPath = path.join(process.cwd(), `input_${Date.now()}.docx`);
88+
const outputPath = path.join(process.cwd(), `output_${Date.now()}.pdf`);
89+
90+
// Download file from URL
91+
const response = await fetch(payload.documentUrl);
92+
const buffer = Buffer.from(await response.arrayBuffer());
93+
fs.writeFileSync(inputPath, buffer);
94+
95+
const inputFile = fs.readFileSync(inputPath);
96+
// Convert to PDF using LibreOffice
97+
const pdfBuffer = await convert(inputFile, ".pdf", undefined);
98+
fs.writeFileSync(outputPath, pdfBuffer);
99+
100+
// Upload to R2
101+
const key = `converted-pdfs/output_${Date.now()}.pdf`;
102+
await s3Client.send(
103+
new PutObjectCommand({
104+
Bucket: process.env.R2_BUCKET,
105+
Key: key,
106+
Body: fs.readFileSync(outputPath),
107+
})
108+
);
109+
110+
// Cleanup temporary files
111+
fs.unlinkSync(inputPath);
112+
fs.unlinkSync(outputPath);
113+
114+
return { pdfLocation: key };
115+
} catch (error) {
116+
console.error("Error converting PDF:", error);
117+
throw error;
118+
}
119+
},
120+
});
121+
```
122+
123+
### Testing your task
124+
125+
To test this task, use this payload structure:
126+
127+
```json
128+
{
129+
"documentUrl": "<a-document-url>" // Replace <a-document-url> with the URL of the document you want to convert
130+
}
131+
```
132+
133+
<LocalDevelopment packages={"libreoffice"} />

docs/guides/introduction.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ Tasks you can copy and paste to get started with Trigger.dev. They can all be ex
4242
| [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. |
4343
| [Deepgram audio transcription](/guides/examples/deepgram-transcribe-audio) | Transcribe audio using Deepgram's speech recognition API. |
4444
| [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. |
45-
| [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. |
45+
| [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. |
4646
| [FFmpeg video processing](/guides/examples/ffmpeg-video-processing) | Use FFmpeg to process a video in various ways and save it to Cloudflare R2. |
4747
| [Firecrawl URL crawl](/guides/examples/firecrawl-url-crawl) | Learn how to use Firecrawl to crawl a URL and return LLM-ready markdown. |
48+
| [LibreOffice PDF conversion](/guides/examples/libreoffice-pdf-conversion) | Convert a document to PDF using LibreOffice. |
4849
| [OpenAI with retrying](/guides/examples/open-ai-with-retrying) | Create a reusable OpenAI task with custom retry options. |
4950
| [PDF to image](/guides/examples/pdf-to-image) | Use `MuPDF` to turn a PDF into images and save them to Cloudflare R2. |
5051
| [React to PDF](/guides/examples/react-pdf) | Use `react-pdf` to generate a PDF and save it to Cloudflare R2. |

docs/mint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@
340340
"guides/examples/fal-ai-realtime",
341341
"guides/examples/ffmpeg-video-processing",
342342
"guides/examples/firecrawl-url-crawl",
343+
"guides/examples/libreoffice-pdf-conversion",
343344
"guides/examples/open-ai-with-retrying",
344345
"guides/examples/pdf-to-image",
345346
"guides/examples/puppeteer",

0 commit comments

Comments
 (0)