Angularjs ui-grid pdfmake.js 導出excel/pdf及pdf中文問題

分類:IT技術 時間:2016-10-11

一. 處理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="http://my.oschina.net/gmd/pdfmake/pdfmake.min.js"></script>
	<script src="http://my.oschina.net/gmd/pdfmake/vfs_fonts.js"></script>
	<script src="http://my.oschina.net/gmd/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}

 


Tags: examples excel export normal 中文

文章來源:


ads
ads

相關文章
ads

相關文章

ad