<?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 = '合同文本附件'
         

            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({
                    content_id:28808
                    
                });
                           
                        var file_name = "宏菱融资租赁合同_陈凌旻_PRJ20200313167.pdf";
                       
                        var file_path = "D:/u01/hls_file/excel/71EBA20ED68B4DA3A4A18A78AA647336con28808.docx";
                     
                       
            			var outputfilepath = file_path + '_pdf';
            			var file_type = new File(outputfilepath);
            			
            			 	var pe = new PdfConvertEngine($instance('leaf.database.service.IDatabaseServiceFactory'), $instance('uncertain.ocm.IObjectRegistry'), $ctx.getData());
	                        var attachment_id = 2798;
	                        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);
	                        }    
            			 
 
                        resp.setHeader("Content-disposition", "attachment; filename=" + encodeURI(file_name + '.pdf', 'UTF-8'));
                        var os = resp.getOutputStream();
                        transfer(outputfilepath, os);
                        os.flush();
                   
            } catch (e) {
                println(e);
                var logger = $logger("server-script");
                logger.severe(e.message);
            }
        ]]></s:server-script>
    </a:init-procedure>
</a:service>