您的位置:首页 > Web前端 > AngularJS

Angularjs ui-grid pdfmake.js 导出excel/pdf及pdf中文问题

2016-07-20 00:00 645 查看
摘要: 1.最近在做angularjs ui-grid 导出excel/ pdf 功能时遇到导出excel cvs文件正常,在导出pdf时发现中文表头导出后为空问题。
这个问题原因: ui-grid 导出pdf 使用了pdfmake.js 这个开源插件,这个插件所提供的字体vfs_fonts.js 是不支持中文的,需要自定义fonts 。

ui-grid使用介绍看这篇博客:http://my.oschina.net/gmd/blog/670895

一. 处理Angularjs ui-grid 导出pdf 无法显示中文问题:

第一步:pdfmake 自定义支持中文的字体

pdfmake 官网 : http://pdfmake.org/#/

pdfmake自定义字体:https://github.com/bpampuch/pdfmake/wiki/Custom-Fonts---client-side

需要下载node.js,然后下载bower,然后下载这个pdfmake的zip并解压,再安装grunt,在pdfmake这个目录下执行grunt dump_dir命令,但是你会发现缺少各种grunt插件,然后你要一个一个安装grunt插件。最后把你想要的支持中文的ttf文件放到pdfmake的examples\fonts目录下,然后执行grunt dump_dir命令,它不报错的情况下会把ttf文件加到build/vfs_fonts.js文件中,然后将vfs_fonts.js拷贝到工程中在html中引入这个js。

第二步: 引入js

pdfmake.min.js 导出pdf插件

vfs_fonts.js 就是第1步自定义的字体js文件

<!-- ui-grid export -->
<script src="../pdfmake/pdfmake.min.js"></script>
<script src="../pdfmake/vfs_fonts.js"></script>
<script src="../ui-grid/csv.js"></script>

第三步: 在你调用createPdf方法执行以下js,注:simblack是自定义的字体。

window.pdfMake.fonts={
simblack: {
normal: 'simpleblack.ttf',
bold: 'simpleblack.ttf',
italics: 'simpleblack.ttf',
bolditalics: 'simpleblack.ttf'
},
Roboto: {
normal: 'Roboto-Regular.ttf',
bold: 'Roboto-Medium.ttf',
italics: 'Roboto-Italic.ttf',
bolditalics: 'Roboto-Italic.ttf'
}
}

第四步: 定义gridOptions 时指定字体为simblack:

exporterPdfDefaultStyle : {font:'simblack',fontSize: 9}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息