• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

iText and streaming

 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a large PDF file, do you need to wait for the whole thing to be generated before starting to send it to the user? (I suspect the answer is that you need to wait for the whole thing to be generated due to PDF formatting, but this seemed like a good week to confirm it.)

Bruno: I am yet another user that is very pleased with iText. It gives us so much control in a clear manner over our PDFs.
 
author
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:
If you have a large PDF file, do you need to wait for the whole thing to be generated before starting to send it to the user?



In theory: no, you don't have to wait.
iText sends pages to the OutputStream as soon as a page is full. You can test this by using System.out as OutputStream and creating a very large PDF. This is explained in chapter 2.

In practice: yes, in a web application, you probably should create the complete PDF first, then send it to the browser (note that iText doesn't support linearized PDF).

Because of flaws in some browsers, dynamically generated PDFs aren't always interpreted correctly. Some versions of MSIE demand that you define the content length of the PDF file in the header BEFORE sending the first byte of PDF. Otherwise, you'll only see blank pages, or a message that the PDF is corrupt.
In chapter 17, I explain what goes wrong. Some versions of MSIE read bytes in fixed blocks. If the size of the PDF isn't a multiple of this block length, all kinds of garbage is added at the end, and the PDF can't be interpreted.
For some applications at Ghent University, we work in a controlled environment (we can choose the browser of the end user); in that case, we simple use a ServletOutputStream when we create an instance of PdfWriter. It works fine with Netscape, Mozilla, and Firefox.
In other applications, we need to use a work around: store the complete PDF in memory (ByteArrayOutputStream), add the size to the header, then write the ByteArrayOutputStream to the ServletOutputStream.
As explained elsewhere, we sometimes have to create PDF documents with 1,000+ pages. Due to slow database access in a particular application at Ghent University, a timeout problem can occur. We avoid this problem by creating the PDF in a thread (iText is thread safe), informing the end user of the status using a simple HTML page. I simplified this solution, and explained it in in chapter 17 (ProgressServlet).

Originally posted by Jeanne Boyarsky:
I am yet another user that is very pleased with iText. It gives us so much control in a clear manner over our PDFs.



Thanks!
Bruno
 
Jeanne Boyarsky
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bruno! The answer was a lot more involved than I had expected. I'm glad I asked .
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic