cancel
Showing results for 
Search instead for 
Did you mean: 

Imap preview eml

dranakan
Champ on-the-rise
Champ on-the-rise
Hello,

I would like to show preview in Share from mails coming from Outlook. I get problem with the accents : If a mail contain this "é" it will show this "?%". It's ok with mail UTF-8, but how can I change to preview all kind of mails ?

I have done this to be able to get a preview of eml files :

Transformer:
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.TransformationOptions;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.TextToPDF;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.encoding.EncodingManager;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import org.alfresco.repo.content.transform.AbstractContentTransformer2;
/**
* Convert Text with accents (UTF-8) to PDF.
*
*
*/
public class TextUtfToPdfContentTransformer extends AbstractContentTransformer2
{
    private static final Log logger = LogFactory.getLog(TextUtfToPdfContentTransformer.class);
   
    private TextToPDF transformer;
   
    public TextUtfToPdfContentTransformer()
    {
        transformer = new TextToPDF();
    }
   
    public void setStandardFont(String fontName)
    {
        try
        {
            transformer.setFont(PDType1Font.getStandardFont(fontName));
        }
        catch (Throwable e)
        {
            throw new AlfrescoRuntimeException("Unable to set Standard Font for PDF generation: " + fontName, e);
        }
    }
   
    public void setTrueTypeFont(String fontName)
    {
        try
        {
            transformer.setFont(PDTrueTypeFont.loadTTF(null, fontName));
        }
        catch (Throwable e)
        {
            throw new AlfrescoRuntimeException("Unable to set True Type Font for PDF generation: " + fontName, e);
        }
    }
   
    public void setFontSize(int fontSize)
    {
        try
        {
            transformer.setFontSize(fontSize);
        }
        catch (Throwable e)
        {
            throw new AlfrescoRuntimeException("Unable to set Font Size for PDF generation: " + fontSize);
        }
    }
   
    /**
     * Only supports Text to PDF
     */
    public boolean isTransformable(String sourceMimetype, String targetMimetype, TransformationOptions options)
    {
        if ( (!MimetypeMap.MIMETYPE_TEXT_PLAIN.equals(sourceMimetype) &&
              !MimetypeMap.MIMETYPE_TEXT_CSV.equals(sourceMimetype) &&
              !MimetypeMap.MIMETYPE_XML.equals(sourceMimetype) ) ||
            !MimetypeMap.MIMETYPE_PDF.equals(targetMimetype))
        {
            // only support (text/plain OR text/csv OR text/xml) to (application/pdf)
            return false;
        }
        else
        {
            return true;
        }
    }

    @Override
    protected void transformInternal(
            ContentReader reader,
            ContentWriter writer,
            TransformationOptions options) throws Exception
    {
        PDDocument pdf = null;
        InputStream is = null;
        InputStreamReader ir = null;
        OutputStream os = null;
        try
        {
           logger.debug("Working…");
            is = reader.getContentInputStream();
            EncodingManager encodingManager = new EncodingManager();
            transformer.getFont().setEncoding(encodingManager.getEncoding(COSName.WIN_ANSI_ENCODING));

            pdf = transformer.createPDFFromText(new InputStreamReader(is, "UTF-8"));
            // dump it all to the writer
            os = writer.getContentOutputStream();
            pdf.save(os);
        }
        finally
        {
            if (pdf != null)
            {
                try { pdf.close(); } catch (Throwable e) {e.printStackTrace(); }
            }
            if (ir != null)
            {
                try { ir.close(); } catch (Throwable e) {e.printStackTrace(); }
            }
            if (is != null)
            {
                try { is.close(); } catch (Throwable e) {e.printStackTrace(); }
            }
            if (os != null)
            {
                try { os.close(); } catch (Throwable e) {e.printStackTrace(); }
            }
        }
    }
   
