问题
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