Skip to content

Commit 31fe573

Browse files
authored
Merge pull request #1 from iamdual/typescript
Switch to TypeScript
2 parents 60bfa61 + 81b6ee3 commit 31fe573

21 files changed

+1951
-516
lines changed

.dockerignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules
1+
.DS_Store
2+
node_modules/
3+
dist/

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- "**.md"
7+
pull_request:
8+
paths-ignore:
9+
- "**.md"
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
strategy:
16+
matrix:
17+
node-version: [14.x, 15.x, 16.x, 17.x, 18.x]
18+
19+
steps:
20+
- uses: actions/checkout@v3
21+
- name: Use Node.js ${{ matrix.node-version }}
22+
uses: actions/setup-node@v3
23+
with:
24+
node-version: ${{ matrix.node-version }}
25+
- run: npm ci
26+
- run: npm test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
22
node_modules/
3+
dist/

README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
# html2pdf
2+
23
An HTML to PDF converter provided by RESTful API and command-line tool. Powered by `puppeteer`.
34

45
## RESTful API
6+
57
The best reason to use this method is that we can use it on any programming languages and software that support HTTP requests.
68

7-
### Start server with npm
9+
### Start server with Docker (recommended)
10+
811
```bash
9-
git clone https://github.com/iamdual/html2pdf
10-
npm install --production
11-
npm start
12+
docker run -p 3000:3000 iamdual/html2pdf
1213
```
1314

14-
### Start server with Docker
15+
### Start server with source
16+
1517
```bash
16-
docker run -p 3000:3000 iamdual/html2pdf
18+
git clone https://github.com/iamdual/html2pdf && cd html2pdf
19+
npm install
20+
npm start
1721
```
1822

19-
### Example usage (JSON POST)
23+
### API request with POST JSON
24+
2025
```bash
2126
curl -X POST 'http://localhost:3000/generate' \
2227
--header 'Content-Type: application/json' \
@@ -27,13 +32,16 @@ curl -X POST 'http://localhost:3000/generate' \
2732
}' > google.pdf
2833
```
2934

30-
### Example usage (Query parameters)
35+
### API request with query parameters
36+
3137
```bash
3238
curl 'http://localhost:3000/generate?source=https://google.com&timeout=10' > google.pdf
3339
```
3440

3541
## Command-line tool
42+
3643
It also support for command-line interface, so you can convert easily to a PDF.
44+
3745
```bash
3846
npm install -g https://github.com/iamdual/html2pdf
3947
html2pdf '<!DOCTYPE html><strong>Hello world!</strong>' -o output.pdf
@@ -42,8 +50,9 @@ html2pdf ./example.html --format A4 --pageRanges 1 -o example.pdf
4250
```
4351

4452
## Parameters
53+
4554
| Name | Type | Description |
46-
|------------|---------|-------------------------------------------------------------------------------------------------------------------------------------|
55+
| ---------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------- |
4756
| source | string | Set PDF document source. It must be specified. The source can be a URL or an HTML code. |
4857
| timeout | integer | Set connection timeout. Default is `10`. |
4958
| javascript | boolean | Enable JavaScripts. To enable, the value should be true. Disabled by default. |
@@ -57,4 +66,5 @@ html2pdf ./example.html --format A4 --pageRanges 1 -o example.pdf
5766
| margin | string | Set margin(s) for the PDF document. It can be all four margin or specified by the values separated with space. Default is `0`. |
5867

5968
## Author
69+
6070
Ekin Karadeniz (iamdual@icloud.com)

bin/index.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

bin/index.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env node
2+
/**
3+
* @iamdual/html2pdf
4+
* @author Ekin Karadeniz <iamdual@icloud.com>
5+
* @source https://github.com/iamdual/html2pdf
6+
*/
7+
8+
let configArgs: [];
9+
10+
const yargs = require("yargs/yargs")(process.argv.slice(2))
11+
.command(
12+
`$0 [options...] <source>`,
13+
"",
14+
(yargs: any) => {
15+
yargs.positional("source", {
16+
type: "string",
17+
describe: "The source can be a URL, a filename or an HTML content",
18+
});
19+
},
20+
(args: any) => {
21+
configArgs = { ...args };
22+
["_", "$0", "o", "output", "s", "stdout"].forEach((k: any) => {
23+
delete configArgs[k];
24+
});
25+
}
26+
)
27+
.option("o", {
28+
alias: "output",
29+
demandOption: false,
30+
describe: "Output PDF to a file",
31+
type: "string",
32+
})
33+
.option("s", {
34+
alias: "stdout",
35+
default: false,
36+
demandOption: false,
37+
describe: "Output PDF to stdout",
38+
type: "boolean",
39+
})
40+
.showHelpOnFail(true)
41+
.demandCommand(1, "");
42+
43+
const argv = yargs.argv;
44+
import { existsSync, readFileSync, writeFile } from "fs";
45+
import Config from "../src/config";
46+
import generator from "../src/generator";
47+
const panic = (message: string | NodeJS.ErrnoException) => {
48+
process.stderr.write(message + "\n\n");
49+
yargs.showHelp();
50+
process.exit(1);
51+
};
52+
53+
let config = new Config();
54+
55+
// Check is source is an exiting file
56+
if (!config.isUrl && existsSync(argv.source)) {
57+
argv.source = readFileSync(argv.source, "utf8");
58+
}
59+
60+
if (typeof argv.output !== "string" && !argv.stdout) {
61+
panic("Error: No output specified!");
62+
}
63+
64+
generator(config).then((pdf) => {
65+
if (typeof argv.output === "string" && argv.output.length > 0) {
66+
writeFile(argv.output, pdf, "binary", (err) => {
67+
if (err) panic(err);
68+
});
69+
} else if (argv.stdout) {
70+
process.stdout.write(pdf);
71+
} else {
72+
panic("Error: No output specified!");
73+
}
74+
});

0 commit comments

Comments
 (0)