@imggion/html2realpdf 0.3.1 → 0.4.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.
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: html2realpdf
3
- description: Integrate and troubleshoot @imggion/html2realpdf in browser TypeScript or JavaScript projects. Use when rendering HTML strings, DOM elements, or React-compatible refs to selectable PDFs; designing production HTML and CSS templates; configuring deterministic A4 layouts, pagination, cards, tables, legends, resources, fonts, SVG, or canvas output; diagnosing clipped content, blank pages, or alignment issues; previewing, downloading, or exporting PDFs; inspecting diagnostics; or migrating supported html2pdf.js chains.
3
+ description: Integrate and troubleshoot @imggion/html2realpdf in browser or Node TypeScript or JavaScript projects. Use when rendering HTML strings, DOM elements, or React-compatible refs to selectable PDFs or to JPEG images with renderImage, including server-side rendering of React markup in Node; designing production HTML and CSS templates; configuring deterministic A4 layouts, pagination, cards, tables, legends, resources, fonts, SVG, or canvas output; diagnosing clipped content, blank pages, or alignment issues; previewing, downloading, or exporting PDFs; inspecting diagnostics; or migrating supported html2pdf.js chains.
4
4
  ---
5
5
 
6
6
  # Use html2realpdf
@@ -11,14 +11,18 @@ Install the ESM package and import only from its public root:
11
11
  npm install @imggion/html2realpdf
