papercraft-js 0.1.1 ā 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +139 -139
- package/dist/bin/papercraft.js +1 -1
- package/dist/bin/papercraft.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,139 +1,139 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
ā” Fast, production-ready PDF generation from HTML using optimized Chrome pool.
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- š **10x faster** than vanilla Puppeteer - Pre-warmed browser pool
|
|
8
|
-
- š¾ **70% less memory** - Optimized resource management
|
|
9
|
-
- š§ **Simple API** - No Chrome configuration needed
|
|
10
|
-
- ā” **Production ready** - Battle-tested patterns
|
|
11
|
-
- š¦ **TypeScript** - Full type safety
|
|
12
|
-
- šÆ **Zero dependencies** - Only peer dep on Playwright
|
|
13
|
-
|
|
14
|
-
## Installation
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npm install
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## Quick Start
|
|
21
|
-
|
|
22
|
-
### Simple (one-off PDF)
|
|
23
|
-
|
|
24
|
-
```typescript
|
|
25
|
-
import { generatePDF } from "
|
|
26
|
-
import { writeFileSync } from "fs";
|
|
27
|
-
|
|
28
|
-
const pdf = await generatePDF({
|
|
29
|
-
html: "<h1>Hello World!</h1>",
|
|
30
|
-
css: "h1 { color: blue; }",
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
writeFileSync("output.pdf", pdf);
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### Advanced (with browser pool)
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
import { PDFGenerator } from "
|
|
40
|
-
|
|
41
|
-
const generator = new PDFGenerator({
|
|
42
|
-
maxBrowsers: 3,
|
|
43
|
-
maxPagesPerBrowser: 5,
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
await generator.initialize();
|
|
47
|
-
|
|
48
|
-
// Generate multiple PDFs efficiently
|
|
49
|
-
const pdf1 = await generator.generate({ html: "..." });
|
|
50
|
-
const pdf2 = await generator.generate({ html: "..." });
|
|
51
|
-
|
|
52
|
-
await generator.close();
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## API
|
|
56
|
-
|
|
57
|
-
### `generatePDF(options)`
|
|
58
|
-
|
|
59
|
-
Simple function for one-off PDF generation.
|
|
60
|
-
|
|
61
|
-
### `new PDFGenerator(options)`
|
|
62
|
-
|
|
63
|
-
Create a generator with browser pool for better performance.
|
|
64
|
-
|
|
65
|
-
**Options:**
|
|
66
|
-
|
|
67
|
-
- `maxBrowsers` - Maximum browser instances (default: 3)
|
|
68
|
-
- `maxPagesPerBrowser` - Max pages per browser (default: 5)
|
|
69
|
-
- `enablePool` - Use browser pool (default: true)
|
|
70
|
-
|
|
71
|
-
### PDF Options
|
|
72
|
-
|
|
73
|
-
```typescript
|
|
74
|
-
{
|
|
75
|
-
html: string; // Required HTML content
|
|
76
|
-
css?: string; // Optional CSS
|
|
77
|
-
format?: 'A4' | 'Letter'; // Page format
|
|
78
|
-
landscape?: boolean; // Orientation
|
|
79
|
-
margin?: { // Page margins
|
|
80
|
-
top?: string;
|
|
81
|
-
bottom?: string;
|
|
82
|
-
left?: string;
|
|
83
|
-
right?: string;
|
|
84
|
-
};
|
|
85
|
-
displayHeaderFooter?: boolean;
|
|
86
|
-
headerTemplate?: string;
|
|
87
|
-
footerTemplate?: string;
|
|
88
|
-
printBackground?: boolean;
|
|
89
|
-
scale?: number;
|
|
90
|
-
}
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
## Performance
|
|
94
|
-
|
|
95
|
-
Generating 100 PDFs:
|
|
96
|
-
|
|
97
|
-
| Method | Time | Memory |
|
|
98
|
-
| ----------------- | ----- | ------ |
|
|
99
|
-
| Vanilla Puppeteer | ~180s | ~2GB |
|
|
100
|
-
| **This Package** | ~18s | ~600MB |
|
|
101
|
-
|
|
102
|
-
## Why use this vs Puppeteer directly?
|
|
103
|
-
|
|
104
|
-
Direct Puppeteer usage:
|
|
105
|
-
|
|
106
|
-
```typescript
|
|
107
|
-
// š¢ Slow and memory-hungry
|
|
108
|
-
const browser = await puppeteer.launch(); // 1-2 seconds!
|
|
109
|
-
const page = await browser.newPage();
|
|
110
|
-
await page.setContent(html);
|
|
111
|
-
const pdf = await page.pdf();
|
|
112
|
-
await browser.close();
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
This package:
|
|
116
|
-
|
|
117
|
-
```typescript
|
|
118
|
-
// š Fast and efficient
|
|
119
|
-
const pdf = await generator.generate({ html }); // 100-300ms
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
## Examples
|
|
123
|
-
|
|
124
|
-
See the [examples](./examples) directory for more use cases.
|
|
125
|
-
|
|
126
|
-
## License
|
|
127
|
-
|
|
128
|
-
MIT
|
|
129
|
-
|
|
130
|
-
## Need More Features?
|
|
131
|
-
|
|
132
|
-
Check out our [managed API service](https://yourservice.com) for:
|
|
133
|
-
|
|
134
|
-
- š Pre-built invoice/receipt/certificate templates
|
|
135
|
-
- āļø Cloud storage and CDN delivery
|
|
136
|
-
- šØ Visual template editor
|
|
137
|
-
- š Usage analytics dashboard
|
|
138
|
-
- š Webhook notifications
|
|
139
|
-
- š³ No infrastructure management
|
|
1
|
+
# papercraft-js
|
|
2
|
+
|
|
3
|
+
ā” Fast, production-ready PDF generation from HTML using optimized Chrome pool.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- š **10x faster** than vanilla Puppeteer - Pre-warmed browser pool
|
|
8
|
+
- š¾ **70% less memory** - Optimized resource management
|
|
9
|
+
- š§ **Simple API** - No Chrome configuration needed
|
|
10
|
+
- ā” **Production ready** - Battle-tested patterns
|
|
11
|
+
- š¦ **TypeScript** - Full type safety
|
|
12
|
+
- šÆ **Zero dependencies** - Only peer dep on Playwright
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install papercraft-js playwright
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### Simple (one-off PDF)
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { generatePDF } from "papercraft-js";
|
|
26
|
+
import { writeFileSync } from "fs";
|
|
27
|
+
|
|
28
|
+
const pdf = await generatePDF({
|
|
29
|
+
html: "<h1>Hello World!</h1>",
|
|
30
|
+
css: "h1 { color: blue; }",
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
writeFileSync("output.pdf", pdf);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Advanced (with browser pool)
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { PDFGenerator } from "papercraft-js";
|
|
40
|
+
|
|
41
|
+
const generator = new PDFGenerator({
|
|
42
|
+
maxBrowsers: 3,
|
|
43
|
+
maxPagesPerBrowser: 5,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
await generator.initialize();
|
|
47
|
+
|
|
48
|
+
// Generate multiple PDFs efficiently
|
|
49
|
+
const pdf1 = await generator.generate({ html: "..." });
|
|
50
|
+
const pdf2 = await generator.generate({ html: "..." });
|
|
51
|
+
|
|
52
|
+
await generator.close();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
### `generatePDF(options)`
|
|
58
|
+
|
|
59
|
+
Simple function for one-off PDF generation.
|
|
60
|
+
|
|
61
|
+
### `new PDFGenerator(options)`
|
|
62
|
+
|
|
63
|
+
Create a generator with browser pool for better performance.
|
|
64
|
+
|
|
65
|
+
**Options:**
|
|
66
|
+
|
|
67
|
+
- `maxBrowsers` - Maximum browser instances (default: 3)
|
|
68
|
+
- `maxPagesPerBrowser` - Max pages per browser (default: 5)
|
|
69
|
+
- `enablePool` - Use browser pool (default: true)
|
|
70
|
+
|
|
71
|
+
### PDF Options
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
{
|
|
75
|
+
html: string; // Required HTML content
|
|
76
|
+
css?: string; // Optional CSS
|
|
77
|
+
format?: 'A4' | 'Letter'; // Page format
|
|
78
|
+
landscape?: boolean; // Orientation
|
|
79
|
+
margin?: { // Page margins
|
|
80
|
+
top?: string;
|
|
81
|
+
bottom?: string;
|
|
82
|
+
left?: string;
|
|
83
|
+
right?: string;
|
|
84
|
+
};
|
|
85
|
+
displayHeaderFooter?: boolean;
|
|
86
|
+
headerTemplate?: string;
|
|
87
|
+
footerTemplate?: string;
|
|
88
|
+
printBackground?: boolean;
|
|
89
|
+
scale?: number;
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Performance
|
|
94
|
+
|
|
95
|
+
Generating 100 PDFs:
|
|
96
|
+
|
|
97
|
+
| Method | Time | Memory |
|
|
98
|
+
| ----------------- | ----- | ------ |
|
|
99
|
+
| Vanilla Puppeteer | ~180s | ~2GB |
|
|
100
|
+
| **This Package** | ~18s | ~600MB |
|
|
101
|
+
|
|
102
|
+
## Why use this vs Puppeteer directly?
|
|
103
|
+
|
|
104
|
+
Direct Puppeteer usage:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
// š¢ Slow and memory-hungry
|
|
108
|
+
const browser = await puppeteer.launch(); // 1-2 seconds!
|
|
109
|
+
const page = await browser.newPage();
|
|
110
|
+
await page.setContent(html);
|
|
111
|
+
const pdf = await page.pdf();
|
|
112
|
+
await browser.close();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
This package:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
// š Fast and efficient
|
|
119
|
+
const pdf = await generator.generate({ html }); // 100-300ms
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Examples
|
|
123
|
+
|
|
124
|
+
See the [examples](./examples) directory for more use cases.
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT
|
|
129
|
+
|
|
130
|
+
## Need More Features?
|
|
131
|
+
|
|
132
|
+
Check out our [managed API service](https://yourservice.com) for:
|
|
133
|
+
|
|
134
|
+
- š Pre-built invoice/receipt/certificate templates
|
|
135
|
+
- āļø Cloud storage and CDN delivery
|
|
136
|
+
- šØ Visual template editor
|
|
137
|
+
- š Usage analytics dashboard
|
|
138
|
+
- š Webhook notifications
|
|
139
|
+
- š³ No infrastructure management
|
package/dist/bin/papercraft.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../bin/papercraft.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { generatePDF } from '../src/index.js';\nimport { readFileSync, writeFileSync } from 'fs';\nimport { resolve } from 'path';\n\ninterface CLIArgs {\n input?: string;\n output?: string;\n format?: 'A4' | 'Letter' | 'Legal' | 'A3';\n landscape?: boolean;\n help?: boolean;\n}\n\nfunction parseArgs(): CLIArgs {\n const args = process.argv.slice(2);\n const parsed: CLIArgs = {};\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i];\n\n if (arg === '--help' || arg === '-h') {\n parsed.help = true;\n } else if (arg === '--input' || arg === '-i') {\n if (i + 1 < args.length) {\n parsed.input = args[++i];\n }\n } else if (arg === '--output' || arg === '-o') {\n if (i + 1 < args.length) {\n parsed.output = args[++i];\n }\n } else if (arg === '--format' || arg === '-f') {\n if (i + 1 < args.length) {\n const format = args[++i];\n if (format === 'A4' || format === 'Letter' || format === 'Legal' || format === 'A3') {\n parsed.format = format;\n }\n }\n } else if (arg === '--landscape' || arg === '-l') {\n parsed.landscape = true;\n }\n }\n\n return parsed;\n}\n\nfunction showHelp(): void {\n console.log(`\nš Papercraft - Fast PDF generation from HTML\n\nUsage:\n papercraft -i <input.html> -o <output.pdf>\n\nOptions:\n -i, --input <file> Input HTML file (required)\n -o, --output <file> Output PDF file (default: output.pdf)\n -f, --format <format> Page format: A4, Letter, Legal, A3 (default: A4)\n -l, --landscape Use landscape orientation\n -h, --help Show this help message\n\nExamples:\n papercraft -i invoice.html -o invoice.pdf\n papercraft -i report.html -o report.pdf --landscape\n papercraft -i page.html -f Letter\n\nDocumentation:\n https://github.com/
|
|
1
|
+
{"version":3,"sources":["../../bin/papercraft.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { generatePDF } from '../src/index.js';\nimport { readFileSync, writeFileSync } from 'fs';\nimport { resolve } from 'path';\n\ninterface CLIArgs {\n input?: string;\n output?: string;\n format?: 'A4' | 'Letter' | 'Legal' | 'A3';\n landscape?: boolean;\n help?: boolean;\n}\n\nfunction parseArgs(): CLIArgs {\n const args = process.argv.slice(2);\n const parsed: CLIArgs = {};\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i];\n\n if (arg === '--help' || arg === '-h') {\n parsed.help = true;\n } else if (arg === '--input' || arg === '-i') {\n if (i + 1 < args.length) {\n parsed.input = args[++i];\n }\n } else if (arg === '--output' || arg === '-o') {\n if (i + 1 < args.length) {\n parsed.output = args[++i];\n }\n } else if (arg === '--format' || arg === '-f') {\n if (i + 1 < args.length) {\n const format = args[++i];\n if (format === 'A4' || format === 'Letter' || format === 'Legal' || format === 'A3') {\n parsed.format = format;\n }\n }\n } else if (arg === '--landscape' || arg === '-l') {\n parsed.landscape = true;\n }\n }\n\n return parsed;\n}\n\nfunction showHelp(): void {\n console.log(`\nš Papercraft - Fast PDF generation from HTML\n\nUsage:\n papercraft -i <input.html> -o <output.pdf>\n\nOptions:\n -i, --input <file> Input HTML file (required)\n -o, --output <file> Output PDF file (default: output.pdf)\n -f, --format <format> Page format: A4, Letter, Legal, A3 (default: A4)\n -l, --landscape Use landscape orientation\n -h, --help Show this help message\n\nExamples:\n papercraft -i invoice.html -o invoice.pdf\n papercraft -i report.html -o report.pdf --landscape\n papercraft -i page.html -f Letter\n\nDocumentation:\n https://github.com/sidgaikwad/papercraft\n `);\n}\n\nasync function main(): Promise<void> {\n const args = parseArgs();\n\n if (args.help || !args.input) {\n showHelp();\n process.exit(args.help ? 0 : 1);\n }\n\n const inputPath = args.input;\n const outputPath = args.output ?? 'output.pdf';\n\n try {\n console.log('š Reading HTML file...');\n const htmlPath = resolve(process.cwd(), inputPath);\n const html = readFileSync(htmlPath, 'utf-8');\n\n console.log('šØ Generating PDF...');\n const startTime = Date.now();\n\n const pdf = await generatePDF({\n html,\n format: args.format ?? 'A4',\n landscape: args.landscape ?? false,\n });\n\n const outputFilePath = resolve(process.cwd(), outputPath);\n writeFileSync(outputFilePath, pdf);\n\n const duration = Date.now() - startTime;\n const size = (pdf.length / 1024).toFixed(2);\n\n console.log(`ā
PDF generated successfully!`);\n console.log(` File: ${outputFilePath}`);\n console.log(` Size: ${size} KB`);\n console.log(` Time: ${duration}ms`);\n } catch (error) {\n console.error('ā Error generating PDF:', error);\n process.exit(1);\n }\n}\n\nmain().catch((error: unknown) => {\n console.error('ā Fatal error:', error);\n process.exit(1);\n});\n"],"mappings":";;;;AAEA,iBAA4B;AAC5B,gBAA4C;AAC5C,kBAAwB;AAUxB,SAAS,YAAqB;AAC5B,QAAM,OAAO,QAAQ,KAAK,MAAM,CAAC;AACjC,QAAM,SAAkB,CAAC;AAEzB,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;AACpC,UAAM,MAAM,KAAK,CAAC;AAElB,QAAI,QAAQ,YAAY,QAAQ,MAAM;AACpC,aAAO,OAAO;AAAA,IAChB,WAAW,QAAQ,aAAa,QAAQ,MAAM;AAC5C,UAAI,IAAI,IAAI,KAAK,QAAQ;AACvB,eAAO,QAAQ,KAAK,EAAE,CAAC;AAAA,MACzB;AAAA,IACF,WAAW,QAAQ,cAAc,QAAQ,MAAM;AAC7C,UAAI,IAAI,IAAI,KAAK,QAAQ;AACvB,eAAO,SAAS,KAAK,EAAE,CAAC;AAAA,MAC1B;AAAA,IACF,WAAW,QAAQ,cAAc,QAAQ,MAAM;AAC7C,UAAI,IAAI,IAAI,KAAK,QAAQ;AACvB,cAAM,SAAS,KAAK,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tC,SAAS,OAAO;AACd,YAAQ,MAAM,gCAA2B,KAAK;AAC9C,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF;AAEA,KAAK,EAAE,MAAM,CAAC,UAAmB;AAC/B,UAAQ,MAAM,uBAAkB,KAAK;AACrC,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "papercraft-js",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "ā” Craft beautiful PDFs from HTML using optimized Chrome pool",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -58,12 +58,12 @@
|
|
|
58
58
|
"license": "MIT",
|
|
59
59
|
"repository": {
|
|
60
60
|
"type": "git",
|
|
61
|
-
"url": "https://github.com/
|
|
61
|
+
"url": "https://github.com/sidgaikwad/papercraft"
|
|
62
62
|
},
|
|
63
63
|
"bugs": {
|
|
64
|
-
"url": "https://github.com/
|
|
64
|
+
"url": "https://github.com/sidgaikwad/papercraft/issues"
|
|
65
65
|
},
|
|
66
|
-
"homepage": "https://github.com/
|
|
66
|
+
"homepage": "https://github.com/sidgaikwad/papercraft#readme",
|
|
67
67
|
"engines": {
|
|
68
68
|
"node": ">=18.0.0"
|
|
69
69
|
},
|