Skip to content

Feature/node #35

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# compiled output
/dist
/node_modules
/var
/.env
!test/functional/.env
/**/node_modules
/**/dist

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/io.getbigger.api.iml
**/.env.docker
.env.local
36 changes: 26 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
FROM openlabs/docker-wkhtmltopdf:latest
MAINTAINER Sharoon Thomas <[email protected]>
FROM node:20

# Install dependencies for running web service
RUN apt-get install -y python-pip
RUN pip install werkzeug executor gunicorn
# Mise à jour du système et installation des dépendances
RUN apt-get update -y && apt-get upgrade -y \
&& apt-get install -y --no-install-recommends curl ca-certificates gnupg lsb-release

ADD app.py /app.py
EXPOSE 80
# Ajouter le dépôt Debian pour Chromium
RUN echo "deb http://deb.debian.org/debian/ stable main contrib" > /etc/apt/sources.list.d/debian.list \
&& apt-get update

# Installer Chromium
RUN apt-get install -y --no-install-recommends chromium \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Vérification de l'installation
RUN ls /usr/bin/

ENTRYPOINT ["usr/local/bin/gunicorn"]
# Copier les fichiers nécessaires
COPY --chown=node:node package*.json ./
COPY --chown=node:node app.js ./

# Installer les dépendances du projet
RUN npm install --frozen-lockfile

# Exposer le port 80
EXPOSE 80

# Show the extended help
CMD ["-b", "0.0.0.0:80", "--log-file", "-", "app:application"]
# Commande pour démarrer l'application
CMD ["node", "app.js"]
51 changes: 9 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,19 @@ Take a note of the public port number where docker binds to.
There are multiple ways to generate a PDF of HTML using the
service.

### Uploading a HTML file

This is a convenient way to use the service from command line
utilities like curl.

```sh
curl -X POST -vv -F 'file=@path/to/local/file.html' http://<docker-host>:<port>/ -o path/to/output/file.pdf
```

where:

* docker-host is the hostname or address of the docker host running the container
* port is the public port to which the container is bound to.

### JSON API

If you are planning on using this service in your application,
it might be more convenient to use the JSON API that the service
uses.

Here is an example using python requests:
Here is an example using axios requests:
When passing our settings we omit the double dash "--" at the start of the option.
For documentation on what options are available, visit http://wkhtmltopdf.org/usage/wkhtmltopdf.txt

```python
```NodeJs
import json
import requests

Expand All @@ -72,36 +62,8 @@ with open('/path/to/local/file.pdf', 'wb') as f:
f.write(response.content)
```

Here is another example in python, but this time we pass options to wkhtmltopdf.
When passing our settings we omit the double dash "--" at the start of the option.
For documentation on what options are available, visit http://wkhtmltopdf.org/usage/wkhtmltopdf.txt

```python
import json
import requests

url = 'http://<docker_host>:<port>/'
data = {
'contents': open('/file/to/convert.html').read().encode('base64'),
'options': {
#Omitting the "--" at the start of the option
'margin-top': '6',
'margin-left': '6',
'margin-right': '6',
'margin-bottom': '6',
'page-width': '105mm',
'page-height': '40mm'
}
}
headers = {
'Content-Type': 'application/json', # This is important
}
response = requests.post(url, data=json.dumps(data), headers=headers)

# Save the response contents to a file
with open('/path/to/local/file.pdf', 'wb') as f:
f.write(response.content)
```

## TODO

Expand All @@ -126,3 +88,8 @@ This image was built at [Openlabs](http://www.openlabs.co.in).
This image is professionally supported by [Openlabs](http://www.openlabs.co.in).
If you are looking for on-site teaching or consulting support, contact our
[sales](mailto:[email protected]) and [support](mailto:[email protected]) teams.




docker build --platform linux/amd64 --target application --file ./Dockerfile .
45 changes: 45 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const express = require("express");
const puppeteer = require("puppeteer");

const app = express();
const port = 80;
const appPrefix = "puppeteer-pdf-app";
app.use(express.json());
app.post("/", async (req, res) => {
let browser = null;
try {
const data = Buffer.from(req.body.contents, "base64");

const browser = await puppeteer.launch({
executablePath: "/usr/bin/chromium",
args: ["--no-sandbox"],
});

const page = await browser.newPage();

await page.setContent(data.toString(), { waitUntil: "domcontentloaded" });

await page.emulateMediaType("screen");

// Downlaod the PDF
const pdf = await page.pdf({
margin: { top: "100px", right: "50px", bottom: "100px", left: "50px" },
printBackground: true,
format: "A4",
...req.body,
});

res.send(Buffer.from(pdf));
} catch (e) {
console.log(e);
throw e;
} finally {
try {
await browser?.close();
} catch (e) {}
}
});

app.listen(port, () => {
console.log(`Puppeteer Pdf AAS app listening on port ${port}`);
});
71 changes: 0 additions & 71 deletions app.py

This file was deleted.

Loading