Search in sources :

Example 1 with IContext

use of fr.opensagres.xdocreport.template.IContext in project vitam-ui by ProgrammeVitam.

the class PdfFileGenerator method createPdfDocument.

/**
 * Method allowing to to generate the dynamic and/or static pdf file.
 * @param xdocGenerator the XDocGenerator used to generate the pdf file.
 * @param dataMap the object containing the data to add to the template.
 * @param pdfOutputStream the generated pdf file /!\ the owner is reponsible for closing the stream.
 * @throws Exception
 */
protected static void createPdfDocument(final IXDocReport xdocGenerator, final Map<String, Object> dataMap, final OutputStream pdfOutputStream) throws Exception {
    final IContext context = xdocGenerator.createContext();
    dataMap.entrySet().forEach(entry -> context.put(entry.getKey(), entry.getValue()));
    final Options options = Options.getFrom(DocumentKind.ODT).to(ConverterTypeTo.PDF);
    xdocGenerator.convert(context, options, pdfOutputStream);
}
Also used : Options(fr.opensagres.xdocreport.converter.Options) IContext(fr.opensagres.xdocreport.template.IContext)

Example 2 with IContext

use of fr.opensagres.xdocreport.template.IContext in project Java-Tutorial by gpcodervn.

the class GenerateDocxReport method main.

public static void main(String[] args) throws IOException, XDocReportException {
    // 1) Load Docx file by filling Velocity template engine and cache it to the registry
    InputStream in = new FileInputStream("template/Project.docx");
    IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);
    // 2) Create fields metadata to manage lazy loop (#forech velocity) for table row.
    // Create FieldsMetadata by setting Velocity as template engine
    FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
    // Load fields metadata from Java Class
    fieldsMetadata.load("project", Project.class);
    // Load is called with true because model is a list of Developer.
    fieldsMetadata.load("developers", Developer.class, true);
    // 3) Create context Java model
    IContext context = report.createContext();
    Project project = new Project("XDocReport");
    context.put("project", project);
    // Register developers list
    context.put("developers", getDevelopers());
    // 4) Generate report by merging Java model with the Docx
    OutputStream out = new FileOutputStream(new File("Project_out.docx"));
    report.process(context, out);
}
Also used : Project(com.gpcoder.model.Project) IXDocReport(fr.opensagres.xdocreport.document.IXDocReport) FieldsMetadata(fr.opensagres.xdocreport.template.formatter.FieldsMetadata) IContext(fr.opensagres.xdocreport.template.IContext) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with IContext

use of fr.opensagres.xdocreport.template.IContext in project Java-Tutorial by gpcodervn.

the class GeneratePDF method main.

public static void main(String[] args) throws XDocConverterException, XDocReportException, IOException {
    // 1) Load Docx file by filling Velocity template engine and cache it to the registry
    InputStream in = new FileInputStream("template/Project.docx");
    IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);
    // 2) Create fields metadata to manage lazy loop (#forech velocity) for table row.
    // Create FieldsMetadata by setting Velocity as template engine
    FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
    // Load fields metadata from Java Class
    fieldsMetadata.load("project", Project.class);
    // Load is called with true because model is a list of Developer.
    fieldsMetadata.load("developers", Developer.class, true);
    // 3) Create context Java model
    IContext context = report.createContext();
    Project project = new Project("XDocReport");
    context.put("project", project);
    // Register developers list
    context.put("developers", getDevelopers());
    // 4) Generate report by merging Java model with the Docx
    OutputStream out = new FileOutputStream(new File("Project_Out.pdf"));
    // report.process(context, out);
    Options options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.XWPF);
    report.convert(context, options, out);
}
Also used : Project(com.gpcoder.model.Project) Options(fr.opensagres.xdocreport.converter.Options) IXDocReport(fr.opensagres.xdocreport.document.IXDocReport) FieldsMetadata(fr.opensagres.xdocreport.template.formatter.FieldsMetadata) IContext(fr.opensagres.xdocreport.template.IContext) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with IContext

use of fr.opensagres.xdocreport.template.IContext in project Java-Tutorial by gpcodervn.

