Hi
I've a problem when merging multiple PDFs in-memory to one single
document (I calle it CompoundDocument). I use the iText library.
I extracted some code out of the commandline tool "concat_pdf" and
adjusted the logic to read the documents from bytearraystreams instead
of reading files (please see the source code snippets below).
I assume that the merging of the docs works well, because if I run the
code I can see the logstatements quite immediately:
>Create pdf ... DONE!
>Write pdf to output stream...
>Processed page 1
>Processed page 2
>Processed page 3
>Processed page 1
>Processed page 2
>Write pdf to output stream... DONE!
--> This takes about 200 milli seconds
The Problem is, that the time consumed until the PDF is finally
displayed in the browser is about 20 seconds! I test the application
locally (using Tomcat). The PDF produced is of size 200Kb.
I assume that there must be something like a timeout that takes place...
Have anybody ever had a similar problem? Any hints?
Any help is appreciated!
Many thanks
Silvio
----------------------8<------------------------------------------------
----
Code Snippet of Merging Class(CompoundDocument):
================================================
public void writeTo(OutputStream inStream) throws
MyDocumentException {
int i=0;
Document lDocument = null;
PdfCopy lWriter = null;
for (Iterator lIterator = documents.iterator();
lIterator.hasNext();i++) {
try {
//get content of actual delegated document
MyDocument lMyDocument = (MyDocument)lIterator.next();
ByteArrayOutputStream lStream = new ByteArrayOutputStream();
//fixme (shd) here we could directly get the stream from the
document
//instead of writing the whole data to a "tempstream" to be able
to read it
lMyDocument.writeTo(lStream);
PdfReader lReader = new PdfReader(lStream.toByteArray());
if (i == 0) {
//create (single) target document instance
lDocument = new Document(lReader.getPageSizeWithRotation(1));
//we create writer to write into global stream
lWriter = new PdfCopy(lDocument, inStream);
//open target document instance
lDocument.open();
}
//read in page by page
int n = lReader.getNumberOfPages();
for(int j = 1; j <= n; j++) {
PdfImportedPage lPage = lWriter.getImportedPage(lReader, j);
lWriter.addPage(lPage);
cLogger.info("Processed page " + j);
lWriter.flush();
}
lStream.flush();
lStream.close();
lWriter.flush();
} catch (DocumentException ex) {
cLogger.error("Error writing pdf to stream!", ex);
throw new MyDocumentException(ex);
} catch (IOException ex) {
cLogger.error("Error writing pdf to stream!", ex);
throw new MyDocumentException(ex);
}
}
if (lWriter != null) {
lWriter.close();
}
if (lDocument != null) {
lDocument.close();
}
}
Servlet (Struts Action):
========================
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
...
MyDocument lCompoundDocument = new CompountDocument();
//add some documents here
//make sure each document itelfs write the pdf to its own byte output
stream
try {
cLogger.debug("Create pdf ... DONE!");
//fill up response
response.setContentType(CONTENT_TYPE_PDF);
response.setContentLength(lDocument.size());
response.setBufferSize(0);
response.setHeader("Content-disposition", "inline;
filename=\"sample.pdf\"");
response.setHeader("Cache-Control", "max-age=30");
response.setHeader("Pragma", "no-cache");
response.flushBuffer();
cLogger.debug("Write pdf to output stream...");
ServletOutputStream lStream = response.getOutputStream();
lDocument.writeTo(lStream); //see the method above
lStream.flush();
response.flushBuffer();
cLogger.debug("Write pdf to output stream... DONE!");
lStream.close();
}
catch (IOException ex)
{
throw new MyException(ex);
}
catch (Exception ex)
{
cLogger.error("Exception occured during creating a document", ex);
throw new MyException(ex);
}
return null;
}
-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
http://ads.osdn.com/?ad_idt12&alloc_id344&op=click
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions