使用wkhtmltopdf完成文件的转换和下载
来源:互联网 发布:学生借款软件推荐 编辑:程序博客网 时间:2024/05/21 09:39
项目需求是将网页上的账单下载成pdf格式的,网上找了一下前辈用的工具,最后选择使用wkhtmltopdf。将自己写的代码简单的做个整理吧。
1、首先从前端传来需要转换的文件(我这边是在前端直接抓取了需要转换的网页内容,传给后台一个String进行处理)
/** * 将传入的源文件或者网络路径生成pdf * @param data 需要生成pdf的源文件或者网络路径 */ @RequestMapping(value="getPdf",method= RequestMethod.POST) @ResponseBody public ResultJson getPdf(HttpServletRequest request,HttpServletResponse response,String data){ ResultJson resultJson = new ResultJson(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss" ); //生成的文件为年月日+时分秒+4位随机数(1970-1-1到现在的毫秒数取余10000) String name = sdf.format(new Date())+currentTimeMillis()%10000; String destPathHtml =request.getSession().getServletContext().getRealPath("/")+"download/"+name+".html"; String destPathPdf =request.getSession().getServletContext().getRealPath("/")+"download/"+name+".pdf"; //将字符串写入到HTML文件中 StringToHtml.stringToHtml(data,destPathHtml); //将HTML文件转换为PDF文件 boolean isSuccess = Htmltopdf.convert(destPathHtml,destPathPdf); //下载文件 DownloadPdf.download(destPathPdf,response); //删除html文件 delFile(destPathHtml); if(isSuccess){ ControllerUtil.setSuccessJson(resultJson,isSuccess); return resultJson; }else{ return resultJson; } } public static void delFile(String file){ File f = new File(file); if(f.exists() && f.isFile()){ f.delete(); } }
2、将字符串写入到HTML文件中
/** * @author djg * 将传入的字符串写入到html文件中 */public class StringToHtml { public static void stringToHtml(String s,String path){ if(s != null){ try { File file = new File(path); if(!file.exists()){ File dir = new File(file.getParent()); dir.mkdirs(); file.createNewFile(); } FileOutputStream fileOutputStream = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream,"utf-8"); osw.write(s); osw.flush(); osw.close(); fileOutputStream.close(); }catch (Exception e){ e.printStackTrace(); } } }}
3、将HTML文件转换为PDF文件
public class Htmltopdf { /** *wkhtmltopdf在硬盘的安装路径 *///我这里是使用配置文件读取的 private static final String WK_HTML_TO_PDF = Global.getResource("wkhtmltopdf"); public static boolean convert(Object srcPath,String destPath){ File file = new File(destPath); File parent = file.getParentFile(); if(!parent.exists()){ parent.mkdirs(); } StringBuilder cmd = new StringBuilder();// cmd.append("C:/wkhtmltopdf/bin/wkhtmltopdf"); cmd.append(WK_HTML_TO_PDF); cmd.append(" "); cmd.append("--page-size A4"); cmd.append(" "); cmd.append(srcPath); cmd.append(" "); cmd.append(destPath); boolean result = true; try { Process process = Runtime.getRuntime().exec(cmd.toString()); HtmlToPdfInter error = new HtmlToPdfInter(process.getErrorStream()); HtmlToPdfInter output = new HtmlToPdfInter(process.getInputStream()); error.start(); output.start(); process.waitFor(); }catch (Exception e){ result = false; } return result; }// public static void main(String[] args){// String htmlUrl = "www.baidu.com";// String path = "f:\\pdf_test.pdf";// convert(htmlUrl , path );// }}
public class HtmlToPdfInter extends Thread{ private static final Logger LOG = LoggerFactory .getLogger(HtmlToPdfInter.class); private InputStream is; public HtmlToPdfInter(InputStream is) { this.is = is; } @Override public void run() { try { InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); br.readLine(); } catch (IOException e) { LOG.error(e.getMessage()); e.printStackTrace(); } }}
4、下载文件
** * @author djg * 下载pdf文件 */public class DownloadPdf { /** * 下载 * @param path 需要下载的文件的路径 * @param response */ public static void download(String path, HttpServletResponse response){ try{ File file = new File(path); String fileName = file.getName(); String ext = fileName.substring(fileName.lastIndexOf(".")+1).toUpperCase(); //以流的形式下载文件 InputStream is = new BufferedInputStream(new FileInputStream(file)); byte[] bytes = new byte[is.available()]; is.read(bytes); is.close(); //清空response response.reset(); //设置response的Header response.addHeader("Content-Disposition", "attachment;filename=" +new String(fileName.getBytes())); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(bytes); toClient.flush(); toClient.close(); }catch (Exception e){ e.printStackTrace(); } }}
码代码时间不长,哪里有问题希望看的兄弟指正。
阅读全文
0 0
- 使用wkhtmltopdf完成文件的转换和下载
- 使用FTP命令自动完成文件的上传和下载
- wkhtmltopdf参数设置和下载PDF
- 使用wkhtmltopdf把网页转换为pdf的经验总结
- Wkhtmltopdf的安装与使用
- PHP:使用PHPExcel完成电子表格文件的导出下载和导入操作
- Html2Pdf:C调用wkhtmltopdf的API来将Html转换为pdf文件
- 使用apache的IOUtils类完成--文件下载(FileDownload)程序
- 使用jmeter测试工具完成文件的下载
- Struts2完成文件的下载
- Struts2完成文件的下载
- Struts2完成文件的下载
- 使用Struts来完成文件下载
- 使用FileUpload组件完成文件下载
- 使用JAXB完成object和xml的转换
- 使用jaxb完成对象和xml的转换
- Android使用Downloadmanager进行下载时,鉴别取消下载和下载完成的广播
- CentOS6.3 下使用wkhtmltopdf(html转pdf文件)
- Windows环境下部署neo4j
- 如何授权企业证书,使用企业级的APP
- MySQL5.7安装与配置
- python格式化输出
- Android RadioButton和CheckBox类
- 使用wkhtmltopdf完成文件的转换和下载
- 客户区坐标与成屏幕坐标的相互转换
- CDN HTTPS安全加速基本概念、解决方案及优化实践
- 通过优化在UE4中实现良好性能和高质量视觉效果
- Python列表(list)
- Just another Robbery(概率dp)
- 网卡信息小程序
- 学习廖大的JS--第一节
- seetaface