0
我有一個PDF生成與iText和我下載它,我需要將它附加到一個Zip文件並下載到,這可能嗎?追加生成的PDF文件到iText文件
這是我的代碼
public ActionForward descargarConstancia(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response){
UsuarioBean usuarioBean = (request.getSession().getAttribute("infoUsuario") == null ? null : (UsuarioBean) request.getSession().getAttribute("infoUsuario"));
if (usuarioBean == null)
return mapping.findForward("cerrarSesionNominas");
try{
ConstanciaRetencionBusiness constanciasBO = new ConstanciaRetencionBusinessImpl();
UsuarioNominasBean usuarioNomBean = (request.getSession().getAttribute("usuarioNominasBean") == null ? null : (UsuarioNominasBean) request.getSession().getAttribute("usuarioNominasBean"));
String rfcOtro = request.getParameter("rfc")!=null ? request.getParameter("rfc") : usuarioNomBean.getRFC();
List listaConst = constanciasBO.selectByRFC(rfcOtro);
int noConstancia = request.getParameter("noConstancia")!=null ? Integer.parseInt(request.getParameter("noConstancia")) : 0;
List lstPdfBytes = new ArrayList();
if(listaConst!=null && listaConst.size()>noConstancia){
ConstanciaRetencionVO vo = (ConstanciaRetencionVO)listaConst.get(noConstancia);
lstPdfBytes.add(constanciasBO.generarConstancia(request, vo));
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=Constancia_"+rfcOtro+".pdf");
OutputStream out = response.getOutputStream();
concatena(lstPdfBytes, out,request);
out.flush();
out.close();
}
}catch(Exception e){
log.info(LineException.getException(e));
return mapping.findForward(mostrarRecibo);
}finally{
}
return mapping.findForward("registroCorrecto");
}
這是代碼FO生成PDF
public void concatena(List lstPdfBytes, OutputStream out,HttpServletRequest req) throws Exception{
try{
PdfReader reader = null;
Document document = new Document();
PdfCopy copy = new PdfCopy(document, out);
document.open();
for(int i=0; lstPdfBytes!=null && i<lstPdfBytes.size(); i++){
reader = new PdfReader((byte[])lstPdfBytes.get(i));
int paginas = reader.getNumberOfPages();
for(int j=0; j<paginas;j++){
copy.addPage(copy.getImportedPage(reader, j+1));
}
}
document.close();
}catch(Exception exc){
log.info(LineException.getException(exc));
throw new Exception(Errores.ERROR_PDF);
}
}
我與Zip4j庫嘗試,但我堅持
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=Constancia_"+rfcOtro+".zip");
OutputStream out = response.getOutputStream();
ConstanciaRetencionVO vo = (ConstanciaRetencionVO)listaConst.get(noConstancia);
byte []arr=constanciasBO.generarConstancia(request, vo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
concatena(lstPdfBytes, out);
ZipParameters parametros = new ZipParameters();
parametros.setCompressionMethod(8);
parametros.setCompressionLevel(5);
parametros.setEncryptFiles(true);
parametros.setPassword("password");
parametros.setEncryptionMethod(99);
parametros.setAesKeyStrength(3);
parametros.setSourceExternalStream(true);
parametros.setFileNameInZip("constancia.pdf");
ZipOutputStream zos = new ZipOutputStream(baos);
zos.putNextEntry((File)null, parametros);
zos.write(arr);
zos.closeEntry();
zos.flush();
zos.finish();
concatena(lstPdfBytes, out);
out.flush();
out.close();