| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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<Element> elementList, PdfConfig config, OutputStream outputStream){
- generateListToStream(Collections.singletonList(elementList),config,outputStream);
- }
- public static void generateListToStream(List<List<Element>> 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<Element> 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<List<Element>> 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<List<Element>> 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);
- }
- }
- }
|