用JavaScript生成PDF、word。。。
阿新 • • 發佈:2019-02-09
JavaScript幾乎是全能的,但用它生成pdf和word文件還是難為它了。
XDOC使這個成為可能,你可以任意獲取頁面的內容,組織成為一個漂亮的PDF或word文件,廢話少說,看看效果:
看看程式碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>XDOC</title> </head> <body> <script type="text/javascript" src="http://localhost:8080/xdoc/xdoc.js" charset="UTF-8"></script> <script type="text/javascript"> //設定XDocServer地址 XDoc.server = "http://localhost:8080/xdoc"; //建立XDOC var xdoc = new XDoc.xdoc(); //元資料 xdoc.meta = new XDoc.meta({author:"xdoc",title:"XDOC JavaScript 文件模型"}); //徽標 xdoc.back.add(new XDoc.img("http://www.baidu.com/img/baidu_sylogo1.gif")); //水印 xdoc.back.add(new XDoc.stext("內部資料", {color:"",fillColor:"#f0f0f0",bold:true,width:400,height:200,rotate:-45,dock:"center"})); //頁碼 var rect = new XDoc.rect({width:200,height:60,color:"",dock:"bottomcenter"}); var para = new XDoc.para({align:"center"}); para.add(new XDoc.text("第#pageno#頁/共#pagecount#頁")); rect.add(para); xdoc.back.add(rect); //內容 xdoc.body.add(new XDoc.para({heading:1,lineSpacing:20}, new XDoc.text("XDOC JavaScript 文件模型", {fontSize:28}))); xdoc.body.add(new XDoc.para({heading:2,lineSpacing:18}, new XDoc.text("段落文字", {fontSize:26}))); para = new XDoc.para({indent:40}, new XDoc.text("縮排40")); xdoc.body.add(para); para.add(new XDoc.text("隸書18", {fontName:"隸書", fongSize:18})); para.add(new XDoc.text("粗體", {fontStyle:"bold"})); para.add(new XDoc.text("紅色下劃線", {fontStyle:"underline_red"})); para.add(new XDoc.text("斜體紅字黃背景", {fontStyle:"italic",fontSize:20,fontColor:"red",backColor:"yellow"})); para.add(new XDoc.text("超連結", {fontStyle:"underline",fontColor:"blue",href:"http://www.hgsql.com"})); xdoc.body.add(new XDoc.para({align:"center",prefix:"■ ",lineSpacing:10,backColor:"lightGray"}, new XDoc.text("居中帶字首,行間距10,背景亮灰色"))); xdoc.body.add(new XDoc.para({heading:2,lineSpacing:18}, new XDoc.text("圖片、圖表、條形碼", {fontSize:26}))); xdoc.body.add(new XDoc.para({}, new XDoc.img("http://www.baidu.com/img/baidu_sylogo1.gif"))); xdoc.body.add(new XDoc.para({}, new XDoc.chart("類別,值\nA,10\nB,20\nC,30"))); xdoc.body.add(new XDoc.para({}, new XDoc.barcode("1234567890", {href:"http://www.hgsql.com"}))); var table = new XDoc.table({sizeType:"autoSize"}); for (var i = 0; i < 6; i++) { for (var j = 0; j < 6; j++) { table.add(new XDoc.text(i + "," + j), i, j); } } table.add(new XDoc.text("合併單元格"), 1, 2, 2,2); table.add(new XDoc.img("http://www.baidu.com/img/baidu_sylogo1.gif",{sizeType:"normal",color:"#000000"}), 3, 1, 2,2); xdoc.body.add(new XDoc.para({}, table)); //顯示 //xdoc.show(); //轉換為PDF xdoc.toPdf(); //轉換為Word2007 //xdoc.toDocx(); //列印 //xdoc.print(); </script> </body> </html>