package com.jeeplus.modules.esignature.service.docdeal;

import com.alibaba.fastjson.JSONObject;
import com.jeeplus.modules.esignature.kinggridserver.KingGridServer;
import com.jeeplus.modules.esignature.utils.HttpRequest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 文档转换接口
 * @author Administrator
 *
 */
public class DocConvertPdfService {
	public static void main(String[] args) throws IOException {
		ms_to_pdf("d:/test.pdf");
	}
	
	
	/**
	 * office文件数据填充生成PDF
	 * @param saveFile
	 * @throws IOException
	 */
	public static void ms_to_pdf(String saveFile) throws IOException{
		HttpRequest request = KingGridServer.createHttpRequest("/api/cnt/msToPDF");
		InputStream is = null;
		try{
			is = DocConvertPdfService.class.getResourceAsStream("/demo.doc");
			request.addInputStream("ms_file", "demo.doc", is);
			
			request.send();
			int code = request.getResponseCode();
			System.out.println("状态码："+code);
			if(code==200){
				System.out.println("生成成功");
				FileOutputStream fos = null;
				try{
					File file  = new File(saveFile);
					file.getParentFile().mkdirs();
					fos = new FileOutputStream(file);
					System.out.println("保存文件至："+saveFile);
					request.result(fos);
				}finally{
					if(fos!=null){
						fos.close();
					}
				}
			}else{
				System.out.println("错误信息："+request.resultToString());
			}
			
		}finally{
			if(is!=null){
				is.close();
			}
		}
	}
	
	/**
	 * docx模板文件数据填充生成PDF
	 * @param saveFile
	 * @throws IOException
	 */
	public static void docx_to_pdf(String saveFile) throws IOException{
		HttpRequest request = KingGridServer.createHttpRequest("/api/cnt/docxToPDF");
		InputStream is = null;
		try{
			is = DocConvertPdfService.class.getResourceAsStream("/demo.docx");
			request.addInputStream("docx_file", "demo.docx", is);
			
			//{"姓名":"李东长","cardname_ID":"360111198708080899","year":"2018","month":"01","day":"01"}
			JSONObject xml_metadata = new JSONObject(true);
			xml_metadata.put("姓名", "李东长");
			xml_metadata.put("cardname_ID", "360111198708080899");
			xml_metadata.put("year", "2018");
			xml_metadata.put("month", "01");
			xml_metadata.put("day", "01");
			
			request.addParam("docx_metadata",xml_metadata.toJSONString());
			request.send();
			int code = request.getResponseCode();
			System.out.println("状态码："+code);
			if(code==200){
				System.out.println("生成成功");
				FileOutputStream fos = null;
				try{
					File file  = new File(saveFile);
					file.getParentFile().mkdirs();
					fos = new FileOutputStream(file);
					System.out.println("保存文件至："+saveFile);
					request.result(fos);
				}finally{
					if(fos!=null){
						fos.close();
					}
				}
			}else{
				System.out.println("错误信息："+request.resultToString());
			}
			
		}finally{
			if(is!=null){
				is.close();
			}
		}
	}
	
	/**
	 * xml模板文件数据填充生成PDF
	 * @param saveFile
	 * @throws IOException
	 */
	public static void xml_to_pdf(String saveFile) throws IOException{
		HttpRequest request = KingGridServer.createHttpRequest("/api/cnt/xmlToPDF");
		InputStream is = null;
		try{
			is = DocConvertPdfService.class.getResourceAsStream("/demo.xml");
			request.addInputStream("xml_file", "demo.xml", is);
			
			//{"姓名":"李东长","cardname_ID":"360111198708080899","year":"2018","month":"01","day":"01"}
			JSONObject xml_metadata = new JSONObject(true);
			xml_metadata.put("姓名", "李东长");
			xml_metadata.put("cardname_ID", "360111198708080899");
			xml_metadata.put("year", "2018");
			xml_metadata.put("month", "01");
			xml_metadata.put("day", "01");
			
			request.addParam("xml_metadata",xml_metadata.toJSONString());
			request.send();
			int code = request.getResponseCode();
			System.out.println("状态码："+code);
			if(code==200){
				System.out.println("生成成功");
				FileOutputStream fos = null;
				try{
					File file  = new File(saveFile);
					file.getParentFile().mkdirs();
					fos = new FileOutputStream(file);
					System.out.println("保存文件至："+saveFile);
					request.result(fos);
				}finally{
					if(fos!=null){
						fos.close();
					}
				}
			}else{
				System.out.println("错误信息："+request.resultToString());
			}
			
		}finally{
			if(is!=null){
				is.close();
			}
		}
	}
}
