java html转化为pdf文件_javascript+java实现html转化为PDF文档

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值