-
Notifications
You must be signed in to change notification settings - Fork 374
DocxReportingJavaMainConverter
Mohammad Naghavi edited this page Aug 5, 2015
·
3 revisions
XDocReport give you the capability to convert generated docx report to another format like XHTML and PDF.
XDocReport converter are modular, it means that you can choose the converter implementation and implement your own converter if you need.
XDocReport provides several converter implementation (2 PDF via FOP with XSL, 2 PDF via IText with POI-HWPF...) After developing several implementation we choose to keep 2 PDF via IText with POI-HWPF because it's more performant and it's more easy to develop Java code instead of developing XSL.
If you wish to :
- use Docx with Freemarker and convert report To PDF/XHTML, you could download *docxandfreemarker.converter--
**
-sample.zip. - use Docx with Velocity and convert report To PDF/XHTML, you could download *docxandvelocity.converter-
**
-sample.zip.
Add first you must add in your Java project the same JARs explained at Java Main section.
**JARs name** | **Description** | **OSGi Bundle/Fragment** |
fr.opensagres.xdocreport.converter.docx.xwpf | PDF/XHTML XDocReport converter implementation | Fragment -> fr.opensagres.xdocreport.converter |
**JARs name** | **Description** | **OSGi Bundle/Fragment** |
org.apache.poi.xwpf.converter | POI converter | Bundle |
fr.opensagres.xdocreport.itext.extension | XHTML+IText extension |
**JARs name** | **Description** | **OSGi Bundle/Fragment** |
poi-3.11.jar | POI | org.apache.poi |
poi-ooxml-3.11.jar | POI OOXML | org.apache.poi |
ooxml-schemas-1.1.jar | OOXML Schemas | org.apache.poi |
xmlbeans-2.3.0.jar | XML Beans | org.apache.xmlbeans |
dom4j-1.6.1.jar | DOM4J | dom4j |
If you wish generate PDF, add IText JARs :
**JARs name** | **Description** | **OSGi Bundle/Fragment** |
[itext-2.1.7.jar](http://search.maven.org/#artifactdetails|com.lowagie|itext|2.1.7|jar) | IText PDF | Bundle |
Instead of doing :
report.process(context, out);
you must do :
Options options = Options.getTo(ConverterTypeTo.XHTML).via(ConverterTypeVia.POI);
report.convert(context, options, out);
package fr.opensagres.xdocreport.samples.docxandvelocity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.ConverterTypeVia;
import fr.opensagres.xdocreport.converter.Options;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.samples.docxandvelocity.model.Project;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
public class DocxProjectWithVelocity2XHTML {
public static void main(String[args) {
try {
// 1) Load Docx file by filling Velocity template engine and cache
// it to the registry
InputStream in = DocxProjectWithVelocity2XHTML.class
.getResourceAsStream("DocxProjectWithVelocity.docx");
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
in, TemplateEngineKind.Velocity);
// 2) Create context Java model
IContext context = report.createContext();
Project project = new Project("XDocReport");
context.put("project", project);
// 3) Generate report by merging Java model with the Docx
OutputStream out = new FileOutputStream(new File(
"DocxProjectWithVelocity_Out.html"));
// report.process(context, out);
Options options = Options.getTo(ConverterTypeTo.XHTML).via(
ConverterTypeVia.XWPF);
report.convert(context, options, out);
} catch (IOException e) {
e.printStackTrace();
} catch (XDocReportException e) {
e.printStackTrace();
}
}
}
package fr.opensagres.xdocreport.samples.docxandvelocity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.ConverterTypeVia;
import fr.opensagres.xdocreport.converter.Options;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.samples.docxandvelocity.model.Developer;
import fr.opensagres.xdocreport.samples.docxandvelocity.model.Project;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
public class DocxProjectWithVelocityListXHTML {
public static void main(String[](]) args) {
try {
// 1) Load Docx file by filling Velocity template engine and cache
// it to the registry
InputStream in = DocxProjectWithVelocityListXHTML.class
.getResourceAsStream("DocxProjectWithVelocityList.docx");
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
in, TemplateEngineKind.Velocity);
// 2) Create context Java model
IContext context = report.createContext();
// Register project
Project project = new Project("XDocReport");
context.put("project", project);
// Register developers list
List<Developer> developers = new ArrayList<Developer>();
developers.add(new Developer("ZERR", "Angelo", "[email protected]"));
developers.add(new Developer("Leclercq", "Pascal", "[email protected]"));
context.put("developers", developers);
// 3) Create fields metadata to manage lazy loop (#forech velocity) for table row.
FieldsMetadata metadata = new FieldsMetadata();
metadata.addFieldAsList("developers.Name");
metadata.addFieldAsList("developers.LastName");
metadata.addFieldAsList("developers.Mail");
report.setFieldsMetadata(metadata);
// 4) Generate report by merging Java model with the Docx
OutputStream out = new FileOutputStream(new File(
"DocxProjectWithVelocityList_Out.html"));
//report.process(context, out);
Options options = Options.getTo(ConverterTypeTo.XHTML).via(
ConverterTypeVia.XWPF);
report.convert(context, options, out);
} catch (IOException e) {
e.printStackTrace();
} catch (XDocReportException e) {
e.printStackTrace();
}
}
}
Instead of doing :
report.process(context, out);
you must do :
Options options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.XWPF);
report.convert(context, options, out);
package fr.opensagres.xdocreport.samples.docxandvelocity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.ConverterTypeVia;
import fr.opensagres.xdocreport.converter.Options;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.samples.docxandvelocity.model.Project;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
public class DocxProjectWithVelocity2PDF {
public static void main(String[args) {
try {
// 1) Load Docx file by filling Velocity template engine and cache
// it to the registry
InputStream in = DocxProjectWithVelocity2PDF.class
.getResourceAsStream("DocxProjectWithVelocity.docx");
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
in, TemplateEngineKind.Velocity);
// 2) Create context Java model
IContext context = report.createContext();
Project project = new Project("XDocReport");
context.put("project", project);
// 3) Generate report by merging Java model with the Docx
OutputStream out = new FileOutputStream(new File(
"DocxProjectWithVelocity_Out.pdf"));
// report.process(context, out);
Options options = Options.getTo(ConverterTypeTo.PDF).via(
ConverterTypeVia.XWPF);
report.convert(context, options, out);
} catch (IOException e) {
e.printStackTrace();
} catch (XDocReportException e) {
e.printStackTrace();
}
}
}
package fr.opensagres.xdocreport.samples.docxandvelocity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.ConverterTypeVia;
import fr.opensagres.xdocreport.converter.Options;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.samples.docxandvelocity.model.Developer;
import fr.opensagres.xdocreport.samples.docxandvelocity.model.Project;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
public class DocxProjectWithVelocityList2PDF {
public static void main(String[](]) args) {
try {
// 1) Load Docx file by filling Velocity template engine and cache
// it to the registry
InputStream in = DocxProjectWithVelocityList2PDF.class
.getResourceAsStream("DocxProjectWithVelocityList.docx");
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
in, TemplateEngineKind.Velocity);
// 2) Create context Java model
IContext context = report.createContext();
// Register project
Project project = new Project("XDocReport");
context.put("project", project);
// Register developers list
List<Developer> developers = new ArrayList<Developer>();
developers.add(new Developer("ZERR", "Angelo", "[email protected]"));
developers.add(new Developer("Leclercq", "Pascal", "[email protected]"));
context.put("developers", developers);
// 3) Create fields metadata to manage lazy loop (#forech velocity) for table row.
FieldsMetadata metadata = new FieldsMetadata();
metadata.addFieldAsList("developers.Name");
metadata.addFieldAsList("developers.LastName");
metadata.addFieldAsList("developers.Mail");
report.setFieldsMetadata(metadata);
// 4) Generate report by merging Java model with the Docx
OutputStream out = new FileOutputStream(new File(
"DocxProjectWithVelocityList_Out.pdf"));
// report.process(context, out);
Options options = Options.getTo(ConverterTypeTo.PDF).via(
ConverterTypeVia.XWPF);
report.convert(context, options, out);
} catch (IOException e) {
e.printStackTrace();
} catch (XDocReportException e) {
e.printStackTrace();
}
}
}
- Overview
- Getting Started
- FAQ
- Which License Applies
- Download
- Developer's Guide
- User's Guide
- Contributor's Guide
- Acknowledgment
- Articles
- Releases