0

I am exporting the data present inside the div to PDF when user click on the export button. I want to show each div content to show in individual pages inside the PDF.

The above scenario is working in the demo https://plnkr.co/edit/KvkVlYmmmJiZ71sghb1l?p=preview

The same when applied to below code, it is not working. Demo here : https://plnkr.co/edit/P9nUSRY5TytkonM6dUHl?p=preview

js code:

$scope.export = function() {
   var pdf = new jsPDF('landscape');
   var source = $('#append-source');
    $('.myDivClass').each(function(){
     var html = "<div>"+$(this) + "</div><!--ADD_PAGE-->";//the code is broken with this line
     // var html = $(this);
      source.append(html);
    });
    console.log(source);
    pdf.addHTML(
          source, 0, 0, {
              pagesplit: true
          },
          function(dispose){
              pdf.save('test3.pdf');
          }
      );
     }

2 Answers2

1

It is not recommended to use jquery like this inside an angular app. To see why look here: Can we use both jQuery and Angular in our Web Application?

However what you want to do is possible if you put the following into your controller:

$scope.export = function() {
    var pdf = new jsPDF('landscape');
    var source = "";
    var width1 = pdf.internal.pageSize.width;

    $('.myDivClass').each(function(){
        var textForPdfPage = $(this).children().eq(1).children()[0].textContent;
        var html = "<div>"+ textForPdfPage + " </div><!--ADD_PAGE-->";
        source+=html;
    });

    margins = {
        top: 80,
        bottom: 60,
        left: 10,
        width: '100%'
    };

    pdf.fromHTML(
        source, // HTML string or DOM elem ref.
        margins.left, // x coord
        margins.top, { // y coord
            'width': width1 // max width of content on PDF
        },
        function (dispose) {
            pdf.save('test.pdf');
        },
        margins
    );
}

Your main problem is that when you where trying to create your html string you only used $(this). $(this) gives you a jquery object. The string you want to put on the page is inside this object and is accessed using the jquery .children() method.

Paddy
  • 595
  • 1
  • 4
  • 14
  • Thanks for the explanation. I want to use addHTML instead of fromHTML. As fromHTML has some issues to recognize some special characters.@Paddy – user8838953 Oct 29 '17 at 05:30
  • I have to use addHTML i stead of fromHTML. I tried to update the plunker https://plnkr.co/edit/Fj9tlQ3GuqyAfGqnEIfU?p=preview as per your suggestion but using addHTML, ended up with errors. Any suggestions on how to achieve it using addHTML. Thanks? @Paddy – user8838953 Oct 29 '17 at 05:37
  • Do you only want "randomText1" on the first page or do you want everything thats inside the div? e.g. "This should be shown in page1 randomText1 option1 option2 option3 qeqweqwe weqwewewew wewew wewqewq wew ewewqe we" – Paddy Oct 29 '17 at 08:36
0

Here is a way of doing what you asked using addHTML() instead of fromHTML():

$scope.export = function() {

        var pdf = new jsPDF('landscape');
        var pdfName = 'test.pdf';

        var options = {};

        var $divs = $('.myDivClass')                //jQuery object of all the myDivClass divs
        var numRecursionsNeeded = $divs.length -1;     //the number of times we need to call addHtml (once per div)
        var currentRecursion=0;

        //Found a trick for using addHtml more than once per pdf. Call addHtml in the callback function of addHtml recursively.
        function recursiveAddHtmlAndSave(currentRecursion, totalRecursions){
            //Once we have done all the divs save the pdf
            if(currentRecursion==totalRecursions){
                pdf.save(pdfName);
            }else{
                currentRecursion++;
                pdf.addPage();
                //$('.myDivClass')[currentRecursion] selects one of the divs out of the jquery collection as a html element
                //addHtml requires an html element. Not a string like fromHtml.
                pdf.addHTML($('.myDivClass')[currentRecursion], 15, 20, options, function(){
                    console.log(currentRecursion);
                    recursiveAddHtmlAndSave(currentRecursion, totalRecursions)
                });
            }
        }

        pdf.addHTML($('.myDivClass')[currentRecursion], 15, 20, options, function(){
            recursiveAddHtmlAndSave(currentRecursion, numRecursionsNeeded);
        });
    }

I left the other answer so people can see both ways of doing it.

Paddy
  • 595
  • 1
  • 4
  • 14
  • Thanks for the inputs. When used the above mentioned code, special characters are not being imported into the PDF and that that is the reason i was using
    . Please see the demo https://plnkr.co/edit/J54E1Jworp3IQG04Z41C?p=preview , when clicked on export button, the bullets and the numbers displayed for option1 and option2 are not shown in the PDF.@Paddy
    – user8838953 Oct 29 '17 at 23:54
  • Looks like a problem with html 2canvas https://github.com/niklasvh/html2canvas/issues/177 – Paddy Oct 30 '17 at 06:54
  • I'm sorry but I don't know how to fix that. Somebody was looking at fixing it but looks like it was never finished: https://github.com/niklasvh/html2canvas/pull/486 – Paddy Oct 30 '17 at 06:56
  • Sorry, I still has issue when the data is large to fit in a page, it is not showing the data in page2. Please find the demo https://plnkr.co/edit/FgG8ooVugPO5XnIj9d4M?p=preview. The data is large to fit in page1 and its not displaying information which should be printed. @Paddy – user8838953 Nov 01 '17 at 16:53