Announcement Announcement Module
No announcement yet.
Spring+PDF issue Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring+PDF issue


    I'm trying to generate PDF using Spring 3.0.5 RELEASE and iText-5.0.6 library.
    Here is an example from reference manual ( Subclassing for PDF views):

    package pdf;
    // imports omitted for brevity
    public class PDFPage extends AbstractPdfView {
        protected void buildPdfDocument(
            Map model,
            Document doc,
            PdfWriter writer,
            HttpServletRequest req,
            HttpServletResponse resp)
            throws Exception {
            List words = (List) model.get("wordList");
            for (int i=0; i<words.size(); i++)
                doc.add( new Paragraph((String) words.get(i)));
    As I see at Spring Javadoc method signature looks like this:
    protected abstract  void 	buildPdfDocument(Map<String,Object> model, com.lowagie.text.Document document, com.lowagie.text.pdf.PdfWriter writer, HttpServletRequest request, HttpServletResponse response)
              Subclasses must implement this method to build an iText PDF document, given the model.
    But iText 5.0.6 contains no com.lowagie package at all! com.itextpdf instead.

    The question is: should I use an older version of PDF iText library or wait for a newer version of Spring with new buildPdfDocument signature fixed?
    Any more workarounds, ideas? Highly appreciate any help.

    Thank you.
    Last edited by garntsev; Mar 22nd, 2011, 09:31 AM. Reason: misprint

  • #2
    you might want to use an older release of iText that will work with Spring 3.0.5. check in older spring release distro they have a package with dependencies in it with itext. unless you require a new feature from latest itext then i guess you'll have to wait for this support.


    • #3
      Really? Does iText change its structure at all? It violates Open for Extension, Closed for Modification!


      • #4

        iText 5.0.0 - ... (December, 2009)

        We're jumping from iText 2.x.y to 5.0.0 for the following reasons:

        * We want to use the same version number for iText and iTextSharp.
        * From now on, the iText.jar is compiled using Java 5 (instead of with the JDK 1.4).
        * The F/OSS license has been upgraded from MPL/LGPL to AGPL.
        * The package names have changed from com.lowagie to com.itextpdf.
        * The toolbox and RTF support have been removed: they are now in a separate project at SourceForge.

        Note that because of the change of the package names, you can't upgrade to iText 5 without changing your code. Before upgrading to this new version, please read the new Terms of Use.
        Versions: iText 5.0.0, iText 5.0.1, iText 5.0.2, iText 5.0.3, iText 5.0.4, iText 5.0.5, iText 5.0.6


        • #5

          And where can we see Spring's requirements/compatibility information like 'best used with libs: [library-list-x.x.x versions]'?


          • #6
            Thanks a lot for your information.

            Since the library has changed, I think Spring team should suggest any alternative or upgrade the related libraries for the latest change of iText, otherwise the team should not claim the capability of PDF.


            • #7
              I am also having a problem using iText 5.x with Spring 3.0.5 due to the package renaming that took place in the itext libraries. I searched and it seems the issue with AbstractPdfView began to be reported a year ago. Can the Spring team please update to reflect the new package structure of com.itextpdf.* ?

              For now I'm forced to go to an old version of itext (2.1.7)


              • #8
                What stops you from creating your own abstract class like Spring has and place into your application. Here is my version:
                import java.util.Map;
                import javax.servlet.http.HttpServletRequest;
                import javax.servlet.http.HttpServletResponse;
                import org.springframework.web.servlet.view.AbstractView;
                import com.itextpdf.text.Document;
                import com.itextpdf.text.DocumentException;
                import com.itextpdf.text.PageSize;
                import com.itextpdf.text.pdf.PdfWriter;
                public abstract class AbstractIText5PdfView extends AbstractView {
                	public AbstractIText5PdfView() {
                	protected boolean generatesDownloadContent() {
                		return true;
                	protected final void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
                		// IE workaround: write into byte array first.
                		ByteArrayOutputStream baos = createTemporaryOutputStream();
                		// Apply preferences and build metadata.
                		Document document = newDocument();
                		PdfWriter writer = newWriter(document, baos);
                		prepareWriter(model, writer, request);
                		buildPdfMetadata(model, document, request);
                		// Build PDF document.
                		buildPdfDocument(model, document, writer, request, response);
                		// Flush to HTTP response.
                		writeToResponse(response, baos);
                	protected Document newDocument() {
                		return new Document(PageSize.A4);
                	protected PdfWriter newWriter(Document document, OutputStream os) throws DocumentException {
                		return PdfWriter.getInstance(document, os);
                	protected void prepareWriter(Map<String, Object> model, PdfWriter writer, HttpServletRequest request)
                			throws DocumentException {
                	protected int getViewerPreferences() {
                		return PdfWriter.ALLOW_PRINTING | PdfWriter.PageLayoutSinglePage;
                	protected void buildPdfMetadata(Map<String, Object> model, Document document, HttpServletRequest request) {
                	protected abstract void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
                			HttpServletRequest request, HttpServletResponse response) throws Exception;
                And it works perfectly.
                Now I can use latest version (5.1) of iText with any Spring 3.0.x version.