Search code examples
javascriptjqueryhtmljspdfword-wrap

to apply word wrap for the long dynamic text


I am working on angularjs application with jsPDF API to export the content to the PDF file. I have a issue when the text is long to show in the PDF, when the text is long complete sentence is not shown in the generated PDF as seen in the demo link below. I want to apply word wrap so that all the content is displayed in the PDF file.

Check the online demo here : https://plnkr.co/edit/w7fZsdFbbRYctK5tWv5u?p=preview

Code demo:

 var app = angular.module("app", []);

 app.controller("myController", ["$scope",
   function($scope) {
   
   $scope.export = function() {

       // var pdf = new jsPDF('landscape');
       var pdf = new jsPDF('p','pt','a4');
        var pdfName = 'test.pdf';

        var options = {   pagesplit: true};

        var $divs = $('.myDivClass')                
        var totalDiv = $divs.length -1;     
        var currentRecursion=0;

        function recursiveAddHtmlAndSave(currentRecursion, totalRecursions){
            //Once we have done all the divs save the pdf
            if(currentRecursion==totalRecursions){
                pdf.save(pdfName);
            }else{
                currentRecursion++;
                pdf.addPage();
                pdf.fromHTML($('.myDivClass')[currentRecursion], 15, 20, options, function(){
                    recursiveAddHtmlAndSave(currentRecursion, totalRecursions)
                });
            }
        }

        pdf.fromHTML($('.myDivClass')[currentRecursion], 15, 20, options, function(){
            recursiveAddHtmlAndSave(currentRecursion, totalDiv);
        });
    }
   }
 ]);
<!doctype html>
<html ng-app="app">

<head>
<link data-require="bootstrap-css@3.2.0" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
    <link rel="stylesheet" href="style.css" />
     
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.33/vfs_fonts.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js"></script>
</head>

<body>
  <div ng-controller="myController">
        <button ng-click="export()">Export</button>

   <div class="myDivClass" style="background-color:white">

The identity of the longest word in English depends upon the definition of what constitutes a word in the English language, as well as how length should be compared. In addition to words derived naturally from the language's roots (without any known intentional invention), English allows new words to be formed by coinage and construction; place names may be considered words; technical terms may be arbitrarily long. Length may be understood in terms of orthography and number of written letters, or (less commonly) 

  </div></div>
</body>

</html>

I have checked the API, and there is splitTextToSize(..) as shown here Word wrap in generated PDF (using jsPDF)? but confused to apply the same in my existing code, as if you see my js code shown above $scope.export i'm iterating the div's to get the dynamic data and then called pdf.save(pdfName). Can anyone provide the solution, Online demo link is also shared.

PS:I may have text, images,and any other html tags in my div(myDivClass).


Solution

  • Well I got a quick workaround for your solution , we can set a width of the page and then the long text gets printed inside the pdf and no content breaks out of the pdf , just update your options in the script.js to this :

    var options = {   pagesplit: true,'width': 550}
    

    and this will do the work for you