使用jspdf(Laravel)将{html表转换为pdf

时间:2017-10-07 19:13:12

标签: php twitter-bootstrap laravel pdf jspdf

美好的一天。如何使用Jspdf将我的html表转换为pdf?我使用laravel,而我的html表使用bootstrap css框架。问题是我能够下载pdf,但表的结构是无组织的。如何根据确切的格式和表格正确获取我的pdf:

这是我的html表格代码:

<table class="table table-bordered table-condensed table-responsive" id="test">
    <thead>
        <tr>
            <th scope="row" colspan="1">Name</th>
            <td colspan="5">{{$student->first_name." ".$student->middle_name." ".$student->surname}}</td>
        </tr>
        <tr>
            <th scope="row">Grade/Class</th>
            <td>{{$student->grade->name}}</td>

            <th scope="row">Term</th>
            <td>{{$term->name}}</td>

            <th scope="row">Date</th>
            <td>{{$date->toFormattedDateString()}}</td>
        </tr>
        <tr>
            <th class="text-center" colspan="2">Subject</th>
            <th class="text-center" colspan="4">Score</th>
        </tr>
    </thead>
    <tbody>
        @foreach($scores as $score)
            <tr>
                <td scope="row" colspan="2">{{$score->subject}}</td>
                @if($score->score <= 69)
                    <td style="color: red;" colspan="4">{{$score->score}}</td>
                @else 
                    <td colspan="4">{{$score->score}}</td>
                @endif
            </tr>
        @endforeach
        <tr>
            <th scope="row" colspan="2" class="text-right">Average</th>
            <td colspan="4" class="text-right"><strong>{{$average}}</strong></td>
        </tr>
    </tbody>
</table>

表图:

enter image description here

而是这是我从jspdf获得的输出:

enter image description here

我的剧本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.debug.js"></script>

<script type="text/javascript">   
    $('#download_pdf').click(function () {
        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 = $('#test')[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);

    });
</script>

0 个答案:

没有答案