packagecom.sd.ddo.controller.pdf;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.net.URLDecoder;importjava.util.Iterator;importjava.util.Map;importjavax.imageio.stream.FileImageOutputStream;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServletResponse;importjavax.swing.filechooser.FileSystemView;importorg.springframework.stereotype.Controller;importorg.springframework.util.FileCopyUtils;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.ResponseBody;importorg.springframework.web.multipart.MultipartFile;importorg.springframework.web.multipart.MultipartHttpServletRequest;importorg.springframework.web.servlet.ModelAndView;importcom.alibaba.fastjson.JSONObject;importcom.lowagie.text.Document;importcom.lowagie.text.DocumentException;importcom.lowagie.text.Image;importcom.lowagie.text.pdf.PdfWriter;importcom.sd.cloud.util.alipay.HttpRequest;importcom.sd.cloud.util.alipay.HttpResponse;importcom.sd.cloud.util.common.CommonUtil;importcom.sd.cloud.util.common.SDConstants;/*** 导出pdf文档
*@authoryugb
* 将html,jsp页面转化为pdf文档*/@Controller
@RequestMapping("/pdftools")public classPDFExportController {/*** 导出pdf文档
*@paramrequest
*@paramresponse
*@throwsServletException
*@throwsIOException
*@throwsDocumentException*/@SuppressWarnings("rawtypes")
@RequestMapping("/exportPdf")public @ResponseBody Object exportPdf(MultipartHttpServletRequest request,HttpServletResponse response)throwsServletException, IOException, DocumentException {
JSONObject result= new JSONObject(); //自定义结果格式
try{
String filename= URLDecoder.decode(request.getParameter("filename"),"utf-8");
String filePath=SDConstants.fileDir+filename+".pdf";
String imagePath= SDConstants.fileDir +filename+".bmp";
Document document= newDocument();
Map getMap=request.getFileMap();
MultipartFile mfile= (MultipartFile) getMap.get("imgData"); //获取数据
InputStream file =mfile.getInputStream();byte[] fileByte =FileCopyUtils.copyToByteArray(file);
FileImageOutputStream imageOutput= new FileImageOutputStream(newFile(imagePath));
imageOutput.write(fileByte,0, fileByte.length);//生成本地图片文件
imageOutput.close();
PdfWriter.getInstance(document,new FileOutputStream(filePath)); //itextpdf文件
document.open();
Image image= Image.getInstance(imagePath); //itext-pdf-image
float heigth =image.getHeight();float width =image.getWidth();int percent = getPercent(heigth, width); //按比例缩小图片
image.setAlignment(Image.MIDDLE);
image.scalePercent(percent+3);
document.add(image);
document.close();
result.put("fileName", filename+".pdf");
result.put("imageName", filename+".bmp");
result.put("mesg", "success");
}catch(DocumentException de) {
System.err.println(de.getMessage());
}catch(Exception e) {
e.printStackTrace();
result.put("mesg", "error");
}returnresult;
}/*** 获得百分比
*@paramh
*@paramw
*@return
*/
private static int getPercent(float h, floatw) {int p = 0;float p2 = 0.0f;
p2= 560 / w * 100;
p=Math.round(p2);returnp;
}
}