pdf-lib
pdf-lib copied to clipboard
BUG: breakTextIntoLines removes empty lines in multiline text
What were you trying to do?
draw multiline text with empty line and maxWidth param
How did you attempt to do it?
function page.drawText have different result when pass maxWidth param and text with empty line for example:
page.drawText(
`first\n` +
`\n` +
`second\n`
,
{
x: 25,
y: 100,
font,
size: 24,
color: rgb(1, 0, 0),
lineHeight: 24,
opacity: 0.75,
maxWidth: 500,
},
)
result :
call page.drawText without maxWidth
page.drawText(
`first\n` +
`\n` +
`second\n`
,
{
x: 25,
y: 100,
font,
size: 24,
color: rgb(1, 0, 0),
lineHeight: 24,
opacity: 0.75,
},
)
result:
as you can see, without maxWidth parameter the empty line is also present
What actually happened?
empty line not rendered
What did you expect to happen?
few tests that should passed
https://github.com/Hopding/pdf-lib/blob/master/tests/utils/strings.spec.ts#L23
it(`handles trailing newlines`, () => {
const input = 'foo\n';
const expected = ['foo', ''];
const actual = breakTextIntoLines(input, [], 21, computeTextWidth);
expect(actual).toEqual(expected);
});
it(`handles empty lines`, () => {
const input = 'foo\n\nbar';
const expected = ['foo', '', 'bar'];
const actual = breakTextIntoLines(input, [], 21, computeTextWidth);
expect(actual).toEqual(expected);
});
How can we reproduce the issue?
see above
Version
1.17.1
What environment are you running pdf-lib in?
Browser
Checklist
- [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
- [x] I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
No response
Also had this issue. As a quick fix I just inserted a single whitespace between two back to back line feeds.
line = line.replaceAll("\n\n", "\n \n");