2

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

user7833845
  • 83
  • 2
  • 11
  • is using jspdf necessary, you can use html2pdf, have a look at it – Sagar Nov 14 '17 at 04:53
  • Yes I have to use jsPDF fromHTML() method, even addHTML() method of jsPDF has many drawbacks so I cannot use it. I already tried html2PDF and it has got its own drawbacks. Poor quality of PDF compared to the one generated using jsPDF fromHTML() and even html2PDF is not recognizing few css elements. jsPDF fromHTML() is supporting wordwrap using splitTextToSize(..) but I could not able to figure it out how to apply in my existing code..Any inputs? @Sagar Bhattacharya – user7833845 Nov 14 '17 at 05:11
  • i will need to check – Sagar Nov 14 '17 at 05:17
  • sure, thanks. I found this link https://stackoverflow.com/questions/24272058/word-wrap-in-generated-pdf-using-jspdf . Few answers in the shared link are good but unable to apply the same for my dynamic data shown in the js code above. @Sagar Bhattacharya – user7833845 Nov 14 '17 at 05:20
  • hey @user7833845 I posted a solution for you , please implement and test if the solution works for you – Sagar Nov 14 '17 at 17:23

2 Answers2

4

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

Sagar
  • 475
  • 2
  • 8
  • Awesome, it worked. Thanks. I have raised another question here, Please check the below if possible https://stackoverflow.com/questions/47311531/issue-to-show-highlighted-text-or-when-any-of-the-table-rows-are-blank @Sagar Bhattacharya – user7833845 Nov 15 '17 at 15:43
0

@Sagar is correct. I just set the width of page dynamicly

var doc = new jsPDF(orientation, 'pt', 'a4', true);// orientation could be 'p' (portrail) or 'l' (landscape)

var a4Width = Number(doc.internal.pageSize.getWidth()); //page width is now based on the orientation

doc.fromHTML($('#yourTarget').html(), 0, 0, {
    pagesplit: true,
    'width': a4Width
});
Anh Hoang
  • 2,242
  • 3
  • 22
  • 23