06-16-2011 08:45 AM
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 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>
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);
06-17-2011 10:12 AM
06-20-2011 02:12 AM
check your database and connection string are UTF-8It's ok, /opt/Alfresco/tomcat/shared/classes/alfresco-global.properties :
db.url=jdbc:mysql://localhost:3309/${db.name}?useUnicode=yes&characterEncoding=UTF-8
/opt/Alfresco/mysql/my.cnfdefault-character-set=utf8
There are some fixes to the code since 3.4d to get accented characters working, you may need to patch. In particular we replaced the old RFC822 transformer. I suggest you look at JIRA for further detailsI have not found something like a new update of the transformer in Jira 😞 Do I replace the org.alfresco.repo.content.transform.EMLTransformer (bean id="transformer.RFC822") with the last in the SVN ?
06-28-2011 10:25 AM
06-28-2011 11:02 AM
06-29-2011 02:52 AM
06-29-2011 04:11 AM
06-29-2011 04:26 AM
06-29-2011 04:52 AM
06-29-2011 11:18 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.