How to wrap word in jsPDF?

Hamza Ahmed Zia picture Hamza Ahmed Zia · Sep 1, 2016 · Viewed 15.7k times · Source

I downloaded jspdf 1.2.60 to generate pdf containing text from my html table. One of the columns contains notes entered by user thus can be very large as a result of which text is running of the page.

On scouring I found this however the

var splitTitle = doc.splitTextToSize(reportTitle, 180);

is no more available in the api. (I looked into the jspdf.js file).

How do I solve this?

Any idea is appreciated.

Answer

Abhijeet picture Abhijeet · Sep 16, 2016

Below code will be useful for your problem. It will work perfectly for your requirement.

    var lMargin=15; //left margin in mm
    var rMargin=15; //right margin in mm
    var pdfInMM=210;  // width of A4 in mm
    function getPDF() {
	var doc = new jsPDF("p","mm","a4");
	var paragraph="Apple's iPhone 7 is officially upon us. After a week of pre-orders, the latest in the iPhone lineup officially launches today.\n\nEager Apple fans will be lining up out the door at Apple and carrier stores around the country to grab up the iPhone 7 and iPhone 7 Plus, while Android owners look on bemusedly.\n\nDuring the Apple Event last week, the tech giant revealed a number of big, positive changes coming to the iPhone 7. It's thinner. The camera is better. And, perhaps best of all, the iPhone 7 is finally water resistant.\n\nStill, while there may be plenty to like about the new iPhone, there's plenty more that's left us disappointed. Enough, at least, to make smartphone shoppers consider waiting until 2017, when Apple is reportedly going to let loose on all cylinders with an all-glass chassis design.";
		
        var lines =doc.splitTextToSize(paragraph, (pdfInMM-lMargin-rMargin));
	doc.text(lMargin,20,lines);
	doc.save('Generated.pdf');
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>
<button id="getPDF" onclick="getPDF()">Get PDF</button>

For splitTextToSize :

As you were saying doc.splitTextToSize(text,size) is not present in jspdf 1.2.60 api.

Reason : It doc.splitTextToSize(text,size) is present in jspdf 1.2.60 lib but not in jspdf.js. It is present in jspdf.debug.js and in its minified vesion jspdf.min.js. That's the reason you are not finding splitTextToSize in your jspdf.js. You were looking it in wrong file. You can replace your jspdf.js with jspdf.debug.js and your code will work perfectly.

jspdf.debug.js and jspdf.min.js are present in \jsPDF-master\dist location in api zip file.

Note : I have provided you code for splitting your long paragraph in multiple lines. Here I've split line after 180mm(210mm-15mm-15mm),If text goes beyond 180mm then text will be go in newline. You can change the parameters according to your use.

Below is screenshot for your reference which contains splitTextToSize function definition in jspdf.debug.js.

enter image description here Let me know if you need any help. Happy Coding...!