PdfUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package cn.com.lzt.common.util;
  2. import com.lowagie.text.*;
  3. import com.lowagie.text.pdf.PdfWriter;
  4. import org.apache.commons.io.IOUtils;
  5. import org.jeecgframework.core.util.ResourceUtil;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.*;
  9. import java.util.Collections;
  10. import java.util.List;
  11. public class PdfUtil {
  12. public static PdfConfig defalutConfig = new DefaultPdfConfig();
  13. public static void generateToStream (List<Element> elementList, PdfConfig config, OutputStream outputStream){
  14. generateListToStream(Collections.singletonList(elementList),config,outputStream);
  15. }
  16. public static void generateListToStream(List<List<Element>> elementListCollection, PdfConfig config, OutputStream outputStream){
  17. if(config==null) config=defalutConfig;
  18. Document doc = new Document(config.getRect());
  19. PdfWriter writer =null;
  20. try {
  21. writer = PdfWriter.getInstance(doc,outputStream);
  22. doc.open();
  23. // doc.setMargins(10, 10, 10, 10);
  24. int i=0;
  25. for(List<Element> elementList:elementListCollection) {
  26. for (Element e : elementList) {
  27. doc.add(e);
  28. }
  29. i++;
  30. if(i!=elementListCollection.size()) {
  31. doc.newPage();
  32. }
  33. }
  34. } catch (DocumentException e) {
  35. e.printStackTrace();
  36. }finally {
  37. doc.close();
  38. }
  39. }
  40. //直接查看不下载
  41. public static void view(List<List<Element>> elementListCollection,HttpServletResponse response){
  42. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  43. byte[] bytes = null;
  44. try{
  45. generateListToStream(elementListCollection,null,outputStream);
  46. bytes= outputStream.toByteArray();
  47. response.setContentType("application/pdf");
  48. response.setContentLength(bytes.length);
  49. response.getOutputStream().write(bytes);
  50. response.getOutputStream().flush();
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }finally {
  54. IOUtils.closeQuietly(outputStream);
  55. }
  56. }
  57. public static void export(List<List<Element>> elementListCollection,HttpServletResponse response, String fileName) {
  58. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  59. byte[] bytes = null;
  60. try{
  61. generateListToStream(elementListCollection,null,outputStream);
  62. bytes= outputStream.toByteArray();
  63. if (isIE()) {
  64. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
  65. } else {
  66. fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
  67. }
  68. response.setHeader("Content-Disposition", "attachment;filename="+ fileName +".pdf");
  69. response.setContentType("application/pdf");
  70. response.setContentLength(bytes.length);
  71. response.getOutputStream().write(bytes);
  72. response.getOutputStream().flush();
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }finally {
  76. IOUtils.closeQuietly(outputStream);
  77. }
  78. }
  79. static boolean isIE() {
  80. HttpServletRequest request= ResourceUtil.getRequest();
  81. return (request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0 || request.getHeader("USER-AGENT").toLowerCase().indexOf("rv:11.0") > 0) ? true : false;
  82. }
  83. public static class PdfConfig{
  84. private Rectangle rect;
  85. public Rectangle getRect() {
  86. return rect;
  87. }
  88. public void setRect(Rectangle rect) {
  89. this.rect = rect;
  90. }
  91. }
  92. private static class DefaultPdfConfig extends PdfConfig{
  93. private static Rectangle DEFAULT_RECT = new Rectangle(PageSize.A4);
  94. public DefaultPdfConfig(){
  95. setRect(DEFAULT_RECT);
  96. }
  97. }
  98. }