@sofereditor/export-pdf 0.1.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/LICENSE +661 -0
- package/README.md +20 -0
- package/dist/index.cjs +540 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +106 -0
- package/dist/index.d.ts +106 -0
- package/dist/index.js +506 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
- package/src/__tests__/caption.test.ts +126 -0
- package/src/__tests__/html.test.ts +226 -0
- package/src/download.ts +26 -0
- package/src/html.ts +474 -0
- package/src/index.ts +17 -0
- package/src/pdf.ts +223 -0
- package/tsconfig.json +8 -0
- package/tsup.config.ts +11 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { SerializedDocument, LegacySerializedDocument } from '@sofereditor/core';
|
|
2
|
+
|
|
3
|
+
interface DocumentToHtmlOptions {
|
|
4
|
+
/** Document title (set on `<title>` and used as the file name hint). Default "Documento". */
|
|
5
|
+
title?: string;
|
|
6
|
+
/** When true (default) emits a full `<html>` document. When false emits only the body fragment. */
|
|
7
|
+
standalone?: boolean;
|
|
8
|
+
/** Extra CSS appended after the default print stylesheet. Only honored when `standalone`. */
|
|
9
|
+
extraCss?: string;
|
|
10
|
+
/** Page geometry hints injected into `@page`. Defaults to A4. */
|
|
11
|
+
page?: {
|
|
12
|
+
width: string;
|
|
13
|
+
height: string;
|
|
14
|
+
marginTop: string;
|
|
15
|
+
marginBottom: string;
|
|
16
|
+
marginLeft: string;
|
|
17
|
+
marginRight: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Serialize a `SerializedDocument` to standalone HTML.
|
|
22
|
+
*
|
|
23
|
+
* Output structure mirrors what `@sofereditor/react` paints at runtime: each
|
|
24
|
+
* top-level block becomes one element (`<p>`, `<h1..6>`, `<blockquote>`, `<pre>`,
|
|
25
|
+
* `<table>`), consecutive `listItem` blocks are reconstructed into nested
|
|
26
|
+
* `<ul>`/`<ol>` trees, and image embeds are emitted as `<img>` tags carrying the
|
|
27
|
+
* embed's data URL (so the export is self-contained).
|
|
28
|
+
*
|
|
29
|
+
* NOT a fragment of the live editor DOM — this walks the model so the output is
|
|
30
|
+
* editor-state agnostic (no caret artifacts, no zero-width phantom anchors).
|
|
31
|
+
*/
|
|
32
|
+
declare function documentToHtml(doc: SerializedDocument | LegacySerializedDocument, options?: DocumentToHtmlOptions): string;
|
|
33
|
+
/** Body-only HTML for embedding in other documents. */
|
|
34
|
+
declare function documentToHtmlFragment(doc: SerializedDocument | LegacySerializedDocument): string;
|
|
35
|
+
/** JSON export — a stable serialization for save/load round-trips. */
|
|
36
|
+
declare function documentToJson(doc: SerializedDocument | LegacySerializedDocument, pretty?: boolean): string;
|
|
37
|
+
|
|
38
|
+
interface ExportPdfFromDocumentOptions extends DocumentToHtmlOptions {
|
|
39
|
+
/** Suggested file name (used by the OS save dialog as the default). */
|
|
40
|
+
filename?: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Open the browser's print dialog for a `SerializedDocument`.
|
|
44
|
+
*
|
|
45
|
+
* Strategy: render the document to standalone HTML in a hidden iframe and call
|
|
46
|
+
* `iframe.contentWindow.print()`. The user picks "Save as PDF" (or any printer)
|
|
47
|
+
* from the native dialog. No external PDF library required.
|
|
48
|
+
*
|
|
49
|
+
* The returned promise resolves after the print dialog closes (`afterprint`).
|
|
50
|
+
*/
|
|
51
|
+
declare function exportPdfFromDocument(doc: SerializedDocument | LegacySerializedDocument, options?: ExportPdfFromDocumentOptions): Promise<void>;
|
|
52
|
+
interface SerializePaginatedHtmlOptions {
|
|
53
|
+
/** Document title (becomes `<title>` and is used as default filename). */
|
|
54
|
+
title?: string;
|
|
55
|
+
/** Extra CSS appended to the cloned styles (e.g. for headers/footers). */
|
|
56
|
+
extraCss?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Override the `@page { size }`. Default reads `--ed-page-width` /
|
|
59
|
+
* `--ed-page-height` CSS variables off the root (set by `<Editor>` from the
|
|
60
|
+
* current `PageSettings`); falls back to A4 if absent.
|
|
61
|
+
*/
|
|
62
|
+
pageSize?: {
|
|
63
|
+
width: string;
|
|
64
|
+
height: string;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
interface ExportPdfFromElementOptions extends SerializePaginatedHtmlOptions {
|
|
68
|
+
/** Suggested file name. */
|
|
69
|
+
filename?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Serialize a live editor root (the `.ed-root` paginated DOM) into a
|
|
73
|
+
* self-contained HTML string. The output preserves the in-editor pagination
|
|
74
|
+
* decisions verbatim — each `.ed-page` becomes a fixed-size page in the final
|
|
75
|
+
* print medium with no re-layout possible.
|
|
76
|
+
*
|
|
77
|
+
* The returned HTML inlines every accessible stylesheet from the host document
|
|
78
|
+
* (or links it as a fallback for cross-origin sheets) so the layout survives
|
|
79
|
+
* transplant to an iframe, a saved file, or a headless renderer.
|
|
80
|
+
*
|
|
81
|
+
* This is the building block for two paths:
|
|
82
|
+
* - `exportPdfFromElement` (this file): writes the HTML into a hidden iframe
|
|
83
|
+
* and calls `window.print()` for the local "Save as PDF" flow.
|
|
84
|
+
* - Upload-to-server snapshots: POST the string to a backend that converts it
|
|
85
|
+
* to PDF via Puppeteer's `setContent + page.pdf()` without re-running the
|
|
86
|
+
* pagination engine.
|
|
87
|
+
*/
|
|
88
|
+
declare function serializePaginatedHtml(root: HTMLElement, options?: SerializePaginatedHtmlOptions): string;
|
|
89
|
+
/**
|
|
90
|
+
* Snapshot a live editor root element (the `.ed-root` paginated DOM) and open
|
|
91
|
+
* the browser's print dialog. Preserves the in-editor pagination decisions
|
|
92
|
+
* verbatim — no re-layout in the print frame.
|
|
93
|
+
*
|
|
94
|
+
* For the "send the snapshot to a server" use case, use
|
|
95
|
+
* `serializePaginatedHtml` directly and POST the returned string.
|
|
96
|
+
*/
|
|
97
|
+
declare function exportPdfFromElement(root: HTMLElement, options?: ExportPdfFromElementOptions): Promise<void>;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Trigger a browser download for an in-memory blob/string. Tiny utility — kept
|
|
101
|
+
* in this package because PDF/HTML/JSON exporters all want it.
|
|
102
|
+
*/
|
|
103
|
+
declare function downloadBlob(blob: Blob, filename: string): void;
|
|
104
|
+
declare function downloadString(text: string, filename: string, mimeType?: string): void;
|
|
105
|
+
|
|
106
|
+
export { type DocumentToHtmlOptions, type ExportPdfFromDocumentOptions, type ExportPdfFromElementOptions, type SerializePaginatedHtmlOptions, documentToHtml, documentToHtmlFragment, documentToJson, downloadBlob, downloadString, exportPdfFromDocument, exportPdfFromElement, serializePaginatedHtml };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { SerializedDocument, LegacySerializedDocument } from '@sofereditor/core';
|
|
2
|
+
|
|
3
|
+
interface DocumentToHtmlOptions {
|
|
4
|
+
/** Document title (set on `<title>` and used as the file name hint). Default "Documento". */
|
|
5
|
+
title?: string;
|
|
6
|
+
/** When true (default) emits a full `<html>` document. When false emits only the body fragment. */
|
|
7
|
+
standalone?: boolean;
|
|
8
|
+
/** Extra CSS appended after the default print stylesheet. Only honored when `standalone`. */
|
|
9
|
+
extraCss?: string;
|
|
10
|
+
/** Page geometry hints injected into `@page`. Defaults to A4. */
|
|
11
|
+
page?: {
|
|
12
|
+
width: string;
|
|
13
|
+
height: string;
|
|
14
|
+
marginTop: string;
|
|
15
|
+
marginBottom: string;
|
|
16
|
+
marginLeft: string;
|
|
17
|
+
marginRight: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Serialize a `SerializedDocument` to standalone HTML.
|
|
22
|
+
*
|
|
23
|
+
* Output structure mirrors what `@sofereditor/react` paints at runtime: each
|
|
24
|
+
* top-level block becomes one element (`<p>`, `<h1..6>`, `<blockquote>`, `<pre>`,
|
|
25
|
+
* `<table>`), consecutive `listItem` blocks are reconstructed into nested
|
|
26
|
+
* `<ul>`/`<ol>` trees, and image embeds are emitted as `<img>` tags carrying the
|
|
27
|
+
* embed's data URL (so the export is self-contained).
|
|
28
|
+
*
|
|
29
|
+
* NOT a fragment of the live editor DOM — this walks the model so the output is
|
|
30
|
+
* editor-state agnostic (no caret artifacts, no zero-width phantom anchors).
|
|
31
|
+
*/
|
|
32
|
+
declare function documentToHtml(doc: SerializedDocument | LegacySerializedDocument, options?: DocumentToHtmlOptions): string;
|
|
33
|
+
/** Body-only HTML for embedding in other documents. */
|
|
34
|
+
declare function documentToHtmlFragment(doc: SerializedDocument | LegacySerializedDocument): string;
|
|
35
|
+
/** JSON export — a stable serialization for save/load round-trips. */
|
|
36
|
+
declare function documentToJson(doc: SerializedDocument | LegacySerializedDocument, pretty?: boolean): string;
|
|
37
|
+
|
|
38
|
+
interface ExportPdfFromDocumentOptions extends DocumentToHtmlOptions {
|
|
39
|
+
/** Suggested file name (used by the OS save dialog as the default). */
|
|
40
|
+
filename?: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Open the browser's print dialog for a `SerializedDocument`.
|
|
44
|
+
*
|
|
45
|
+
* Strategy: render the document to standalone HTML in a hidden iframe and call
|
|
46
|
+
* `iframe.contentWindow.print()`. The user picks "Save as PDF" (or any printer)
|
|
47
|
+
* from the native dialog. No external PDF library required.
|
|
48
|
+
*
|
|
49
|
+
* The returned promise resolves after the print dialog closes (`afterprint`).
|
|
50
|
+
*/
|
|
51
|
+
declare function exportPdfFromDocument(doc: SerializedDocument | LegacySerializedDocument, options?: ExportPdfFromDocumentOptions): Promise<void>;
|
|
52
|
+
interface SerializePaginatedHtmlOptions {
|
|
53
|
+
/** Document title (becomes `<title>` and is used as default filename). */
|
|
54
|
+
title?: string;
|
|
55
|
+
/** Extra CSS appended to the cloned styles (e.g. for headers/footers). */
|
|
56
|
+
extraCss?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Override the `@page { size }`. Default reads `--ed-page-width` /
|
|
59
|
+
* `--ed-page-height` CSS variables off the root (set by `<Editor>` from the
|
|
60
|
+
* current `PageSettings`); falls back to A4 if absent.
|
|
61
|
+
*/
|
|
62
|
+
pageSize?: {
|
|
63
|
+
width: string;
|
|
64
|
+
height: string;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
interface ExportPdfFromElementOptions extends SerializePaginatedHtmlOptions {
|
|
68
|
+
/** Suggested file name. */
|
|
69
|
+
filename?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Serialize a live editor root (the `.ed-root` paginated DOM) into a
|
|
73
|
+
* self-contained HTML string. The output preserves the in-editor pagination
|
|
74
|
+
* decisions verbatim — each `.ed-page` becomes a fixed-size page in the final
|
|
75
|
+
* print medium with no re-layout possible.
|
|
76
|
+
*
|
|
77
|
+
* The returned HTML inlines every accessible stylesheet from the host document
|
|
78
|
+
* (or links it as a fallback for cross-origin sheets) so the layout survives
|
|
79
|
+
* transplant to an iframe, a saved file, or a headless renderer.
|
|
80
|
+
*
|
|
81
|
+
* This is the building block for two paths:
|
|
82
|
+
* - `exportPdfFromElement` (this file): writes the HTML into a hidden iframe
|
|
83
|
+
* and calls `window.print()` for the local "Save as PDF" flow.
|
|
84
|
+
* - Upload-to-server snapshots: POST the string to a backend that converts it
|
|
85
|
+
* to PDF via Puppeteer's `setContent + page.pdf()` without re-running the
|
|
86
|
+
* pagination engine.
|
|
87
|
+
*/
|
|
88
|
+
declare function serializePaginatedHtml(root: HTMLElement, options?: SerializePaginatedHtmlOptions): string;
|
|
89
|
+
/**
|
|
90
|
+
* Snapshot a live editor root element (the `.ed-root` paginated DOM) and open
|
|
91
|
+
* the browser's print dialog. Preserves the in-editor pagination decisions
|
|
92
|
+
* verbatim — no re-layout in the print frame.
|
|
93
|
+
*
|
|
94
|
+
* For the "send the snapshot to a server" use case, use
|
|
95
|
+
* `serializePaginatedHtml` directly and POST the returned string.
|
|
96
|
+
*/
|
|
97
|
+
declare function exportPdfFromElement(root: HTMLElement, options?: ExportPdfFromElementOptions): Promise<void>;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Trigger a browser download for an in-memory blob/string. Tiny utility — kept
|
|
101
|
+
* in this package because PDF/HTML/JSON exporters all want it.
|
|
102
|
+
*/
|
|
103
|
+
declare function downloadBlob(blob: Blob, filename: string): void;
|
|
104
|
+
declare function downloadString(text: string, filename: string, mimeType?: string): void;
|
|
105
|
+
|
|
106
|
+
export { type DocumentToHtmlOptions, type ExportPdfFromDocumentOptions, type ExportPdfFromElementOptions, type SerializePaginatedHtmlOptions, documentToHtml, documentToHtmlFragment, documentToJson, downloadBlob, downloadString, exportPdfFromDocument, exportPdfFromElement, serializePaginatedHtml };
|