2014-09-22 96 views
1

我試圖將HTML轉換成PDF和我在ASP.NET MVC5工作是我的代碼:
jsPDF將HTML轉換成PDF

@model Ebok.Models.AtmClaimReportDetailsInfoViewModel 

@{ 
    ViewBag.Title = "Dzienny raport rozliczenia transakcji bankomatu "+Model.AtmPid+" sieci "+Model.CustomerNumber; 
} 
<div style="border-width: 2px; padding: 1em; font-size:120%;line-height: 1.5em;" id="fromHTMLtestdiv"> // `<-- THIS DIV NOT WORK` 

    <h3 class="text-align-center ">Dzienny raport rozliczenia transakcji bankomatu @Model.AtmPid sieci @Model.CustomerNumber</h3> 

    <div class="row"> 
     <div class="text-align-center col-xs-5"> 

      <div class="text-align-center padding-top-10"> 
       @Model.CompanyNameString 

       <br /> 
       NIP: @Model.Nip 
      </div> 
     </div> 

     <div class="text-align-center col-xs-5"> 
      Rozliczenie bankomatu @Model.AtmPid sieci @Model.CustomerNumber 
      za dzień: @Model.BusinessDate.ToShortDateString() <br /> 
      Wygenerowano dnia: @Model.CreationDate.ToShortDateString() 
     </div> 

    </div> 


     <table class="table table-bordered"> 
      <thead> 
       <tr> 
        <th> Sieć</th> 
        <th> ATM</th> 
        <th> Adres</th> 
        <th> Kwota korekty (uznanie)</th> 
        <th> Kwota transakcji</th> 
        <th> Data i godzina transakcji</th> 
       </tr> 
      </thead> 
      <tbody> 
       @Html.Partial("ClaimReportsDetailsPartial") 
      </tbody> 
     </table> 

    <button onclick="javascript:demoFromHTML()" class="button">Run Code</button> 

    <div id="fromHTMLtestdiv"> // **THIS DIV WORK FINE** 
     <h1> 
      We support special element handlers. Register them with jQuery-style. 
      <a>asdsdsd</a> 
     </h1> 
    </div> 
</div> 

<div class="form-actions"> 
    <button class="print-link btn btn-info" onclick="jQuery.print('#printClaimReport')"><i class="glyphicon glyphicon-print"></i></button> 

    <input type="button" value="Powrót" onclick="location.href='@Url.Action("Index")'" class="btn btn-info" /> 
</div> 

@section Scripts { 

<script type='text/javascript'> 
    function demoFromHTML() { 
     var pdf = new jsPDF('p', 'pt', 'letter') 

     // source can be HTML-formatted string, or a reference 
     // to an actual DOM element from which the text will be scraped. 
     , source = $('#fromHTMLtestdiv')[0] 

     // we support special element handlers. Register them with jQuery-style 
     // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.) 
     // There is no support for any other type of selectors 
     // (class, of compound) at this time. 
     , specialElementHandlers = { 
      // element with id of "bypass" - jQuery style selector 
      '#bypassme': function (element, renderer) { 
       // true = "handled elsewhere, bypass text extraction" 
       return true 
      } 
     } 

     margins = { 
      top: 80, 
      bottom: 60, 
      left: 40, 
      width: 522 
     }; 
     // all coords and widths are in jsPDF instance's declared units 
     // 'inches' in this case 
     pdf.fromHTML(
      source // HTML string or DOM elem ref. 
      , margins.left // x coord 
      , margins.top // y coord 
      , { 
       'width': margins.width // max width of content on PDF 
       , 'elementHandlers': specialElementHandlers 
      }, 
      function (dispose) { 
       // dispose: object with X, Y of the last line add to the PDF 
       //   this allow the insertion of new lines after html 
       pdf.save('Test.pdf'); 
      }, 
      margins 
     ) 
     pdf.output('dataurl'); 
    } 


</script> 

} 

當使用我收到一條消息

TypeError: renderer.pdf.table is not a function 
第一個div

,但第二個div工作正常。

有沒有人有任何想法?

+0

我解決了這個通過更改插件的版本來解決問題。 – 2014-09-22 09:46:28

+0

這包括你如何解決你的答案,以正確回答你的問題。 – bot 2014-09-24 01:16:26

回答

1

我通過更改插件的版本解決了這個問題。

+0

請解釋一下你的意思;哪個插件和哪個版本?我正在使用剛剛下載的JSPDF並獲取此錯誤。 – 2015-03-09 18:10:15

4

所有, 我遇到了同樣的問題,它原來我錯過了jspdf.plugin.cell.js插件,設在這裏

https://github.com/MrRio/jsPDF/blob/master/jspdf.plugin.cell.js

希望幫助

+1

雖然此鏈接可能會回答問題,但最好在此處包含答案的重要部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – 2015-04-30 16:37:51

+2

@DavidGrinberg,我相信我那樣做了.. 1)我說缺少一個插件文件,然後2)指向文件 作爲參考 – 2015-04-30 16:53:23

+0

謝謝,這正是我的問題! – Shryme 2015-06-17 13:30:26