use of fr.opensagres.xdocreport.document.images.IImageProvider in project esup-ecandidat by EsupPortail.
the class CandidatureController method generateLettre.
/**
* @param template
* @param data
* @param fichierSignature
* @param locale
* @param sendNotification
* @return l'inputstram de la lettre
*/
public InputStream generateLettre(final InputStream template, final ExportLettreCandidat data, final InputStream fichierSignature, final String locale, final Boolean sendNotification) {
// InputStream template = MethodUtils.getXDocReportTemplate(templateLettre, locale, cacheController.getLangueDefault().getCodLangue());
if (template == null) {
return null;
}
final ByteArrayInOutStream out = new ByteArrayInOutStream();
try {
/* Ajout date et heure */
data.setDateHeure(formatterDateTime.format(LocalDateTime.now()));
data.setDate(formatterDate.format(LocalDateTime.now()));
/* Generation du template */
final IXDocReport report = XDocReportRegistry.getRegistry().loadReport(template, TemplateEngineKind.Velocity);
/* Variables */
final IContext context = report.createContext();
context.put("data", data);
/* Pour l'image */
final FieldsMetadata metadata = new FieldsMetadata();
if (fichierSignature != null) {
metadata.addFieldAsImage("imageSignature");
final IImageProvider originalSizeLogo = new ByteArrayImageProvider(fichierSignature, true);
context.put("imageSignature", originalSizeLogo);
} else {
/* Si pas de fichier, on supprime l'image */
metadata.addFieldAsImage("imageSignature", NullImageBehaviour.RemoveImageTemplate);
}
report.setFieldsMetadata(metadata);
/* On vérifie si on affiche le tableau de signataire */
if (data.getLibelleSignature().equals("") && fichierSignature == null) {
context.put("showSignataire", false);
} else {
context.put("showSignataire", true);
}
/* Transformation en pdf */
final Options options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.XWPF);
report.convert(context, options, out);
return pdfManager.cryptAndSignPdf(out, new Locale(locale != null ? locale : "fr"));
} catch (final Exception e) {
// probleme de taille de signature XDocConverterException + StackOverflowError
if (e.getClass() != null && e instanceof XDocConverterException && e.getCause() != null && e.getCause() instanceof StackOverflowError) {
if (sendNotification) {
Notification.show(applicationContext.getMessage("candidature.lettre.download.sign.error", null, UI.getCurrent().getLocale()), Type.WARNING_MESSAGE);
}
} else {
if (sendNotification) {
Notification.show(applicationContext.getMessage("candidature.lettre.download.error", null, UI.getCurrent().getLocale()), Type.WARNING_MESSAGE);
}
logger.error("erreur a la création de la lettre", e);
}
return null;
} finally {
// fermeture des fichiers
MethodUtils.closeRessource(fichierSignature);
MethodUtils.closeRessource(template);
MethodUtils.closeRessource(out);
}
}
Aggregations