the class GenerateDocxReport method main.

public static void main(String[] args) throws IOException, XDocReportException {
    // 1) Load Docx file by filling Velocity template engine and cache it to the registry
    InputStream in = GenerateDocxReport.class.getResourceAsStream("project.docx");
    IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);
    // 2) Create fields metadata to manage lazy loop (#forech velocity) for table row.
    // 1) Create FieldsMetadata by setting Velocity as template engine
    FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
    // 2) Load fields metadata from Java Class
    fieldsMetadata.load("project", Project.class);
    // Here load is called with true because model is a list of Developer.
    fieldsMetadata.load("developers", Developer.class, true);
    // 3) Create context Java model
    IContext context = report.createContext();
    Project project = new Project("XDocReport");
    context.put("project", project);
    // Register developers list
    List<Developer> developers = new ArrayList<Developer>();
    developers.add(new Developer("Giang", "Phan", "gpcoder@gmail.com"));
    developers.add(new Developer("ZERR", "Angelo", "angelo.zerr@gmail.com"));
    context.put("developers", developers);
    // 4) Generate report by merging Java model with the Docx
    OutputStream out = new FileOutputStream(new File("project_out.docx"));
    report.process(context, out);
}
Also used : IXDocReport(fr.opensagres.xdocreport.document.IXDocReport) FieldsMetadata(fr.opensagres.xdocreport.template.formatter.FieldsMetadata) IContext(fr.opensagres.xdocreport.template.IContext) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) File(java.io.File)

Example 5 with IContext

use of fr.opensagres.xdocreport.template.IContext in project SORMAS-Project by hzi-braunschweig.

the class TemplateEngine method generateDocumentDocx.

public byte[] generateDocumentDocx(Properties properties, File templateFile) throws DocumentTemplateException {
    try {
        FileInputStream templateInputStream = new FileInputStream(templateFile);
        IXDocReport report = readXDocReport(templateInputStream);
        IContext context = report.createContext();
        for (Object key : properties.keySet()) {
            if (key instanceof String) {
                Object property = properties.get(key);
                // Sanitize property to avoid XML parsing errors
                if (property instanceof Enum<?> && property.toString() != null) {
                    property = returnSanitizedString(property.toString());
                } else if (property instanceof String) {
                    property = returnSanitizedString((String) property);
                }
                if (property != null && !(property instanceof String && ((String) property).isEmpty())) {
                    context.put((String) key, property);
                }
            }
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        report.process(context, outputStream);
        return outputStream.toByteArray();
    } catch (IOException | XDocReportException | VelocityException e) {
        throw new DocumentTemplateException(String.format(I18nProperties.getString(Strings.errorDocumentGeneration), templateFile.getName()));
    }
}
Also used : IXDocReport(fr.opensagres.xdocreport.document.IXDocReport) IContext(fr.opensagres.xdocreport.template.IContext) DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) VelocityException(org.apache.velocity.exception.VelocityException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XDocReportException(fr.opensagres.xdocreport.core.XDocReportException)

Aggregations

IContext (fr.opensagres.xdocreport.template.IContext)8 IXDocReport (fr.opensagres.xdocreport.document.IXDocReport)7 Options (fr.opensagres.xdocreport.converter.Options)5 InputStream (java.io.InputStream)5 XDocReportException (fr.opensagres.xdocreport.core.XDocReportException)4 FieldsMetadata (fr.opensagres.xdocreport.template.formatter.FieldsMetadata)4 IOException (java.io.IOException)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 Project (com.gpcoder.model.Project)2 XDocConverterException (fr.opensagres.xdocreport.converter.XDocConverterException)2 ByteArrayInOutStream (fr.univlorraine.ecandidat.utils.ByteArrayInOutStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 DocumentTemplateException (de.symeda.sormas.api.docgeneneration.DocumentTemplateException)1 ByteArrayImageProvider (fr.opensagres.xdocreport.document.images.ByteArrayImageProvider)1 IImageProvider (fr.opensagres.xdocreport.document.images.IImageProvider)1 Candidat (fr.univlorraine.ecandidat.entities.ecandidat.Candidat)1