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

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

    Marcus Smedman Newbie

      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"
           <m:attachment fileName="data.pdf"  contentType="application/pdf" >
                <ui:include src="../pdf/pdffoo.xhtml" />
      ... etc

      and the pdffoo.xhtml looks like this:

      <p:document xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:p="http://jboss.com/products/seam/pdf" type="PDF" pageSize="A4"

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

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

      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:p="http://jboss.com/products/seam/pdf" type="PDF" pageSize="A4"
           <p:paragraph>Test! åäö</p:paragraph>

      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
          Marcus Smedman Newbie

          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 {
          @@ -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;
          +   }
              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
            Marcus Smedman Newbie


            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?