12
12
  ```
13
13
 
14
- The module is safe to import during SSR, but call rendering APIs only in a
15
- browser. Do not deep-import `dist` files or construct exported result classes.
14
+ In the browser it renders Elements, refs, and HTML strings. In Node the same
15
+ import (the `node` export condition, or `@imggion/html2realpdf/node`) renders
16
+ HTML strings; see "Render in Node" below. Do not deep-import `dist` files or
17
+ construct exported result classes.
16
18
 
17
19
  ## Choose the API
18
20
 
19
21
  - Use `renderPdf` for convenient page-lifetime rendering.
20
22
  - Use `createRenderer` for repeated renders, custom fonts, explicit Worker
21
23
  selection, or deterministic disposal.
24
+ - Use `renderImage` (or `renderer.renderImage`) for a JPEG of a component or
25
+ a whole page; see "Render images" below.
22
26
  - Use the default export only when adapting a supported html2pdf.js chain.
23
27
  - Prefer a mounted `Element` or ref-shaped `{ current }` value when computed
24
28
  styles, form state, pseudo-elements, canvas pixels, or Shadow DOM matter.
@@ -323,8 +327,12 @@ Pass only renderer-owned resources and execution settings to `createRenderer`:
323
327
  | `fonts` | `FontRegistration[]`; none by default | Register reusable TrueType faces once for every render owned by this renderer. |
324
328
 
325
329
  Configure each font registration with `family`, binary `data`, optional
326
- `weight` (`400` by default, or `"normal"`/`"bold"`), and optional `style`
327
- (`"normal"` by default or `"italic"`).
330
+ `weight` (`400` by default, any CSS weight from `1` to `1000`, or
331
+ `"normal"`/`"bold"`), and optional `style` (`"normal"` by default or
332
+ `"italic"`). Register one face per weight you use, such as Regular `400`,
333
+ Medium `500`, and SemiBold `600`: faces are chosen with CSS font matching, and
334
+ only the same family, weight, and style replaces an earlier face. Pass
335
+ TrueType (`.ttf`) bytes; WOFF and WOFF2 files are rejected.
328
336
 
329
337
  ### Per-render options
330
338
 
@@ -370,6 +378,73 @@ Configure `pageBreak` with selector strings or arrays in `before`, `after`, and
370
378
  `legacy: true` to honor `.html2pdf__page-break`. Authored inline `!important`
371
379
  break declarations retain precedence.
372
380
 
381
+ ### Render images
382
+
383
+ `renderImage(source, options)` accepts the same sources and snapshot/resource
384
+ options as `renderPdf` (except `page`, `pageBreak`, `metadata`,
385
+ `conformance`, `attachments`, and `enableLinks`) and returns a
386
+ `RenderedImage` with `bytes`, `width`, `height`, `mimeType`, `diagnostics`,
387
+ `toBlob()`, `toDataURL()`, `createObjectURL()`, `download()`, and `dispose()`.
388
+
389
+ | Option | Values and default | Use |
390
+ | --- | --- | --- |
391
+ | `width` | 1–4096 CSS px; element width for Element/ref, `viewport.width` or 800 for strings | Canvas width. Height always follows the content; page breaks do not apply. |
392
+ | `scale` | 0.5–4; `1` | Device pixels per CSS pixel (use `2` for retina-sharp output). |
393
+ | `quality` | 0–1; `0.8` | JPEG quality on the `canvas.toBlob` scale. Out-of-range values throw `RangeError` (do not pass `80`); `0` encodes at 1% with an `IMAGE_QUALITY_ADJUSTED` diagnostic. |
394
+ | `chromaSubsampling` | `"4:2:0"` or `"4:4:4"`; `4:4:4` when `quality >= 0.9`, else `4:2:0` | Prefer `4:4:4` for small colored text. |
395
+ | `background` | CSS color; `"#ffffff"` | Opaque color behind transparent content. |
396
+ | `cssProfile` | `"web"` (default for images), `"document"`, or `"strict"` | Same profiles as PDF rendering. |
397
+ | `format` | `"jpeg"` | The only encoder; other values throw. |
398
+
399
+ ```ts
400
+ const image = await renderImage(cardRef, { scale: 2, quality: 0.85 });
401
+ previewImg.src = image.createObjectURL(); // revoked by image.dispose()
402
+ downloadButton.onclick = () => image.download("card.jpg");
403
+ ```
404
+
405
+ Inline SVG charts, canvases, gradients, shadows, and PNG images rasterize
406
+ natively. JPEG resources are converted to PNG during the browser snapshot.
407
+ Outputs above 16,777,216 pixels fail with `WasmRenderError` status `-8`; very
408
+ long documents at high scale may need a smaller `scale` or `width`.
409
+
410
+ ### Render in Node
411
+
412
+ Use Node for server-side output from HTML strings, for example
413
+ `renderToStaticMarkup(<Report />)`:
414
+
415
+ ```ts
416
+ import { createRenderer } from "@imggion/html2realpdf";
417
+
418
+ const renderer = await createRenderer({ fonts }); // reads the packaged WASM from disk
419
+ try {
420
+ const image = await renderer.renderImage(html, { width: 1200, scale: 2 });
421
+ await writeFile("report.jpg", image.bytes);
422
+ const pdf = await renderer.render(html, { cssProfile: "web" });
423
+ await writeFile("report.pdf", pdf.toUint8Array());
424
+ } finally {
425
+ renderer.dispose();
426
+ }
427
+ ```
428
+
429
+ - Pass strings only; Elements and refs throw `UnsupportedEnvironmentError`.
430
+ - Nothing is fetched. Supply `<img>`, `<link rel="stylesheet">`, and CSS
431
+ `url()` resources through `resourceResolver` (CSS text; PNG, JPEG, or SVG
432
+ data URL or `Blob`) with `baseUrl` for relative URLs, or set
433
+ `resourcePolicy: "omit"` to drop them with `RESOURCE_OMITTED`.
434
+ - Inline `<svg>` renders natively at its `width`/`height`/`viewBox` or CSS
435
+ size, with `currentColor`, presentation attributes, inline styles, and
436
+ stylesheet classes applied. Its text never becomes page text. Unsupported
437
+ SVG cannot be rasterized in Node: `fallback: "error"` (default) throws
438
+ `UnsupportedCssError`, otherwise it is left blank with
439
+ `INLINE_SVG_UNSUPPORTED`; malformed SVG follows `resourcePolicy`
440
+ (`INLINE_SVG_INVALID`).
441
+ - Prefer PNG over JPEG `<img>` for `renderImage`; JPEG inputs stay blank in
442
+ raster output (`IMAGE_FORMAT_NOT_RASTERIZED`).
443
+ - The core cascade styles strings: type, class, id, universal, descendant,
444
+ and child selectors. Rules with pseudo-classes or attribute selectors are
445
+ skipped. Set `line-height` on each element that needs it; it does not
446
+ inherit yet. Prefer inline styles or simple class rules for server templates.
447
+
373
448
  ### Preview options
374
449
 
375
450
  Pass these options to `PdfDocument.preview`: