zip a pdf file created using itext

删除回忆录丶 提交于 2021-01-28 15:10:29

问题


    response.setHeader("Content-Disposition", "attachment; filename=r.pdf");
    Document document1 = new Document(PageSize.A4);
    PdfWriter pdfWriter=PdfWriter.getInstance(document1, response.getOutputStream());
    pdfWriter.setPageEvent(new HeaderAndFooter(name));
   try{
     document1.open();
      XMLWorkerHelper worker1 = XMLWorkerHelper.getInstance();
      long seed =123;
      Random rnd = new Random (seed); 
      Collections.shuffle(arrlist1,rnd); 
      for(int i=0;i<nb;i++){
      String str =(String) arrlist1.get(i);
      worker1.parseXHtml(pdfWriter, document1, new StringReader(str));
  }
  }catch(Exception e){e.printStackTrace();}
  document1.close();
   pdfWriter.flush();
   pdfWriter.close();
   multi.finish();
 %>

I have this code where a pdf page is created,I would like to know how to zip it?? I found this code on some1's post

var zip = new JSZip();
zip.file("Hello.txt", "Hello World\n");
var img = zip.folder("images");
img.file("smile.gif", imgData, {base64: true});
var content = zip.generate();
location.href="data:application/zip;base64,"+content;

but dont know how to apply it to mine?


回答1:


Create the pdf in ZipOutputStream instead of response.getOutputStream().

Sample:

        OutputStream servletOutputStream =response.getOutputStream(); 
        ZipOutputStream zos = new ZipOutputStream(servletOutputStream);
        ZipEntry entry = new ZipEntry("sample.pdf");
        zos.putNextEntry(entry);

        Document document1 = new Document(PageSize.A4);
        PdfWriter pdfWriter=PdfWriter.getInstance(document1, zos.getOutputStream());
        pdfWriter.setPageEvent(new HeaderAndFooter(name));


来源:https://stackoverflow.com/questions/20511389/zip-a-pdf-file-created-using-itext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!