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>