首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jsPDF将html转换为pdf

jsPDF将html转换为pdf
EN

Stack Overflow用户
提问于 2014-09-22 08:47:18
回答 2查看 6.7K关注 0票数 1

我正在尝试将HTML转换为PDF格式,我在ASP.NET MVC5中工作,下面是我的代码:

代码语言:javascript
复制
    @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>

}

使用第一个div时,我会收到一条消息。

代码语言:javascript
复制
TypeError: renderer.pdf.table is not a function

,但是第二个div可以正常工作。

有人知道吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-25 10:00:49

我通过改变插件的版本来解决这个问题。

票数 1
EN

Stack Overflow用户

发布于 2015-04-30 16:19:18

总之,我遇到了同样的问题,结果发现我错过了位于这里的jspdf.plugin.cell.js插件。

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

希望这有帮助

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25969956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档