br.com.jarch.docx4j.Docx4jBuilder Maven / Gradle / Ivy
The newest version!
package br.com.jarch.docx4j;
import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.Options;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Docx4jBuilder {
private static final int BLOCK_SIZE = 1024;
private InputStream docxStream;
private byte[] docxBytes;
private Map keyValues;
private List listFieldsMetaDataList;
public Docx4jBuilder fromDocx(InputStream docxStream) {
this.docxStream = docxStream;
return this;
}
public Docx4jBuilder fromDocx(byte[] docxBytes) {
this.docxBytes = docxBytes;
return this;
}
public Docx4jBuilder addFieldMetaData(String fieldMetaData) {
if (listFieldsMetaDataList == null) {
listFieldsMetaDataList = new ArrayList<>();
}
listFieldsMetaDataList.add(fieldMetaData);
return this;
}
public Docx4jBuilder withFieldsMetaDataList(List fieldsMetaDataList) {
this.listFieldsMetaDataList = fieldsMetaDataList;
return this;
}
public Docx4jBuilder addKeyValue(String key, Object value) {
if (keyValues == null) {
keyValues = new HashMap<>();
}
keyValues.put(key, value);
return this;
}
public Docx4jBuilder withKeyValues(Map keyValues) {
this.keyValues = keyValues;
return this;
}
public byte[] generatePdfBytes() {
try {
return getByte(generatePdfInputStream());
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public InputStream generatePdfInputStream() {
try {
if (docxStream == null) {
docxStream = new ByteArrayInputStream(docxBytes);
}
File filePdf = File.createTempFile("jarchdocxmergepdf", ".pdf");
filePdf.deleteOnExit();
try (OutputStream osPdf = new FileOutputStream(filePdf)) {
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(docxStream, TemplateEngineKind.Freemarker);
report.setCacheOriginalDocument(true);
if (listFieldsMetaDataList != null && !listFieldsMetaDataList.isEmpty()) {
FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
listFieldsMetaDataList.forEach(fieldsMetadata::addFieldAsList);
}
report.convert(keyValues, Options.getTo(ConverterTypeTo.PDF), osPdf);
return new FileInputStream(filePdf);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
// private static byte[] getByte(File arquivo) throws IOException {
// Path path = Paths.get(arquivo.getAbsolutePath());
// return Files.readAllBytes(path);
// }
private static byte[] getByte(InputStream is) throws IOException {
byte[] buf;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
buf = new byte[BLOCK_SIZE];
int len;
while ((len = is.read(buf, 0, BLOCK_SIZE)) != -1) {
bos.write(buf, 0, len);
}
buf = bos.toByteArray();
return buf;
}
public static void main(String[] args) throws Exception {
// try {
/*
byte[] bytes = new Docx4jBuilder()
.fromDocx(new FileInputStream("/home/wagner/Project/WESA/jarch/jarch-core/src/main/java/br/com/jarch/docx/TesteDocx4J.docx"))
.addKeyValue("Campo1", "Campo de Mesclagem via RUNTIME")
.generatePdfBytes();
File tempFile = File.createTempFile("aaa", ".pdf");
FileUtils.save(tempFile, bytes);
*/
/*
InputStream templateInputStream = new FileInputStream("/home/wagner/Project/WESA/jarch/jarch-core/src/main/java/br/com/jarch/docx/TesteDocx4J.docx");
//this.getClass().getClassLoader().getResourceAsStream(TEMPLATE_NAME);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateInputStream);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
VariablePrepare.prepare(wordMLPackage);
HashMap variables = new HashMap<>();
variables.put("Campo1", "Conteudo campo 1");
documentPart.variableReplace(variables);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
wordMLPackage.save(outputStream);
FileUtils.save(File.createTempFile("aaa", ".pdf"), outputStream.toByteArray());
// return outputStream.toByteArray();
*/
// } catch (IOException e) {
// LogUtils.generate(e);
// }
}
}