js生成pdf文件,支持中文字体,插入图片

来源:互联网 发布:映像淘宝视频 编辑:程序博客网 时间:2024/05/05 12:38
先引入两个js文件:
<script src="/js/pdfmake.min.js"></script>

<script src="/js/vfs_fonts.js"></script>

下载地址:http://download.csdn.net/detail/yuanyuan171601/9753111

<button onclick="download()">下载PDF文件</button>
<script>    function download() {        var x = new ImageDataURL(["../images/wc_logo.png"]);//插入的图片路径,可以插入多张图片        x.oncomplete = function() {            var imgs = new Array();            console.log("complete");            for (key in this.imgdata) {                if (this.imgdata[key] == this.emptyobj)//不存在的图片直接忽略                    continue;
imgs.push({image:this.imgdata[key],margin:[ 150, -100, 0, 0 ]});
//pdfmake的图片格式{image:image dataurl}                //margin调整位置,四个坐标依次为左上右下    }
var dd = {
content: [
{text:'比希望更炽热的',fontSize: 12,margin:[ 300, 0, 0, 0 ]},
 {text:'比绝望更深邃的',fontSize: 12,margin:[ 300, 0, 0, 0 ]},
               {text:'是爱啊',fontSize: 20},
 imgs,
        ],
       defaultStyle: {
  font: '微软雅黑'
       }
   };
     pdfMake.fonts = {
           微软雅黑: {
        normal: 'msyh.ttf',
           bold: 'msyh.ttf',
          italics: 'msyh.ttf',
          bolditalics: 'msyh.ttf'
       }
  };
 pdfMake.createPdf(dd).download();
      }
   }

关于pdf内容的样式的调整可以参考pdfmake的官方文档:http://pdfmake.org/#/gettingstarted

0 0
原创粉丝点击