Skip to content

Commit d60299b

Browse files
committed
add docker support
1 parent 778619d commit d60299b

File tree

6 files changed

+47
-30
lines changed

6 files changed

+47
-30
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM alpine:3.17.1
2+
3+
RUN apk add --no-cache \
4+
chromium \
5+
nss \
6+
freetype \
7+
harfbuzz \
8+
ca-certificates \
9+
ttf-freefont \
10+
nodejs \
11+
npm
12+
13+
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
14+
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
15+
ENV HTML2PDF_NO_SANDBOX=true
16+
17+
WORKDIR /app
18+
COPY ["package.json", "package-lock.json", "./"]
19+
RUN npm install --production
20+
COPY . .
21+
CMD ["npm", "start"]

README.md

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
11
# html2pdf
2-
HTML to PDF converter provided by command line interface and RESTful API. Powered by `puppeteer`.
3-
4-
## Installing
5-
```
6-
npm install -g https://github.com/iamdual/html2pdf
7-
```
2+
An HTML to PDF converter provided by RESTful API and command-line tool. Powered by `puppeteer`.
83

94
## Usage
105

11-
### Command-line
12-
```bash
13-
html2pdf '<!DOCTYPE html><strong>Hello world!</strong>' -o output.pdf
14-
15-
html2pdf "https://www.google.com" --format A4 --timeout 10 -o google.pdf
16-
```
17-
18-
### Library
19-
```js
20-
const html2pdf = require("@iamdual/html2pdf");
21-
const config = html2pdf.config({source: 'https://google.com', format: 'A4', timeout: 10});
22-
html2pdf.generate(config).then(pdf => {
23-
console.log('PDF generated!');
24-
require('fs').writeFile('google.pdf', pdf, 'binary', err => {});
25-
});
26-
```
27-
286
### RESTful API
297
```bash
30-
git clone https://github.com/iamdual/html2pdf
31-
npm start
32-
338
curl -X POST 'http://localhost:3000/generate' \
349
--header 'Content-Type: application/json' \
3510
--data '{
@@ -39,6 +14,13 @@ curl -X POST 'http://localhost:3000/generate' \
3914
}' > google.pdf
4015
```
4116

17+
### Command-line
18+
```bash
19+
npm install -g https://github.com/iamdual/html2pdf
20+
html2pdf '<!DOCTYPE html><strong>Hello world!</strong>' -o output.pdf
21+
html2pdf "https://www.google.com" --format A4 --timeout 10 -o google.pdf
22+
```
23+
4224
## Parameters
4325
| Name | Type | Description |
4426
|------------|---------|-------------------------------------------------------------------------------------------------------------------------------------|

src/generator/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
*/
66

77
const puppeteer = require('puppeteer');
8-
const chromiumPath = process.env.CHROMIUM_PATH ? process.env.CHROMIUM_PATH : undefined;
8+
9+
let args = [];
10+
if (process.env.HTML2PDF_NO_SANDBOX) {
11+
args.push('--no-sandbox')
12+
}
913

1014
module.exports = async function generator(config) {
1115

1216
// Create a browser instance
1317
const browser = await puppeteer.launch({
1418
headless: true,
15-
executablePath: chromiumPath,
19+
args: args
1620
});
1721

1822
// Create a new page

src/rest-api/generate.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ const generator = require('../generator');
1010

1111
module.exports = async function generate(req, res) {
1212

13+
if (!['GET', 'POST'].includes(req.method)) {
14+
res.status(405).json({ error: 'INVALID_METHOD', errorMessage: 'Method not allowed.' });
15+
return;
16+
}
17+
1318
let config;
1419
if (req.method === 'POST') {
1520
config = configParser(req.body);
@@ -32,6 +37,9 @@ module.exports = async function generate(req, res) {
3237
res.status(408).json({ error: 'TIMEOUT', errorMessage: 'Request timeout.' });
3338
return;
3439
}
40+
if (process.env.NODE_ENV === 'development') {
41+
console.log(error);
42+
}
3543
res.status(500).json({ error: 'SERVER_ERROR', errorMessage: 'We are unable to fulfill your request.' });
3644
}
3745
}

src/rest-api/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ const app = express();
99
app.use(express.json());
1010
app.use((err, req, res, next) => {
1111
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
12-
return res.status(400).send({ error: 'INVALID_REQUEST', errorMessage: err.message });
12+
res.status(400).send({ error: 'INVALID_REQUEST', errorMessage: err.message });
13+
return;
1314
}
1415
next();
1516
});
1617
app.all('/generate', require('./generate'));
1718

18-
const port = process.env.LISTEN_PORT || 3000;
19+
const port = process.env.HTML2PDF_API_PORT || 3000;
1920
app.listen(port, function () {
2021
console.log('Server has been started on http://localhost:%d', port);
2122
});

0 commit comments

Comments
 (0)