    protected InputStreamReader buildReader(InputStream is, String encoding, String node)
    {
        // If they gave an encoding, try to use it
        if(encoding != null)
        {
            Charset charset = null;
            try
            {
                charset = Charset.forName(encoding);
            } catch(Exception e)
            {
                logger.warn("JVM doesn't understand encoding '" + encoding +
                        "' when transforming " + node);
            }
            if(charset != null)
            {
                logger.debug("Processing plain text in encoding " + charset.displayName());
                return new InputStreamReader(is, charset);
            }
        }
       
        // Fall back on the system default
        logger.debug("Processing plain text using system default encoding");
        return new InputStreamReader(is);
    }
}

Bean to use the transformer


<bean id="transformer.complex.Mail.Pdf2swf"
        class="org.alfresco.repo.content.transform.ComplexContentTransformer"
        parent="baseContentTransformer" >
      <property name="transformers">
         <list>
            <ref bean="transformer.RFC822" />
            <ref bean="transformer.PdfBox.TextUtfToPdf" />
            <ref bean="transformer.Pdf2swf" />
         </list>
      </property>
      <property name="intermediateMimetypes">
         <list>
            <value>text/plain</value>
            <value>application/pdf</value>
         </list>
      </property>
   </bean>

I also try this code (working only with mails utf-8). What do I change ?

            logger.debug("Working…");
            is = reader.getContentInputStream();
           
            EncodingManager encodingManager = new EncodingManager();
            transformer.getFont().setEncoding(encodingManager.getEncoding(COSName.WIN_ANSI_ENCODING));          
             ir = buildReader(is, reader.getEncoding(), reader.getContentUrl());
            
             pdf = transformer.createPDFFromText(ir);
             // dump it all to the writer
             os = writer.getContentOutputStream();
             pdf.save(os);


Help :
http://issues.alfresco.com/jira/browse/ALF-3246?page=com.atlassian.jira.plugin.system.issuetabpanels...
http://forums.alfresco.com/fr/viewtopic.php?f=8&t=4049#p18699

(Alfresco 3.4D, Redhat).
14 REPLIES 14

mrogers
Star Contributor
Star Contributor
Ho hum. :roll:  Thats not how it works.

nlaselva
Champ in-the-making
Champ in-the-making
Sorry, maybe it doesn't work the way I stated. Nevertheless a bug is a bug. In Community Edition the bug is still there. In Enterprise edition it has been fixed. I didn't want to be unfair, but only to notify to dranakan that if he is looking for a QUICK fix for this bug, the Enterprise edition is the quickest solution (maybe not the cheapest).

mrogers
Star Contributor
Star Contributor
I expect that it is fixed on HEAD now.   And is probably fixed in Community 3.5.a.    However I don't have details of that release to confirm or test.

dranakan
Champ on-the-rise
Champ on-the-rise
Hello,

The accented characters are ok with Alfresco 4.0b 🙂

Just need to add in tomcat/webapps/alfresco/WEB-INF/classes/alfresco/swf-transform-context.xml to enable the EML preview :

<!– This transformer allows for the webpreviewing of outlook msg files. –>
   <bean id="transformer.complex.Mail.rfc82swff"
        class="org.alfresco.repo.content.transform.ComplexContentTransformer"
        parent="baseContentTransformer" >
      <property name="transformers">
         <list>
            <ref bean="transformer.RFC822" />
            <ref bean="transformer.PdfBox.TextToPdf" />
            <ref bean="transformer.Pdf2swf" />
         </list>
      </property>
      <property name="intermediateMimetypes">
         <list>
            <value>text/plain</value>
            <value>application/pdf</value>
         </list>
      </property>
   </bean>

I see that the pictures are not visible in the preview (it replaces the picture by the URL of the picture). The picture can be displayed ?

mrogers
Star Contributor
Star Contributor
We need a different transformer stack for preview to see the compound types like Mime messages.  I'm not happy with the text based swf based preview like below which is why its not configured out of the box, however I agree that its better than nothing! 

Contributions or suggestions anyone?