2 Replies Latest reply on Oct 2, 2009 8:30 AM by mrblacksmith

    PDF mail attachment with iso-8859-1 encoding is corrupt

    mrblacksmith

      I have succesfully created a pdf as an attachment to a mail using a mail template like this:


      <?xml version="1.0" encoding="iso-8859-1"?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <m:message xmlns="http://www.w3.org/1999/xhtml"
           xmlns:m="http://jboss.com/products/seam/mail"
           xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:p="http://jboss.com/products/seam/pdf"
           xmlns:h="http://java.sun.com/jsf/html">
           <m:attachment fileName="data.pdf"  contentType="application/pdf" >
                <ui:include src="../pdf/pdffoo.xhtml" />
           </m:attachment>
      ... etc
      



      and the pdffoo.xhtml looks like this:


      <p:document xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:p="http://jboss.com/products/seam/pdf" type="PDF" pageSize="A4"
           title="test">
           <p:paragraph>Test!</p:paragraph>
      </p:document>
      



      but when I use swedish characters 'åäö' inside the pdf template like this:


      <p:document xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:p="http://jboss.com/products/seam/pdf" type="PDF" pageSize="A4"
           title="test">
           <p:paragraph>Test! åäö</p:paragraph>
      </p:document>
      



      I get an (pretty much expected) error like this:


      com.sun.facelets.FaceletException: Error Parsing /pdf/pdffoo.xhtml: Error Traced[line: 5] Invalid byte 2 of 3-byte UTF-8 sequence.
      



      so, I add the encoding line like this to pdffoo.xhtml:


      <?xml version="1.0" encoding="iso-8859-1"?>
      <p:document xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:p="http://jboss.com/products/seam/pdf" type="PDF" pageSize="A4"
           title="test">
           <p:paragraph>Test! åäö</p:paragraph>
      </p:document>
      



      and now the gernerated PDF is invalid. The generated pdf only contains this:


      <?xml version="1.0" encoding="iso-8859-1"?>
      



      Nothing else. Anyone have an idea what's wrong here?

        • 1. Re: PDF mail attachment with iso-8859-1 encoding is corrupt
          mrblacksmith

          It seems to me that this is an issue due to the implementation of org.jboss.seam.mail.ui.UIAttachment. It will only render a pdf/excel file if the first line/child in the (pdf/excel) xhtml template contains a pdf/excel tag. If I add a


          <?xml version="1.0" encoding="iso-8859-1"?> 
          



          line at the top of my pdf template it will fail when added as an attachment, but it will be renderder correct to a pdf when not used as an attachment.


          I rewrote it a bit and performed a simple test, and now I can display iso-8859-1 characters (like swedish 'åäö') in the attached pdf.


          Here's a patch:


          Index: UIAttachment.java
          ===================================================================
          --- UIAttachment.java     (revision 11528)
          +++ UIAttachment.java     (working copy)
          @@ -5,12 +5,14 @@
           import java.io.InputStream;
           import java.lang.reflect.Method;
           import java.net.URL;
          +import java.util.List;
           
           import javax.activation.DataHandler;
           import javax.activation.DataSource;
           import javax.activation.FileDataSource;
           import javax.activation.URLDataSource;
           import javax.faces.FacesException;
          +import javax.faces.component.UIComponent;
           import javax.faces.component.ValueHolder;
           import javax.faces.context.FacesContext;
           import javax.faces.convert.Converter;
          @@ -85,11 +87,11 @@
              public void encodeBegin(FacesContext context) throws IOException
              {
                 if (this.getChildCount() > 0) {
          -         if (Reflections.isInstanceOf(this.getChildren().get(0).getClass(), "org.jboss.seam.pdf.ui.UIDocument") ||
          -             Reflections.isInstanceOf(this.getChildren().get(0).getClass(), "org.jboss.seam.excel.ui.UIWorkbook")) 
          +         UIComponent uiComponent = findUIComponent();
          +         if (uiComponent != null)
                    {
          -            Method method = Reflections.getSetterMethod(this.getChildren().get(0).getClass(), "sendRedirect");
          -            Reflections.invokeAndWrap(method, this.getChildren().get(0), false);
          +            Method method = Reflections.getSetterMethod(uiComponent.getClass(), "sendRedirect");
          +            Reflections.invokeAndWrap(method, uiComponent, false);
                       JSF.renderChildren(context, this);
                    } else {
                       setValue(encode(context).getBytes());
          @@ -101,6 +103,22 @@
                 }
              }
              
          +   private static String PDF_CLASS = "org.jboss.seam.pdf.ui.UIDocument";
          +   private static String EXCEL_CLASS = "org.jboss.seam.excel.ui.UIWorkbook";
          +   
          +   private UIComponent findUIComponent()
          +   {
          +      List<UIComponent> children = this.getChildren();
          +      for (UIComponent uiComponent : children)
          +      {
          +         if (Reflections.isInstanceOf(uiComponent.getClass(), PDF_CLASS) || Reflections.isInstanceOf(uiComponent.getClass(), EXCEL_CLASS))
          +         {
          +            return uiComponent;
          +         }
          +      }
          +      return null;
          +   }
          +
              @Override
              public void encodeEnd(FacesContext context) throws IOException
              {
          



          I don't know if this fix has any other implications, is there anyone here who could give their view on the fix?

          • 2. Re: PDF mail attachment with iso-8859-1 encoding is corrupt
            mrblacksmith

            http://www.seamframework.org/Community/Contribute



            First, please ask on the Seam Users Forum if your problem is really related to a bug in Seam. If it is related to a bug in Seam, search the JIRA issue database for similar problems. Only if you did not find a reported bug, and other users and developers on the forum confirmed that you found a bug, please report it

            OK, so what do you say guys, is this a confirmed bug or not? I cannot find any related bug in JIRA and I have a patch - shall I add an issue in JIRA?