@xenterprises/fastify-xpdf 1.0.0 → 1.0.1
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/.dockerignore +62 -0
- package/.env.example +116 -6
- package/index.d.ts +267 -0
- package/package.json +1 -1
package/.dockerignore
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Git
|
|
2
|
+
.git
|
|
3
|
+
.gitignore
|
|
4
|
+
.gitattributes
|
|
5
|
+
|
|
6
|
+
# Node modules (will be installed in container)
|
|
7
|
+
node_modules
|
|
8
|
+
npm-debug.log*
|
|
9
|
+
yarn-debug.log*
|
|
10
|
+
yarn-error.log*
|
|
11
|
+
lerna-debug.log*
|
|
12
|
+
|
|
13
|
+
# Dependencies
|
|
14
|
+
.npm
|
|
15
|
+
.eslintcache
|
|
16
|
+
*.tsbuildinfo
|
|
17
|
+
|
|
18
|
+
# Temporary files
|
|
19
|
+
.DS_Store
|
|
20
|
+
Thumbs.db
|
|
21
|
+
*.tmp
|
|
22
|
+
.env.local
|
|
23
|
+
.env.*.local
|
|
24
|
+
|
|
25
|
+
# IDE
|
|
26
|
+
.vscode
|
|
27
|
+
.idea
|
|
28
|
+
*.swp
|
|
29
|
+
*.swo
|
|
30
|
+
*.swn
|
|
31
|
+
*~
|
|
32
|
+
|
|
33
|
+
# Test & Coverage
|
|
34
|
+
coverage
|
|
35
|
+
.nyc_output
|
|
36
|
+
test
|
|
37
|
+
*.test.js
|
|
38
|
+
|
|
39
|
+
# Build artifacts (if any)
|
|
40
|
+
dist
|
|
41
|
+
build
|
|
42
|
+
out
|
|
43
|
+
|
|
44
|
+
# Docker files (not needed in image)
|
|
45
|
+
Dockerfile*
|
|
46
|
+
docker-compose*.yml
|
|
47
|
+
.dockerignore
|
|
48
|
+
|
|
49
|
+
# CI/CD
|
|
50
|
+
.github
|
|
51
|
+
.gitlab-ci.yml
|
|
52
|
+
|
|
53
|
+
# Documentation (optional - include if you want docs in image)
|
|
54
|
+
# *.md
|
|
55
|
+
|
|
56
|
+
# Example/demo files
|
|
57
|
+
examples
|
|
58
|
+
demo
|
|
59
|
+
|
|
60
|
+
# Logs
|
|
61
|
+
logs
|
|
62
|
+
*.log
|
package/.env.example
CHANGED
|
@@ -1,11 +1,121 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ============================================================================
|
|
2
|
+
# xPDF Fastify Plugin - Environment Configuration
|
|
3
|
+
# ============================================================================
|
|
4
|
+
# This file defines environment variables for the xPDF module.
|
|
5
|
+
# Copy this to .env and update values for your environment.
|
|
6
|
+
# ============================================================================
|
|
7
|
+
|
|
8
|
+
# ============================================================================
|
|
9
|
+
# SERVER CONFIGURATION
|
|
10
|
+
# ============================================================================
|
|
11
|
+
|
|
12
|
+
# Server port (default: 3000)
|
|
13
|
+
PORT=3000
|
|
14
|
+
|
|
15
|
+
# Node environment (development | production | test)
|
|
16
|
+
NODE_ENV=development
|
|
17
|
+
|
|
18
|
+
# Server hostname/domain (used in example server)
|
|
19
|
+
DOMAIN=http://localhost:3000
|
|
20
|
+
|
|
21
|
+
# ============================================================================
|
|
22
|
+
# PUPPETEER CONFIGURATION
|
|
23
|
+
# ============================================================================
|
|
24
|
+
# Optional - uses sensible defaults if not provided
|
|
25
|
+
|
|
26
|
+
# Run in headless mode (true | false)
|
|
27
|
+
# Default: true
|
|
28
|
+
# Set to false for debugging (displays browser window)
|
|
2
29
|
PUPPETEER_HEADLESS=true
|
|
3
|
-
PUPPETEER_ARGS=--no-sandbox,--disable-setuid-sandbox
|
|
4
30
|
|
|
5
|
-
#
|
|
31
|
+
# Chrome/Chromium launch arguments (comma-separated)
|
|
32
|
+
# Default: --no-sandbox,--disable-setuid-sandbox
|
|
33
|
+
# Examples:
|
|
34
|
+
# Production: --no-sandbox,--disable-dev-shm-usage
|
|
35
|
+
# Docker: --no-sandbox,--disable-gpu
|
|
36
|
+
# PUPPETEER_ARGS=--no-sandbox,--disable-setuid-sandbox
|
|
37
|
+
|
|
38
|
+
# Path to Chromium/Chrome executable
|
|
39
|
+
# Leave empty to auto-detect or use system-installed browser
|
|
40
|
+
# Example: /usr/bin/chromium-browser
|
|
41
|
+
# PUPPETEER_EXECUTABLE_PATH=
|
|
42
|
+
|
|
43
|
+
# Timeout for page operations in milliseconds (default: 30000)
|
|
44
|
+
# PUPPETEER_TIMEOUT=30000
|
|
45
|
+
|
|
46
|
+
# ============================================================================
|
|
47
|
+
# xPDF PLUGIN CONFIGURATION
|
|
48
|
+
# ============================================================================
|
|
49
|
+
|
|
50
|
+
# Default PDF page format
|
|
51
|
+
# Options: A4, Letter, A3, A5, Tabloid, Ledger
|
|
52
|
+
# Default: A4
|
|
6
53
|
PDF_DEFAULT_FORMAT=A4
|
|
7
|
-
|
|
54
|
+
|
|
55
|
+
# Default page margins (in CSS units: cm, mm, in, px)
|
|
56
|
+
# Default: 1cm on all sides
|
|
57
|
+
# PDF_MARGIN_TOP=1cm
|
|
58
|
+
# PDF_MARGIN_RIGHT=1cm
|
|
59
|
+
# PDF_MARGIN_BOTTOM=1cm
|
|
60
|
+
# PDF_MARGIN_LEFT=1cm
|
|
61
|
+
|
|
62
|
+
# Print background graphics and colors
|
|
63
|
+
# Default: true
|
|
64
|
+
# PDF_PRINT_BACKGROUND=true
|
|
65
|
+
|
|
66
|
+
# ============================================================================
|
|
67
|
+
# xSTORAGE INTEGRATION (Optional)
|
|
68
|
+
# ============================================================================
|
|
69
|
+
# Only required if PDF_USE_STORAGE=true
|
|
70
|
+
|
|
71
|
+
# Enable saving PDFs to xStorage (S3-compatible storage)
|
|
72
|
+
# Default: false
|
|
73
|
+
# Set to true to automatically save generated/merged PDFs
|
|
8
74
|
PDF_USE_STORAGE=false
|
|
9
75
|
|
|
10
|
-
#
|
|
11
|
-
|
|
76
|
+
# Default folder in storage for saved PDFs
|
|
77
|
+
# Default: pdfs
|
|
78
|
+
PDF_DEFAULT_FOLDER=pdfs
|
|
79
|
+
|
|
80
|
+
# S3-Compatible Storage Endpoint
|
|
81
|
+
# Example: https://s3.amazonaws.com or https://minio.example.com
|
|
82
|
+
# STORAGE_ENDPOINT=https://s3.amazonaws.com
|
|
83
|
+
|
|
84
|
+
# AWS Region or storage provider region
|
|
85
|
+
# Example: us-east-1, us-west-2, auto
|
|
86
|
+
# STORAGE_REGION=us-east-1
|
|
87
|
+
|
|
88
|
+
# Access Key ID for S3-compatible storage
|
|
89
|
+
# STORAGE_ACCESS_KEY_ID=your_access_key
|
|
90
|
+
|
|
91
|
+
# Secret Access Key for S3-compatible storage
|
|
92
|
+
# STORAGE_SECRET_ACCESS_KEY=your_secret_key
|
|
93
|
+
|
|
94
|
+
# S3 Bucket name
|
|
95
|
+
# STORAGE_BUCKET=my-pdfs-bucket
|
|
96
|
+
|
|
97
|
+
# Public URL for accessing stored files
|
|
98
|
+
# Used in response URLs when PDF is saved to storage
|
|
99
|
+
# Example: https://cdn.example.com/pdfs or https://my-bucket.s3.amazonaws.com
|
|
100
|
+
# STORAGE_PUBLIC_URL=https://my-bucket.s3.amazonaws.com
|
|
101
|
+
|
|
102
|
+
# ============================================================================
|
|
103
|
+
# LOGGING & MONITORING
|
|
104
|
+
# ============================================================================
|
|
105
|
+
|
|
106
|
+
# Fastify logger level (trace, debug, info, warn, error, fatal)
|
|
107
|
+
LOG_LEVEL=info
|
|
108
|
+
|
|
109
|
+
# Enable request logging
|
|
110
|
+
# LOG_REQUESTS=true
|
|
111
|
+
|
|
112
|
+
# ============================================================================
|
|
113
|
+
# SECURITY & RATE LIMITING (for HTTP endpoints)
|
|
114
|
+
# ============================================================================
|
|
115
|
+
|
|
116
|
+
# Enable HTTPS enforcement (if behind reverse proxy)
|
|
117
|
+
# HTTPS_ONLY=false
|
|
118
|
+
|
|
119
|
+
# Rate limiting: max requests per minute
|
|
120
|
+
# RATE_LIMIT_WINDOW=1m
|
|
121
|
+
# RATE_LIMIT_MAX=100
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xPDF - Fastify Plugin for PDF Generation and Manipulation
|
|
3
|
+
* TypeScript Type Definitions
|
|
4
|
+
*
|
|
5
|
+
* @module @xenterprises/fastify-xpdf
|
|
6
|
+
* @version 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { FastifyPluginAsync, FastifyInstance } from 'fastify';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* PDF Generation Options
|
|
13
|
+
*/
|
|
14
|
+
export interface PDFGenerationOptions {
|
|
15
|
+
/** Output filename (auto-generated if not provided) */
|
|
16
|
+
filename?: string;
|
|
17
|
+
|
|
18
|
+
/** Page format: A4, Letter, A3, A5, Tabloid, Ledger */
|
|
19
|
+
format?: 'A4' | 'Letter' | 'A3' | 'A5' | 'Tabloid' | 'Ledger';
|
|
20
|
+
|
|
21
|
+
/** Landscape orientation */
|
|
22
|
+
landscape?: boolean;
|
|
23
|
+
|
|
24
|
+
/** Page margins */
|
|
25
|
+
margin?: {
|
|
26
|
+
top?: string;
|
|
27
|
+
right?: string;
|
|
28
|
+
bottom?: string;
|
|
29
|
+
left?: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** Print background graphics and colors */
|
|
33
|
+
printBackground?: boolean;
|
|
34
|
+
|
|
35
|
+
/** Save to xStorage after generation */
|
|
36
|
+
saveToStorage?: boolean;
|
|
37
|
+
|
|
38
|
+
/** Storage folder path */
|
|
39
|
+
folder?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* PDF Generation Result
|
|
44
|
+
*/
|
|
45
|
+
export interface PDFGenerationResult {
|
|
46
|
+
/** PDF buffer */
|
|
47
|
+
buffer: Buffer;
|
|
48
|
+
|
|
49
|
+
/** Generated filename */
|
|
50
|
+
filename: string;
|
|
51
|
+
|
|
52
|
+
/** File size in bytes */
|
|
53
|
+
size: number;
|
|
54
|
+
|
|
55
|
+
/** Storage key (only if saved to storage) */
|
|
56
|
+
storageKey?: string;
|
|
57
|
+
|
|
58
|
+
/** Public URL (only if saved to storage) */
|
|
59
|
+
url?: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* PDF Merge Options
|
|
64
|
+
*/
|
|
65
|
+
export interface PDFMergeOptions extends PDFGenerationOptions {
|
|
66
|
+
// Inherits from PDFGenerationOptions
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* PDF Merge Result
|
|
71
|
+
*/
|
|
72
|
+
export interface PDFMergeResult extends PDFGenerationResult {
|
|
73
|
+
/** Total page count in merged PDF */
|
|
74
|
+
pageCount: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* PDF Form Field
|
|
79
|
+
*/
|
|
80
|
+
export interface PDFFormField {
|
|
81
|
+
/** Field name */
|
|
82
|
+
name: string;
|
|
83
|
+
|
|
84
|
+
/** Field type: text, checkbox, radio, etc. */
|
|
85
|
+
type: string;
|
|
86
|
+
|
|
87
|
+
/** Current field value */
|
|
88
|
+
value: any;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* PDF Form Filling Options
|
|
93
|
+
*/
|
|
94
|
+
export interface PDFFormFillingOptions extends PDFGenerationOptions {
|
|
95
|
+
/** Flatten form after filling (make non-editable) */
|
|
96
|
+
flatten?: boolean;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* PDF Form Filling Result
|
|
101
|
+
*/
|
|
102
|
+
export interface PDFFormFillingResult extends PDFGenerationResult {
|
|
103
|
+
// Inherits from PDFGenerationResult
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Plugin Configuration Options
|
|
108
|
+
*/
|
|
109
|
+
export interface XPDFPluginOptions {
|
|
110
|
+
/** Enable saving to xStorage automatically */
|
|
111
|
+
useStorage?: boolean;
|
|
112
|
+
|
|
113
|
+
/** Default storage folder */
|
|
114
|
+
defaultFolder?: string;
|
|
115
|
+
|
|
116
|
+
/** Puppeteer launch options */
|
|
117
|
+
headless?: boolean;
|
|
118
|
+
|
|
119
|
+
/** Chrome/Chromium launch arguments */
|
|
120
|
+
args?: string[];
|
|
121
|
+
|
|
122
|
+
/** Executable path for Chrome/Chromium */
|
|
123
|
+
executablePath?: string;
|
|
124
|
+
|
|
125
|
+
/** Default page format */
|
|
126
|
+
format?: string;
|
|
127
|
+
|
|
128
|
+
/** Print background by default */
|
|
129
|
+
printBackground?: boolean;
|
|
130
|
+
|
|
131
|
+
/** Default page margins */
|
|
132
|
+
margin?: {
|
|
133
|
+
top?: string;
|
|
134
|
+
right?: string;
|
|
135
|
+
bottom?: string;
|
|
136
|
+
left?: string;
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Helper Functions
|
|
142
|
+
*/
|
|
143
|
+
export namespace helpers {
|
|
144
|
+
/**
|
|
145
|
+
* Generate unique PDF filename
|
|
146
|
+
* @param baseName Base filename without extension
|
|
147
|
+
* @returns Unique filename with timestamp
|
|
148
|
+
*/
|
|
149
|
+
function generatePdfFilename(baseName?: string): string;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Validate PDF buffer
|
|
153
|
+
* @param buffer Buffer to validate
|
|
154
|
+
* @returns True if valid PDF
|
|
155
|
+
*/
|
|
156
|
+
function isValidPdfBuffer(buffer: Buffer): boolean;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Get PDF metadata
|
|
160
|
+
* @param buffer PDF buffer
|
|
161
|
+
* @returns Metadata including page count and file size
|
|
162
|
+
*/
|
|
163
|
+
function getPdfMetadata(buffer: Buffer): Promise<{
|
|
164
|
+
pageCount: number;
|
|
165
|
+
fileSize: number;
|
|
166
|
+
}>;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Format PDF options with defaults
|
|
170
|
+
* @param options User options
|
|
171
|
+
* @param defaults Default options
|
|
172
|
+
* @returns Merged options
|
|
173
|
+
*/
|
|
174
|
+
function formatPdfOptions(options: any, defaults: any): any;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* xPDF Service Methods (available on fastify.xPDF)
|
|
179
|
+
*/
|
|
180
|
+
export interface XPDFService {
|
|
181
|
+
/**
|
|
182
|
+
* Generate PDF from HTML
|
|
183
|
+
* @param html HTML content
|
|
184
|
+
* @param options Generation options
|
|
185
|
+
* @returns PDF generation result
|
|
186
|
+
*/
|
|
187
|
+
generateFromHtml(
|
|
188
|
+
html: string,
|
|
189
|
+
options?: PDFGenerationOptions
|
|
190
|
+
): Promise<PDFGenerationResult>;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Generate PDF from Markdown
|
|
194
|
+
* @param markdown Markdown content
|
|
195
|
+
* @param options Generation options
|
|
196
|
+
* @returns PDF generation result
|
|
197
|
+
*/
|
|
198
|
+
generateFromMarkdown(
|
|
199
|
+
markdown: string,
|
|
200
|
+
options?: PDFGenerationOptions
|
|
201
|
+
): Promise<PDFGenerationResult>;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Fill PDF form fields
|
|
205
|
+
* @param pdfBuffer Source PDF buffer
|
|
206
|
+
* @param fieldValues Key-value pairs of field names and values
|
|
207
|
+
* @param options Filling options
|
|
208
|
+
* @returns Filled PDF result
|
|
209
|
+
*/
|
|
210
|
+
fillForm(
|
|
211
|
+
pdfBuffer: Buffer,
|
|
212
|
+
fieldValues: Record<string, any>,
|
|
213
|
+
options?: PDFFormFillingOptions
|
|
214
|
+
): Promise<PDFFormFillingResult>;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* List all form fields in a PDF
|
|
218
|
+
* @param pdfBuffer Source PDF buffer
|
|
219
|
+
* @returns Array of form fields
|
|
220
|
+
*/
|
|
221
|
+
listFormFields(pdfBuffer: Buffer): Promise<PDFFormField[]>;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Merge multiple PDFs into one
|
|
225
|
+
* @param pdfBuffers Array of PDF buffers
|
|
226
|
+
* @param options Merge options
|
|
227
|
+
* @returns Merged PDF result
|
|
228
|
+
*/
|
|
229
|
+
mergePDFs(
|
|
230
|
+
pdfBuffers: Buffer[],
|
|
231
|
+
options?: PDFMergeOptions
|
|
232
|
+
): Promise<PDFMergeResult>;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Fastify Instance with xPDF Decoration
|
|
237
|
+
*/
|
|
238
|
+
declare module 'fastify' {
|
|
239
|
+
interface FastifyInstance {
|
|
240
|
+
/** xPDF service methods */
|
|
241
|
+
xPDF: XPDFService;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* xPDF Plugin
|
|
247
|
+
* Registers PDF generation and manipulation capabilities
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* import Fastify from 'fastify';
|
|
252
|
+
* import xPDF from '@xenterprises/fastify-xpdf';
|
|
253
|
+
*
|
|
254
|
+
* const fastify = Fastify();
|
|
255
|
+
*
|
|
256
|
+
* await fastify.register(xPDF, {
|
|
257
|
+
* useStorage: true,
|
|
258
|
+
* defaultFolder: 'pdfs'
|
|
259
|
+
* });
|
|
260
|
+
*
|
|
261
|
+
* // Use xPDF methods
|
|
262
|
+
* const result = await fastify.xPDF.generateFromHtml('<h1>Hello</h1>');
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
declare const xPDF: FastifyPluginAsync<XPDFPluginOptions>;
|
|
266
|
+
|
|
267
|
+
export default xPDF;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xenterprises/fastify-xpdf",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"description": "Fastify plugin for PDF generation and manipulation. Convert HTML/Markdown to PDF, fill forms, and merge PDFs.",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"exports": {
|