<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<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.TextToPdf" />
<ref bean="transformer.Pdf2swf" />
</list>
</property>
<property name="intermediateMimetypes">
<list>
<value>text/plain</value>
<value>application/pdf</value>
</list>
</property>
</bean>
</beans>
* À noter que ceci ne fonctionne pas avec les courriels .msg.
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);
Ceci permet de décoder le contenu "utf-8" du message. On pourrait y ajouter la détection d'encodage pour généraliser l'utilité de la classe.
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>
14:35:39,027 User:System DEBUG [content.transform.ContentTransformerRegistry] Searched for transformer:
source mimetype: message/rfc822
target mimetype: image/png
transformers: []
14:35:39,438 User:System DEBUG [content.transform.ContentTransformerRegistry] Searched for transformer:
source mimetype: message/rfc822
target mimetype: application/x-shockwave-flash
transformers: [ComplexContentTransformer[ average=99664ms]]
14:35:39,441 User:System DEBUG [content.transform.ContentTransformerRegistry] Searched for transformer:
source mimetype: message/rfc822
target mimetype: application/x-shockwave-flash
transformers: [ComplexContentTransformer[ average=99664ms]]
14:35:39,443 User:System DEBUG [manageoffice.transformer.TextUtfToPdfContentTransformer] Working…
14:35:39,948 User:System DEBUG [util.exec.RuntimeExec] Execution result:
os: Linux
command: [/usr/local/bin/pdf2swf, -T, 9, -s, poly2bitmap,subpixels=72, /opt/Alfresco/tomcat/temp/Alfresco/RuntimeExecutableContentTransformerWorker_source_5580025634285233592.pdf, -o, /opt/Alfresco/tomcat/temp/Alfresco/RuntimeExecutableContentTransformerWorker_target_8960526334669163264.swf]
succeeded: true
exit code: 0
out: NOTICE processing PDF page 1 (612x792:0:0) (move:0:0)
NOTICE processing PDF page 2 (612x792:0:0) (move:0:0)
NOTICE Writing SWF file /opt/Alfresco/tomcat/temp/Alfresco/RuntimeExecutableContentTransformerWorker_target_8960526334669163264.swf
err:
14:35:40,180 User:admin DEBUG [content.transform.ContentTransformerRegistry] Searched for transformer:
source mimetype: application/x-shockwave-flash
target mimetype: text/plain
transformers: []
14:35:40,225 User:admin DEBUG [content.transform.ContentTransformerRegistry] Searched for transformer:
source mimetype: message/rfc822
target mimetype: text/plain
transformers: [EMLTransformer[ average=3ms]]
14:35:40,262 User:admin DEBUG [content.transform.ContentTransformerRegistry] Searched for transformer:
source mimetype: application/x-shockwave-flash
target mimetype: text/plain
transformers: []
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);
<bean id="transformer.complex.Email2image"
class="org.alfresco.repo.content.transform.ComplexContentTransformer"
parent="baseContentTransformer" >
<property name="transformers">
<list>
<ref bean="transformer.RFC822" />
<ref bean="transformer.complex.Text.Image" />
</list>
</property>
<property name="intermediateMimetypes">
<list>
<value>text/plain</value>
</list>
</property>
</bean>
Content from pre 2016 and from language groups that have been closed.
Content is read-only.
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.