I have a strange issue related to concatenation of PDFs. Everything
works very well (almost). I am able to process dozens of pdfs, while
adding headers and footers. The problem occurs when certain pdfs are
the last to get concatenated. From what I can tell, there is nothing
special about these pdfs. The same files work perfectly well when
that are NOT at the end. I have narrowed it down to one particular pdf
that consists of about half a page of text. I played around with it
until adding a single character (any character) caused the "damaged
file" error from acrobat. As I mentioned before, the same "bad" file
works just fine when i added another file to the queue so it would not
be be processed last. When I first saw this issue, I assumed it is
related to me not "flushing" :) properly and thus sending out
incomplete pdf. I checked, but everything looks correct. I dont
think I had this issue when I used PDFCopy, but because I needed to
apply headers and footers I had to switch to PDFWriter.
Here is my set up. I use a servlet that starts the process:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// some code here that initializes needed variables
baos = ReportGenerator.regenerateReport(baos, sURL, exhibitList,
sFirmName, sFooter);
resp.setHeader("Expires", "0");
resp.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
resp.setHeader("Pragma", "public");
resp.setContentType("application/pdf");
resp.setContentLength(baos.size());
ServletOutputStream out = resp.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
then I have a separate class that handles the generation:
public static ByteArrayOutputStream
regenerateReport(ByteArrayOutputStream baos, String sURL, ArrayList
exhibitList, String sFirmName, String sFooter) throws IOException {
Document document = null;
PdfWriter writer = null;
PdfReader reader = null;
document = new Document(PageSize.LETTER);
try {
writer = PdfWriter.getInstance(document, baos);
writer.setPageEvent(new ReportGenerator(sFirmName, sFooter));
document.open();
reader = new PdfReader(new URL(sURL));
processPDF(reader, writer, document);
} catch(DocumentException de) {
de.printStackTrace();
} finally {
document.close();
writer.freeReader(reader);
}
return baos;
}
private static void processPDF(PdfReader reader, PdfWriter writer,
Document document) throws IOException, BadPdfFormatException,
DocumentException {
reader.consolidateNamedDestinations();
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page;
for (int i = 1; i <= n; i++) {
document.newPage();
page = writer.getImportedPage(reader, i);
cb.addTemplate(page, 0, -3);
}
}
Any ideas? Any help would be greatly appriciated. I already spent a
few hours on this, and I am afraid I may have to put in some "ugly"
work around if I do not get to a solution soon. Thank you.
--
Michael Shmulenson
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions