Skip to content

Commit 60cf5a3

Browse files
committed
first version 👍
1 parent ff83133 commit 60cf5a3

9 files changed

+1108
-19
lines changed

.github/workflows/docker-image.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Docker Image CI
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
tags:
8+
- 'v*'
9+
10+
jobs:
11+
push_to_registry:
12+
name: Push Docker image to Docker Hub
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Check out the repo
16+
uses: actions/checkout@v4
17+
18+
- name: Set up QEMU
19+
uses: docker/setup-qemu-action@v3
20+
21+
- name: Set up Docker Buildx
22+
uses: docker/setup-buildx-action@v3
23+
24+
- name: Log in to Docker Hub
25+
uses: docker/login-action@v3
26+
with:
27+
username: ${{ secrets.DOCKERHUB_USERNAME }}
28+
password: ${{ secrets.DOCKERHUB_TOKEN }}
29+
30+
- name: Extract metadata (tags, labels) for Docker
31+
id: meta
32+
uses: docker/[email protected]
33+
with:
34+
images: codeinchq/office2pdf
35+
36+
- name: Build and push Docker image
37+
uses: docker/build-push-action@v5
38+
with:
39+
context: .
40+
platforms: linux/amd64,linux/arm64
41+
target: office2pdf-prod
42+
push: true
43+
tags: ${{ steps.meta.outputs.tags }}
44+
labels: ${{ steps.meta.outputs.labels }}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea
2+
.DS_Store
3+
/node_modules

Dockerfile

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
####################################################################################################
2+
# OFFICE2PDF
3+
####################################################################################################
4+
FROM --platform=$TARGETPLATFORM node:21-alpine AS office2pdf
5+
6+
ENV PORT=3000
7+
ENV NODE_ENV=production
8+
WORKDIR /app
9+
10+
# https://github.com/woahbase/alpine-libreoffice/blob/master/Dockerfile_x86_64
11+
RUN apk add --no-cache --purge -uU \
12+
curl icu-libs unzip zlib-dev musl \
13+
mesa-gl mesa-vulkan-swrast \
14+
libreoffice libreoffice-base libreoffice-lang-fr \
15+
ttf-freefont ttf-opensans ttf-inconsolata \
16+
ttf-liberation ttf-dejavu \
17+
libstdc++ dbus-x11 \
18+
&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories \
19+
&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories \
20+
&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories \
21+
&& apk add --no-cache -U ttf-font-awesome ttf-mononoki ttf-hack \
22+
&& rm -rf /var/cache/apk/* /tmp/*
23+
24+
25+
####################################################################################################
26+
# OFFICE2PDF prod
27+
####################################################################################################
28+
FROM office2pdf AS office2pdf-prod
29+
30+
COPY main.mjs /app/
31+
COPY package.json /app/
32+
COPY package-lock.json /app/
33+
RUN mkdir -p /app/temp
34+
RUN npm install --production
35+
36+
EXPOSE $PORT
37+
ENTRYPOINT ["node", "main.mjs"]
38+
39+
40+
####################################################################################################
41+
# OFFICE2PDF dev
42+
####################################################################################################
43+
FROM office2pdf AS office2pdf-dev
44+
45+
ENV NODE_ENV=development
46+
RUN npm install --global nodemon
47+
48+
EXPOSE $PORT
49+
ENTRYPOINT ["nodemon", "main.mjs"]

LICENSE

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1-
MIT License
1+
Copyright 2024 Code Inc. / Joan Fabrégat <[email protected]>
22

3-
Copyright (c) 2024 Code Inc.
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
116

12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
11
# office2pdf
2-
Containerized API for the conversion of Office files to PDF using LibreOffice
2+
3+
[![Docker Image CI](https://github.com/codeinchq/office2pdf/actions/workflows/docker-image.yml/badge.svg)](https://github.com/codeinchq/office2pdf/actions/workflows/docker-image.yml)
4+
5+
This repository contains a simple containerized API to convert Office documents to PDF documents
6+
using [LibreOffice](https://www.libreoffice.org/) headless.
7+
8+
The image is available on [Docker Hub](https://hub.docker.com/r/codeinchq/office2pdf) under the name `codeinchq/office2pdf`.
9+
10+
## Configuration
11+
12+
By default, the container listens on port 3000. The port is configurable using the `PORT` environment variable.
13+
14+
## Usage
15+
16+
All requests must by send in POST to the `/convert` endpoint with a `multipart/form-data` content type. The request must contain an Office file with the key `file`.
17+
18+
The server returns `200` if the conversion was successful and the images are available in the response body. In case of error, the server returns a `400` status code with a JSON object containing the error message (format: `{error: string}`).
19+
20+
## Example
21+
22+
### Step 1: run the container using Docker
23+
```bash
24+
docker run -p "3000:3000" codeinchq/office2pdf
25+
```
26+
27+
### Step 2: convert an Office file to PDF
28+
```bash
29+
curl -X POST -F "file=@/path/to/file.docx" http://localhost:3000/convert -o example.pdf
30+
```
31+
32+
## License
33+
34+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

docker-compose.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
office2pdf:
3+
image: codeinchq/office2pdf:latest
4+
build:
5+
context: .
6+
target: office2pdf-dev
7+
platforms:
8+
#- linux/amd64
9+
- linux/arm64
10+
volumes:
11+
- ./:/app/
12+
ports:
13+
- "3000:3000"
14+
tty: true

main.mjs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2024 Code Inc. <https://www.codeinc.co>
3+
*
4+
* Use of this source code is governed by an MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import express from 'express';
10+
import multer from 'multer';
11+
import uniqid from 'uniqid';
12+
import {resolve} from "path";
13+
import {unlinkSync} from "node:fs";
14+
import {execSync} from "child_process";
15+
import {existsSync} from "node:fs";
16+
17+
const port = +(process.env.PORT ?? 3000);
18+
const tempDir = 'temp';
19+
20+
const app = express();
21+
const upload = multer({dest: tempDir});
22+
23+
app.post('/convert', upload.single('file'), (req, res) => {
24+
if (!req.file.filename) {
25+
res.status(400);
26+
res.send({error: 'No file uploaded'});
27+
return;
28+
}
29+
30+
// converting the PDF file to images
31+
console.log(`Converting Office file ${req.file.originalname} to PDF`);
32+
try {
33+
const outPath = `${req.file.path}.pdf`;
34+
35+
execSync(
36+
`libreoffice `
37+
+ `--headless `
38+
+ `--convert-to pdf:writer_pdf_Export `
39+
+ `--outdir ${tempDir} `
40+
+ `${req.file.path}`,
41+
);
42+
43+
if (!existsSync(outPath)) {
44+
throw 'Failed to convert the office file to PDF';
45+
}
46+
47+
// sending the images as a response
48+
// await res.download(imagePath);
49+
res.sendFile(outPath, {root: resolve()}, (err) => {
50+
// cleaning up
51+
unlinkSync(req.file.path);
52+
unlinkSync(outPath);
53+
});
54+
}
55+
catch (e) {
56+
console.error(e.message);
57+
res.status(400);
58+
res.send({error: e.message});
59+
unlinkSync(req.file.path);
60+
}
61+
});
62+
63+
app.listen(port, () => {
64+
console.log(`Now listening on port ${port}`);
65+
});

0 commit comments

Comments
 (0)