@turbodocx/html-to-docx 1.17.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 CHANGED
@@ -247,6 +247,9 @@ full fledged examples can be found under `example/`
247
247
  - `minImageSize` <?[Number]> minimum image size in bytes. Defaults to `1024` (1KB).
248
248
  - `maxCacheSize` <?[Number]> maximum total cache size in bytes (LRU cache limit to prevent OOM). Defaults to `20971520` (20MB).
249
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)
250
253
  - `footerHTMLString` <[String]> clean html string equivalent of footer. Defaults to `<p></p>` if footer flag is `true`.
251
254
 
252
255
  ### Returns
@@ -280,6 +283,130 @@ Also you could add attribute `data-start="n"` to start the numbering from the n-
280
283
  `<ol data-start="2">` will start the numbering from ( B. b. II. ii. 2. )
281
284
 
282
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
+
283
410
  ## RTL (Right-to-Left) Language Support
284
411
 
285
412
  The library also supports RTL languages like Arabic and Hebrew. Use the `direction` option to enable RTL text flow: