Skip to content

Printing list item with line break fails with Unsupported Number: NaN #1486

@filecage

Description

@filecage

Bug Report

Description of the problem

When using doc.list() to print list items, the process fails if one list item contains a line break (newline or return carriage characters both affected).

This is due to the internal flatten() of the text mixin calculates the amount of levels based on the number of list items given here:

pdfkit/lib/mixins/text.js

Lines 124 to 142 in 6338314

var flatten = function(list) {
let n = 1;
for (let i = 0; i < list.length; i++) {
const item = list[i];
if (Array.isArray(item)) {
level++;
flatten(item);
level--;
} else {
items.push(item);
levels.push(level);
if (listType !== 'bullet') {
numbers.push(n++);
}
}
}
};
flatten(list);

But the firstLine event is emitted per actual line, causing the event handler to access an undefined level item here:
https://github.com/foliojs/pdfkit/blob/6338314daf44f7f188e4cc6cb4d70fc2f2698fca/lib/mixins/text.js#L179-182

Which then results in diff being NaN and the consecutive variables this.x and wrapper.lineWidth as well.

From my knowledge of the library, I can't tell whether flatten() should produce the levels based on the actual lines or the firstLine event should only be emitted once per list item.

Code sample

With any PDFKit document, call

doc.list(['Foo\nBar']);

Your environment

  • pdfkit version: 0.14.0
  • Node version: 20.10.0
  • Browser version (if applicable): -
  • Operating System: macOS 14.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions