Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap word in jsPDF?

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.

like image 380
Hamza Ahmed Zia Avatar asked Sep 01 '16 08:09

Hamza Ahmed Zia


2 Answers

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...!

like image 70
Abhijeet Avatar answered Nov 13 '22 11:11

Abhijeet


It just works without any flaws. Proof:

function createPdf() {
	var doc = new jsPDF('p','in','letter')
	var loremipsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id eros turpis. Vivamus tempor urna vitae sapien mollis molestie. Vestibulum in lectus non enim bibendum laoreet at at libero. Etiam malesuada erat sed sem blandit in varius orci porttitor. Sed at sapien urna. Fusce augue ipsum, molestie et adipiscing at, varius quis enim. Morbi sed magna est, vel vestibulum urna. Sed tempor ipsum vel mi pretium at elementum urna tempor. Nulla faucibus consectetur felis, elementum venenatis mi mollis gravida. Aliquam mi ante, accumsan eu tempus vitae, viverra quis justo.\n\nProin feugiat augue in augue rhoncus eu cursus tellus laoreet. Pellentesque eu sapien at diam porttitor venenatis nec vitae velit. Donec ultrices volutpat lectus eget vehicula. Nam eu erat mi, in pulvinar eros. Mauris viverra porta orci, et vehicula lectus sagittis id. Nullam at magna vitae nunc fringilla posuere. Duis volutpat malesuada ornare. Nulla in eros metus. Vivamus a posuere libero.'

        // This line works. Try generating PDF.
	lines = doc.splitTextToSize(loremipsum, 7.5)

	doc.text(0.5, 0.5, lines)
	doc.save('Test.pdf')
}
<!DOCTYPE html>
<html>
<head>
	<title>PDF Test</title>
</head>
<body>
	<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.60/jspdf.min.js"></script>
	<button onclick="createPdf()">Create PDF</button>
</body>
</html>

You can see the same results in the latest version 1.2.61 too. So, clearly, something else is causing the error.

EDIT: Here I've used in so here it wraps if text goes beyond 7.5in. If you're using a different unit, deal with it properly, or that may create problems

like image 3
Saravanabalagi Ramachandran Avatar answered Nov 13 '22 12:11

Saravanabalagi Ramachandran