2017-10-28 106 views
0

當用戶單擊導出按鈕時,我將div內的數據導出爲PDF。我想顯示每個div內容以顯示在PDF中的單個頁面中。在每個頁面中分別顯示每個div內容

以上情景在演示https://plnkr.co/edit/KvkVlYmmmJiZ71sghb1l?p=preview

相同的時下面的代碼應用於工作,它無法工作。 演示這裏:https://plnkr.co/edit/P9nUSRY5TytkonM6dUHl?p=preview

js代碼:

$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'); 
      } 
    ); 
    } 

回答

1

不建議使用jquery這樣的角度的應用程序內。要知道爲什麼看這裏:Can we use both jQuery and Angular in our Web Application?

你想要做但是如果你把下列控制器是可能的:

$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 
    ); 
} 

你的主要問題是,當你在哪裏嘗試創建HTML字符串你僅使用$(this)$(this)爲您提供了一個jQuery對象。你想放在頁面上的字符串在這個對象中,並使用jquery .children()方法訪問。

+0

感謝您的解釋。我想使用addHTML而不是fromHTML。由於HTML有一些問題需要識別一些特殊字符。@ Paddy – user8838953

+0

我不得不使用addHTML來代替HTML。我試圖根據您的建議更新https://plnkr.co/edit/Fj9tlQ3GuqyAfGqnEIfU?p=preview,但使用addHTML,最後出現錯誤。有關如何使用addHTML實現它的任何建議。謝謝? @Paddy – user8838953

+0

你只想在第一頁上使用「randomText1」,還是你想要div裏面的所有內容?例如「這應該在第1頁 randomText1 選項1 選項2 2選項 qeqweqwe顯示 weqwewewew wewew wewqewq WEW ewewqe 我們」 – Paddy

0

這是)做的一種方式,而不是fromHTML()你問使用addHTML(:

$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); 
     }); 
    } 

我離開對方的回答讓人們可以看到這樣做的兩種方式。

+0

謝謝你的投入。使用上述代碼時,特殊字符不會被導入到PDF中,這就是我使用

的原因。請參閱演示https://plnkr.co/edit/J54E1Jworp3IQG04Z41C?p=preview,當點擊導出按鈕時,PDF中不顯示選項1和選項2顯示的項目符號和數字。@ Paddy – user8838953

+0

看起來像一個問題與html 2canvas https://github.com/niklasvh/html2canvas/issues/177 – Paddy

+0

我很抱歉,但我不知道如何解決這個問題。有人正在修復它,但看起來像它從來沒有完成:https://github.com/niklasvh/html2canvas/pull/486 – Paddy

相關問題