pdf_test.lsc 4.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
    <a:init-procedure>
        <s:server-script import="jacob/jacob.js"><![CDATA[
            importPackage(java.util.zip);
            importPackage(java.io);
            importPackage(Packages.hls.plugin.docx4j);
            importPackage(Packages.hls.plugin.jacob.engine);
            //importPackage(org.apache.tools.zip); /*可以传入参数*/
            //importPackage(java.util.zip);
            
            function writeFile(zos, fn, fp) {
                var ze = new ZipEntry(fn);
                //zos.setEncoding("UTF-8");//如果是org.apache.tools.zip需要追加字符集
                zos.putNextEntry(ze);
                var fis = new FileInputStream(fp);
                var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                var len = -1;
                while ((len = fis.read(b)) != -1) {
                    zos.write(b, 0, len);
                }
                fis.close();
            }
            
            
            function transfer(file, os) {
                var fis = new FileInputStream(file);
                var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                var len = -1;
                while ((len = fis.read(b)) != -1) {
                    os.write(b, 0, len);
                }
                fis.close();
            }
            
            
            function getdate() {
                var now = new Date()
                y = now.getFullYear()
                m = now.getMonth() + 1
                d = now.getDate()
                m = m < 10 ? "0" + m : m
                d = d < 10 ? "0" + d : d
                return y + "" + m + "" + d
            }
            $ctx["__request_type__"] = 'file'; //to indicate this request is not a JSON_REQUEST
            var resp = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
            resp.setHeader("Pragma", "No-cache");
            resp.setHeader("Cache-Control", "no-cache, must-revalidate");
            var date_str = getdate();
            var doc_code = '合同文本附件'
52 53
         

54 55 56 57 58 59
            resp.setDateHeader("Expires", 0);
            resp.setContentType("application/x-msdownload");
            
            try {
                var attachment_batch_dl = $bm('cont.CON500.con_contract_content_download');
                var result = attachment_batch_dl.queryAsMap({
60 61
                    content_id:28808
                    
62
                });
63 64 65 66 67 68
                           
                        var file_name = "宏菱融资租赁合同_陈凌旻_PRJ20200313167.pdf";
                       
                        var file_path = "D:/u01/hls_file/excel/71EBA20ED68B4DA3A4A18A78AA647336con28808.docx";
                     
                       
69 70
            			var outputfilepath = file_path + '_pdf';
            			var file_type = new File(outputfilepath);
71 72 73
            			
            			 	var pe = new PdfConvertEngine($instance('leaf.database.service.IDatabaseServiceFactory'), $instance('uncertain.ocm.IObjectRegistry'), $ctx.getData());
	                        var attachment_id = 2798;
74 75 76 77 78 79 80 81
	                        var server = jacob_config.jacobUrl + attachment_id;
	                        var target_path = jacob_config.target_path;
	                        var url = jacob_config.jacobserviceUrl + '?target_url=' + server + '&target_path=' + target_path;
	                        try {
	                            pe.run(url);
	                        } catch (e) {
	                            raise_app_error('转化PDF失败:' + e);
	                        }    
82 83
            			 
 
84 85
                        resp.setHeader("Content-disposition", "attachment; filename=" + encodeURI(file_name + '.pdf', 'UTF-8'));
                        var os = resp.getOutputStream();
86
                        transfer(outputfilepath, os);
87
                        os.flush();
88
                   
89 90 91 92 93 94 95 96
            } catch (e) {
                println(e);
                var logger = $logger("server-script");
                logger.severe(e.message);
            }
        ]]></s:server-script>
    </a:init-procedure>
</a:service>