jquery插件導出excel和pdf(解決中文亂碼問題)


參考文件:http://jackyrong.iteye.com/blog/2169683

               https://my.oschina.net/aruan/blog/418980

    https://segmentfault.com/a/1190000013168209

js引用文件地址:https://files.cnblogs.com/files/likui-bookHouse/tableExport.jquery.plugin-master.rar

html文件代碼:

<html>
<head>
    <title>Export html table to excel and csv using jquery</title>
    <script src="~/Content/js/jquery-1.7.1.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
    <script type="text/javascript" src="~/Content/js/excel/jquery.base64.js"></script>

    <script type="text/javascript" src="~/Content/js/excel/tableExport.js"></script>

    <script type="text/javascript" src="~/Content/js/excel/jspdf/libs/sprintf.js">

    </script>

    <script type="text/javascript" src="~/Content/js/excel/jspdf/jspdf.js"></script>
    <script type="text/javascript" src="~/Content/js/excel/jspdf/libs/base64.js"></script>

</head>
<body>
    [align=right]
    <br><br><br>
    <button class="btn btn-success" onClick="$('#customers').tableExport({type: 'excel', escape: 'false'});">Excel Export</button>
    <button class="btn btn-success" onClick="$('#customers').tableExport({type: 'pdf', escape: 'false'});">CSV Export</button>
    <br><br>
    [/align]
    <table id="customers" class="table table-striped table-bordered">
        <thead>
            <tr class='warning'>
                <th>Country</th>
                <th>Population</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Chinna</td>
                <td>1,363,480,000</td>
                <td>March 24, 2014</td>
            </tr>
            <tr>
                <td>India</td>
                <td>1,241,900,000</td>
                <td>March 24, 2014</td>
            </tr>
            <tr>
                <td>United States</td>
                <td>317,746,000</td>
                <td>March 24, 2014</td>
            </tr>
            <tr>
                <td>Indonesia</td>
                <td>249,866,000</td>
                <td>July 1, 2013</td>
            </tr>
            <tr>
                <td>Brazil</td>
                <td>201,032,714</td>
                <td>July 1, 2013</td>
            </tr>
            <tr>
                <td>澳大利亞</td>
                <td>2018年3月20日</td>
                <td>二零一八年三月二十日</td>
            </tr>
        </tbody>
    </table>
    </div>
</body>
</html> 

顯示效果:

 

導出pdf(解決中文亂碼問題)

導出過程中發現中文顯示亂碼,根據文檔發現jsPDF不支持中文,網上資料是使用html2canvas方式轉換canva方式,並不是十分靈活。后根據項目jsPDF-CustomFonts-support引入中文字體,解決了導出pdf后中文字體顯示亂碼的問題。
將項目源碼下載到本地,dist文件夾下為相關腳本,font文件夾下為相關字體文件。

腳本實現:tabExport.js 中的代碼

}else if(defaults.type == 'pdf'){
    
                    //var doc = new jsPDF('p','pt', 'a4', true);
                    //doc.setFontSize(defaults.pdfFontSize);

                    var doc = new jsPDF('p', 'pt', 'a6');      //a4:表示打印的pdf紙張大小(這個設置得越大,會顯得內容越小)
                    doc.addFont('NotoSansCJKtc-Regular.ttf', 'NotoSansCJKtc', 'normal');     //添加字體
                    //pdf標題設置
                    doc.setFont('NotoSansCJKtc');                         //設置字體
                    //pdf標題設置
                    doc.text(20, 20, '導出標題');       //並且設置字體大小為5。(之前采用它默認的字體大小,打印的pdf字體都堆在一起了。都是淚啊!)
                    doc.setFontSize(5);
                    

 

好了,pdf文件打印出來了,看效果吧!

 

 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第二種打印pdf的方法(把文字部分轉換為圖片類型,然后再打印成pdf文件)

4、具體操作
1)下載jspdf插件,官網有。
2)html頁面引用兩個js文件 jspdf.debug.js  和 html2canvas.js  (利用該插件將html頁面轉化成圖片,在插入到pdf中)

這里我沒引入jspdf.debug.js,而是引用的jspdf.js(它們都包含了所有文件)

3)編寫一個js方法 即可實現 轉化pdf。並可以指定導出區域。

 

 

pdf.js中的方法代碼:

function exportPhotoPdf() {
    if(confirm("您確認下載打印pdf的功能模板?")){
        var pdf = new jsPDF('p','pt','a4');
        // 設置打印比例 越大打印越小
        pdf.internal.scaleFactor = 2;
        var options = {
            pagesplit: true,             //設置是否自動分頁
            "background": '#FFFFFF'       //如果導出的pdf為黑色背景,需要將導出的html模塊內容背景 設置成白色。
        };
        var printHtml = $('#customers').get(0);   // 頁面某一個div里面的內容,通過id獲取div內容
        pdf.addHTML(printHtml,15, 15, options,function() {
            pdf.save('pdf打印功能測試.pdf');
        });
    }
}
    

點擊打印按鈕,打印效果如下:

總結:這種方法導出的pdf格式效果最理想,但是不支持中文。並且相對於第一種方法,清晰度不夠。第一種方法雖然很清晰,但樣式消失了。算是各有各的優點吧!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM