package cn.com.lzt.common.util; import com.lowagie.text.*; import com.lowagie.text.pdf.PdfWriter; import org.apache.commons.io.IOUtils; import org.jeecgframework.core.util.ResourceUtil; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.Collections; import java.util.List; public class PdfUtil { public static PdfConfig defalutConfig = new DefaultPdfConfig(); public static void generateToStream (List elementList, PdfConfig config, OutputStream outputStream){ generateListToStream(Collections.singletonList(elementList),config,outputStream); } public static void generateListToStream(List> elementListCollection, PdfConfig config, OutputStream outputStream){ if(config==null) config=defalutConfig; Document doc = new Document(config.getRect()); PdfWriter writer =null; try { writer = PdfWriter.getInstance(doc,outputStream); doc.open(); // doc.setMargins(10, 10, 10, 10); int i=0; for(List elementList:elementListCollection) { for (Element e : elementList) { doc.add(e); } i++; if(i!=elementListCollection.size()) { doc.newPage(); } } } catch (DocumentException e) { e.printStackTrace(); }finally { doc.close(); } } //直接查看不下载 public static void view(List> elementListCollection,HttpServletResponse response){ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] bytes = null; try{ generateListToStream(elementListCollection,null,outputStream); bytes= outputStream.toByteArray(); response.setContentType("application/pdf"); response.setContentLength(bytes.length); response.getOutputStream().write(bytes); response.getOutputStream().flush(); } catch (Exception e) { e.printStackTrace(); }finally { IOUtils.closeQuietly(outputStream); } } public static void export(List> elementListCollection,HttpServletResponse response, String fileName) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] bytes = null; try{ generateListToStream(elementListCollection,null,outputStream); bytes= outputStream.toByteArray(); if (isIE()) { fileName = java.net.URLEncoder.encode(fileName, "UTF-8"); } else { fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1"); } response.setHeader("Content-Disposition", "attachment;filename="+ fileName +".pdf"); response.setContentType("application/pdf"); response.setContentLength(bytes.length); response.getOutputStream().write(bytes); response.getOutputStream().flush(); } catch (Exception e) { e.printStackTrace(); }finally { IOUtils.closeQuietly(outputStream); } } static boolean isIE() { HttpServletRequest request= ResourceUtil.getRequest(); return (request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0 || request.getHeader("USER-AGENT").toLowerCase().indexOf("rv:11.0") > 0) ? true : false; } public static class PdfConfig{ private Rectangle rect; public Rectangle getRect() { return rect; } public void setRect(Rectangle rect) { this.rect = rect; } } private static class DefaultPdfConfig extends PdfConfig{ private static Rectangle DEFAULT_RECT = new Rectangle(PageSize.A4); public DefaultPdfConfig(){ setRect(DEFAULT_RECT); } } }