Jspdf: Line breaks

Created on 23 Jul 2013  路  17Comments  路  Source: MrRio/jsPDF

How do I get jsPDF to recognize a line break?? Only way I've managed to break text to a new line is to insert '

', but that doesn't work for multiple line breaks. I saw a closed issue here suggesting that '\n' would work, but it doesn't. What am I doing wrong?

Most helpful comment

have you tried this:

"Im line number1\r\nAnd Im line number2"

All 17 comments

have you tried this:

"Im line number1\r\nAnd Im line number2"

I tried both <h1></h1> and \r\n
does not works

can you please share the demo

This is the HTML plugin? If you just wrap each line in paragraphs <p>line one</p><p>line two</p>

i wanted to insert blank lines

So... perhaps <p></p> ...?

You asked for a demo, but you aren't providing any info on what plugin or function you're using so we have no idea what specific demo do you need, it should be easy if you just post your code.

How you resolved?

Yeah we need to use <br/> instead of <p> to avoid the excess padding for things like address formatting. <br/> is not working, nor is \n or \r\n

@ajwhite The latest dist files should support BR tags already.

Hello, I am having the same problem. The BR Tags do not function.

The following worked for me...

    <h2><span style="color: white">|</span></h2>

When I try to use <br /> jsPDF throws an error: Uncaught TypeError: Cannot read property '1' of undefined on line 5379 in the debug file.

Renderer.renderParagraph @ jspdf.debug.js:5379
Renderer.setBlockBoundary @ jspdf.debug.js:5430
DrillForContent @ jspdf.debug.js:4940
(anonymous function) @ jspdf.debug.js:5080
done @ jspdf.debug.js:4950
loadImgs @ jspdf.debug.js:4980
process @ jspdf.debug.js:5078
jsPDFAPI.fromHTML @ jspdf.debug.js:5517

Can also confirm Uncaught TypeError: Cannot read property '1' of undefined error when there are <br/>

Hello~
Piling onto the <br> problem.
I have also tried <p></p> and it did not work.

Okay, so in jspdf.debug.js line#4730 there is the relevant line for parsing the table cell.
rowData[headers[j].name] = tableRow.cells[j].textContent.replace(/\r?\n/g, '');

Here it regex-es out the new line character \n with an empty string. So I removed the \n in the regex and added a \n in my HTML instead of a <br>. It's probably workable for my case...so I thought I would share.

And while I'm here, anyone have a usecase for why the \n should be part of the regex?

Awesome find @kamikaz1k

P.S. you can link to code lines by clicking on the line number https://github.com/MrRio/jsPDF/blob/master/dist/jspdf.debug.js#L4730

have you tried this:

"Im line number1\r\nAnd Im line number2"

perfect solution !!!

Was this page helpful?
0 / 5 - 0 ratings