@turbodocx/html-to-docx 1.16.0 → 1.18.0
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 +155 -0
- package/dist/html-to-docx.esm.js +2 -2
- package/dist/html-to-docx.umd.js +2 -2
- package/index.d.ts +14 -0
- package/package.json +9 -6
package/README.md
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
@turbodocx/html-to-docx
|
|
4
4
|
====================
|
|
5
5
|
[![NPM Version][npm-image]][npm-url]
|
|
6
|
+
[](https://github.com/TurboDocx/html-to-docx/actions/workflows/codeql.yml)
|
|
6
7
|
[](https://github.com/turbodocx/html-to-docx)
|
|
7
8
|
[](https://typescript.org)
|
|
8
9
|
[](https://discord.gg/NYKwz4BcpX)
|
|
@@ -88,6 +89,22 @@ async function withOptions() {
|
|
|
88
89
|
});
|
|
89
90
|
}
|
|
90
91
|
|
|
92
|
+
// With image processing options
|
|
93
|
+
async function withImageOptions() {
|
|
94
|
+
const htmlWithImages = `<div>
|
|
95
|
+
<img src="https://example.com/image.jpg" alt="Example">
|
|
96
|
+
</div>`;
|
|
97
|
+
|
|
98
|
+
const docx = await HtmlToDocx(htmlWithImages, null, {
|
|
99
|
+
imageProcessing: {
|
|
100
|
+
maxRetries: 3, // Retry failed image downloads up to 3 times
|
|
101
|
+
verboseLogging: true, // Enable detailed logging for debugging
|
|
102
|
+
downloadTimeout: 10000, // 10 second timeout per download attempt
|
|
103
|
+
maxImageSize: 5242880 // 5MB max image size
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
91
108
|
// With all parameters
|
|
92
109
|
async function complete() {
|
|
93
110
|
const headerHtml = "<p>Document Header</p>";
|
|
@@ -219,6 +236,20 @@ full fledged examples can be found under `example/`
|
|
|
219
236
|
- `direction` <?[String]> text direction for RTL (right-to-left) languages. Set to `'rtl'` for Arabic, Hebrew, etc. Defaults to `'ltr'`.
|
|
220
237
|
- `preProcessing` <?[Object]>
|
|
221
238
|
- `skipHTMLMinify` <?[Boolean]> flag to skip minification of HTML. Defaults to `false`.
|
|
239
|
+
- `imageProcessing` <?[Object]>
|
|
240
|
+
- `maxRetries` <?[Number]> maximum number of retry attempts for failed image downloads. Defaults to `2`.
|
|
241
|
+
- `verboseLogging` <?[Boolean]> flag to enable detailed logging of image processing operations. Defaults to `false`.
|
|
242
|
+
- `downloadTimeout` <?[Number]> timeout in milliseconds for each image download attempt. Defaults to `5000` (5 seconds).
|
|
243
|
+
- `maxImageSize` <?[Number]> maximum allowed image size in bytes. Defaults to `10485760` (10MB).
|
|
244
|
+
- `retryDelayBase` <?[Number]> base delay in milliseconds for exponential backoff between retries. Defaults to `500` (500ms).
|
|
245
|
+
- `minTimeout` <?[Number]> minimum timeout in milliseconds. Defaults to `1000` (1 second).
|
|
246
|
+
- `maxTimeout` <?[Number]> maximum timeout in milliseconds. Defaults to `30000` (30 seconds).
|
|
247
|
+
- `minImageSize` <?[Number]> minimum image size in bytes. Defaults to `1024` (1KB).
|
|
248
|
+
- `maxCacheSize` <?[Number]> maximum total cache size in bytes (LRU cache limit to prevent OOM). Defaults to `20971520` (20MB).
|
|
249
|
+
- `maxCacheEntries` <?[Number]> maximum number of unique images in cache (LRU eviction). Defaults to `100`.
|
|
250
|
+
- `svgHandling` <?[String]> strategy for handling SVG images. Defaults to `'convert'`. Options:
|
|
251
|
+
- `'convert'` - Converts SVG to PNG for maximum compatibility with all Word versions (requires `sharp` package)
|
|
252
|
+
- `'native'` - Embeds SVG natively for Office 2019+ (preserves vector quality)
|
|
222
253
|
- `footerHTMLString` <[String]> clean html string equivalent of footer. Defaults to `<p></p>` if footer flag is `true`.
|
|
223
254
|
|
|
224
255
|
### Returns
|
|
@@ -252,6 +283,130 @@ Also you could add attribute `data-start="n"` to start the numbering from the n-
|
|
|
252
283
|
`<ol data-start="2">` will start the numbering from ( B. b. II. ii. 2. )
|
|
253
284
|
|
|
254
285
|
|
|
286
|
+
## SVG Image Support
|
|
287
|
+
|
|
288
|
+
The library provides comprehensive SVG image support with two strategies to fit your needs:
|
|
289
|
+
|
|
290
|
+
### Installation & Package Size
|
|
291
|
+
|
|
292
|
+
The library supports SVG images with [`sharp`](https://sharp.pixelplumbing.com/) as an optional peer dependency for high-quality SVG→PNG conversion.
|
|
293
|
+
|
|
294
|
+
**Basic Installation** (Lightweight):
|
|
295
|
+
```bash
|
|
296
|
+
npm install @turbodocx/html-to-docx
|
|
297
|
+
```
|
|
298
|
+
- **Package size**: ~2.8MB (sharp not included)
|
|
299
|
+
- **Compatibility**: SVGs embedded natively - requires Office 2019+ or Microsoft 365
|
|
300
|
+
- **Best for**: Modern-only environments or size-constrained deployments (Lambda, Edge)
|
|
301
|
+
- **Auto-fallback**: Library automatically uses native SVG mode when sharp is unavailable
|
|
302
|
+
|
|
303
|
+
**Full Installation** (Maximum Compatibility - Recommended):
|
|
304
|
+
```bash
|
|
305
|
+
npm install @turbodocx/html-to-docx sharp
|
|
306
|
+
```
|
|
307
|
+
- **Base package**: ~2.8MB
|
|
308
|
+
- **With sharp**: Additional ~34MB (platform-specific native binaries)
|
|
309
|
+
- **Compatibility**: Converts SVGs to PNG - works with all Word versions (2007+)
|
|
310
|
+
- **Best for**: Production applications requiring broad compatibility
|
|
311
|
+
|
|
312
|
+
#### Why is sharp optional?
|
|
313
|
+
|
|
314
|
+
Sharp is a native Node.js module that provides the best SVG to PNG conversion quality, but adds ~34MB of platform-specific native binaries to your `node_modules`. We've made it an optional peer dependency so you can choose:
|
|
315
|
+
|
|
316
|
+
| Configuration | Install Command | Size | SVG Handling | Word Compatibility | Use Case |
|
|
317
|
+
|--------------|-----------------|------|--------------|-------------------|----------|
|
|
318
|
+
| **Without sharp** (default) | `npm install @turbodocx/html-to-docx` | 2.8MB | Native SVG | Office 2019+ only | Modern environments, Lambda/edge functions |
|
|
319
|
+
| **With sharp** (recommended) | `npm install @turbodocx/html-to-docx sharp` | 2.8MB + 34MB binaries | PNG conversion | All versions (2007+) | Production apps, broad compatibility |
|
|
320
|
+
|
|
321
|
+
The library **gracefully handles both scenarios** - if sharp is unavailable, SVGs are automatically embedded in native format.
|
|
322
|
+
|
|
323
|
+
### 1. Convert to PNG (Default - Maximum Compatibility)
|
|
324
|
+
|
|
325
|
+
By default, SVG images are automatically converted to PNG format for maximum compatibility with all Word versions (requires `sharp`):
|
|
326
|
+
|
|
327
|
+
```javascript
|
|
328
|
+
const htmlWithSVG = `
|
|
329
|
+
<div>
|
|
330
|
+
<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48Y2lyY2xlIGN4PSI1MCIgY3k9IjUwIiByPSI0MCIgZmlsbD0iIzM0OThkYiIvPjwvc3ZnPg==" alt="Circle">
|
|
331
|
+
</div>
|
|
332
|
+
`;
|
|
333
|
+
|
|
334
|
+
// Default behavior - SVG converted to PNG
|
|
335
|
+
const docx = await HTMLtoDOCX(htmlWithSVG);
|
|
336
|
+
|
|
337
|
+
// Or explicitly set to convert
|
|
338
|
+
const docx = await HTMLtoDOCX(htmlWithSVG, null, {
|
|
339
|
+
imageProcessing: {
|
|
340
|
+
svgHandling: 'convert' // Converts SVG to PNG (default)
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
**Benefits:**
|
|
346
|
+
- ✅ Works with all Word versions (2007+)
|
|
347
|
+
- ✅ Compatible with Word Online, Google Docs, LibreOffice
|
|
348
|
+
- ✅ No compatibility warnings or errors
|
|
349
|
+
|
|
350
|
+
### 2. Native SVG (Office 2019+ Only)
|
|
351
|
+
|
|
352
|
+
For modern Office environments, you can embed SVG images natively to preserve vector quality:
|
|
353
|
+
|
|
354
|
+
```javascript
|
|
355
|
+
const htmlWithSVG = `
|
|
356
|
+
<div>
|
|
357
|
+
<img src="data:image/svg+xml;base64,..." alt="Vector Graphic">
|
|
358
|
+
</div>
|
|
359
|
+
`;
|
|
360
|
+
|
|
361
|
+
const docx = await HTMLtoDOCX(htmlWithSVG, null, {
|
|
362
|
+
imageProcessing: {
|
|
363
|
+
svgHandling: 'native' // Embed SVG natively (Office 2019+)
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
**Benefits:**
|
|
369
|
+
- ✅ Perfect vector quality at any zoom level
|
|
370
|
+
- ✅ Smaller file sizes for complex graphics
|
|
371
|
+
- ✅ Editable in modern Office applications
|
|
372
|
+
|
|
373
|
+
**Requirements:**
|
|
374
|
+
- Microsoft Office 2019 or later
|
|
375
|
+
- Microsoft 365
|
|
376
|
+
- Word for Mac 2019+
|
|
377
|
+
|
|
378
|
+
**Note:** Older Word versions will show an "unreadable content" error with native SVG. Use `'convert'` mode for backwards compatibility.
|
|
379
|
+
|
|
380
|
+
### Handling SVG Without Sharp
|
|
381
|
+
|
|
382
|
+
If `sharp` is not installed (e.g., using `--no-optional`), the library automatically falls back to native SVG embedding:
|
|
383
|
+
|
|
384
|
+
```javascript
|
|
385
|
+
// Even with svgHandling: 'convert', if sharp unavailable → uses native SVG
|
|
386
|
+
const docx = await HTMLtoDOCX(htmlWithSVG, null, {
|
|
387
|
+
imageProcessing: {
|
|
388
|
+
svgHandling: 'convert', // Tries to convert, falls back to native
|
|
389
|
+
suppressSharpWarning: false, // Set to true to suppress warning when sharp is missing
|
|
390
|
+
verboseLogging: true // Shows: "Sharp not available, using native SVG"
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
**No crashes, no errors** - the library detects sharp availability at runtime and adjusts automatically:
|
|
396
|
+
|
|
397
|
+
```bash
|
|
398
|
+
# With sharp installed
|
|
399
|
+
✅ SVG → PNG conversion → Works in Word 2007+
|
|
400
|
+
|
|
401
|
+
# Without sharp (--no-optional)
|
|
402
|
+
ℹ️ SVG → Native embedding → Works in Office 2019+ only
|
|
403
|
+
|
|
404
|
+
# Suppress the warning (if intentionally using native SVG mode)
|
|
405
|
+
imageProcessing: { suppressSharpWarning: true }
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
**Pro tip**: For serverless/Lambda deployments with size constraints, install without sharp and set `suppressSharpWarning: true` to avoid console warnings. Document that generated files require Office 2019+.
|
|
409
|
+
|
|
255
410
|
## RTL (Right-to-Left) Language Support
|
|
256
411
|
|
|
257
412
|
The library also supports RTL languages like Arabic and Hebrew. Use the `direction` option to enable RTL text flow:
|