Skip to content

Commit 9a6a6cd

Browse files
committed
v2.5.32
fixed issue #253
1 parent 26dc771 commit 9a6a6cd

File tree

9 files changed

+53
-6
lines changed

9 files changed

+53
-6
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
Convert HTML to PDFMake format with ease. This library bridges the gap between HTML content and [PDFMake](https://pdfmake.github.io/docs/) document definitions, allowing you to generate PDFs from basic HTML while maintaining based styling and structure.
44

5-
**Note**: if you need to convert a complex HTML, check some online solutions, like [Doppio](https://doppio.sh/), or you could try to convert [your HTML to canvas](https://github.com/chearon/dropflow) or [to an image](https://github.com/zumerlab/snapdom) and then to [export it to PDF](https://github.com/parallax/jsPDF).
5+
**Note**: if you need to convert a complex HTML (e.g. something produced by a Rich Text Editor), check some online solutions, like [Doppio](https://doppio.sh/), or you could try to convert [your HTML to canvas](https://github.com/chearon/dropflow) or [to an image](https://github.com/zumerlab/snapdom) and then to [export it to PDF](https://github.com/parallax/jsPDF).
6+
7+
This library will have the same limitation as PDFMake. If you need to verify if a style is supported by PDFMake, you can check [its documentation](https://deepwiki.com/bpampuch/pdfmake).
68

79
## Features
810

browser.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/browser-2.5.31.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/browser-2.5.32.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ <h1>HTML to PDFMake convertor</h1>
116116
<div id="pdf_ie" style="display:none;padding:3em">The PDF file is sent to you for download. Use a modern browser (like Chrome or Firefox) to display the PDF in this page.</div>
117117
</div>
118118
</div>
119-
<script src="browser-2.5.31.js"></script>
119+
<script src="browser-2.5.32.js"></script>
120120
<script src="https://cdn.jsdelivr.net/npm/pdfmake@latest/build/pdfmake.min.js"></script>
121121
<script src="https://cdn.jsdelivr.net/npm/pdfmake@latest/build/vfs_fonts.js"></script>
122122
<script>

example.pdf

0 Bytes
Binary file not shown.

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ function htmlToPdfMake(htmlText, options) {
487487
ret[nodeNameLowerCase] = (ret.stack || ret.text).slice(0);
488488
delete ret.stack;
489489
delete ret.text;
490+
490491
// apply all the inhirent classes and styles from the parents, or for the current element
491492
ret = this.applyStyle({ret:ret, parents:parents.concat([element])});
492493
// check if we have `start`
@@ -511,8 +512,10 @@ function htmlToPdfMake(htmlText, options) {
511512
// if not, we restructure our node
512513
// by moving the non-stack stuff inside a "text"
513514
text = ret.stack.slice(0, -1);
515+
// make sure we only have 'text' as a child
516+
// otherwise we switch to a stack
514517
ret = [
515-
{"text": text}, // (Array.isArray(text) ? {"stack": text} : {"text": text}),
518+
(Array.isArray(text) && text.filter(function(child) { return !child.text; }).length > 0 ? { "stack": text } : { "text": text }),
516519
ret.stack[ret.stack.length-1]
517520
];
518521
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-to-pdfmake",
3-
"version": "2.5.31",
3+
"version": "2.5.32",
44
"description": "Convert HTML code to PDFMake",
55
"main": "index.js",
66
"scripts": {

test/unit.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,48 @@ test("unit tests", function(t) {
12481248
t.finish();
12491249
});
12501250

1251+
t.test("complex nested ul/ol with p tags", function (t) {
1252+
var html = `<ul><li><p><strong>sometitle</strong></p><ol><li><p><strong>sometitle2:</strong></p><ul><li><p>sometext</p></li><li><p>sometext</p></li><li><p>sometext</p></li><li><p>sometext</p></li></ul></li><li><p><strong>sometitle3:</strong></p><ul><li><p>sometext</p></li></ul></li><li><p><strong>sometitle4:</strong></p><ul><li><p>sometext</p></li><li><p>sometext</p></li><li><p>sometext</p></li></ul></li><li><p><strong>sometitle5:</strong></p><ul><li><p>sometext</p></li><li><p>sometext</p></li><li><p>sometext</p></li></ul></li><li><p><strong>sometitle6:</strong></p><ul><li><p>sometext</p></li></ul></li></ol><p><strong>sometitle7:</strong></p><ul><li><p>sometext</p></li><li><p>somtext</p></li></ul></li></ul>`;
1253+
var ret = htmlToPdfMake(html, {
1254+
window: window
1255+
});
1256+
if (debug) console.log(JSON.stringify(ret));
1257+
1258+
// 1. Basic structure check
1259+
t.check(Array.isArray(ret) && ret.length === 1, "return is OK");
1260+
t.check(ret[0].nodeName === "UL" && Array.isArray(ret[0].ul) && ret[0].ul.length === 1, "root is UL with one LI");
1261+
1262+
// 2. Check the main LI stack
1263+
var mainLiStack = ret[0].ul[0].stack[0].stack;
1264+
t.check(Array.isArray(mainLiStack) && mainLiStack.length === 3 && mainLiStack[0].nodeName === "P" && mainLiStack[1].nodeName === "OL" && mainLiStack[2].nodeName === "P", "main LI stack has 3 items (p, ol, p)");
1265+
1266+
// 3. Check 'sometitle' (first item in main stack)
1267+
// This path (mainLiStack[0].stack[0]...) passed, so we keep it.
1268+
t.check(mainLiStack[0].text[0].text === "sometitle", "first P (sometitle)");
1269+
1270+
// 4. Check the nested OL (second item in main stack)
1271+
var nestedOl = mainLiStack[1];
1272+
t.check(nestedOl.nodeName === "OL" && Array.isArray(nestedOl.ol) && nestedOl.ol.length === 5, "nested OL has 5 LIs");
1273+
1274+
// 5. Check 'sometitle2:' (first LI of the nested OL)
1275+
// Path: ret[0].ul[0].stack[1].ol[0].stack[0].text[0].text
1276+
var nestedLi1 = nestedOl.ol[0];
1277+
t.check(nestedLi1.stack[0].text[0].text[0].text === "sometitle2:", "nested OL LI 1 P (sometitle2:)");
1278+
1279+
// 6. Check "super grand child" (UL inside the first LI of the nested OL)
1280+
// Path: ret[0].ul[0].stack[1].ol[0].stack[1].ul[0].stack[0].text
1281+
var deepUl = nestedLi1.stack[1]; // This assumes stack[0] was the <p>
1282+
t.check(deepUl.nodeName === "UL" && Array.isArray(deepUl.ul) && deepUl.ul.length === 4, "deep UL has 4 LIs");
1283+
t.check(deepUl.ul[0].stack[0].text === "sometext", "deep UL LI 1 P (sometext)");
1284+
t.check(deepUl.ul[3].stack[0].text === "sometext", "deep UL LI 4 P (sometext)");
1285+
1286+
// 7. Check 'sometitle7:' (third item in main stack)
1287+
// Path: ret[0].ul[0].stack[2].text[0].text
1288+
t.check(mainLiStack[2].text[0].text === "sometitle7:", "second P (sometitle7:)");
1289+
1290+
t.finish();
1291+
});
1292+
12511293
t.test("CSS units",function(t) {
12521294
var html = `<div style="line-height:107%;font-size:large;width:110px;height:50pt;margin-left:1in;margin-top:2em;margin-right:1rem;margin-bottom:0.5cm">hello world</div>`;
12531295
var ret = htmlToPdfMake(html, {

0 commit comments

Comments
 (0)