I am trying to generate a document thanks to the plugin:
xdocreport-1.0.4.jar
Let's say that it has worked for me from the PC, following a template to generate another one with the data entered in the form but from the android phone I can not make it work, I imagine that the problem is in the GeneradorDocumentosService by the TOURL ...
I have two classes:
- Main.java
- GeneradorDocumentosService.java
My document Test.docx within Assets with its variables with MergeField
My problem when I gave it to generate the file:
In the class Main.java I have this, full code Main.Java
public static void generateDocument(String rutaPlantilla, String extension,
boolean convertirPdf)
throws IOException, XDocReportException
{
Map<String, Object> variablesMap = new HashMap<String, Object>();
variablesMap.put("name", name);
variablesMap.put("surname", surname);
variablesMap.put("date", date);
// 2) Create fields metadata to manage lazy loop (#forech velocity)
// for table row.
FieldsMetadata metadata = new FieldsMetadata();
metadata.addFieldAsList("listaNumeros.Numero");
metadata.addFieldAsList("listaNumeros.Cuadrado");
metadata.addFieldAsList("listaNumeros.Raiz");
// Mapa con las variables de tipo imagen. Estas variables contienen el path
de la imagen
Map<String, String> imagenesMap = new HashMap<String, String>();
imagenesMap.put("header_image_logo", "./Logo.png");
GeneradorDocumentosService generadorDocumentosService = new
GeneradorDocumentosService();
byte[] mergedOutput =
generadorDocumentosService.generarDocumento(rutaPlantilla,
TemplateEngineKind.Freemarker, variablesMap, imagenesMap,
convertirPdf, metadata
);
FileOutputStream os = new FileOutputStream("Faycan_Contract_"+"."
+extension);
os.write(mergedOutput);
os.flush();
os.close();
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNombre = (EditText) findViewById(R.id.txtNombre);
txtApellidos = (EditText) findViewById(R.id.txtApellidos);
txtFecha = (EditText) findViewById(R.id.txtFecha);
name = txtNombre.getText().toString();
surname = txtApellidos.getText().toString();
date = txtFecha.getText().toString();
btnGenerar = (Button) findViewById(R.id.btnGenerar);
btnGenerar.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
generateDocument("file:///android_asset/test.docx", "docx", false);
} catch (Exception e)
{
Toast.makeText(getApplicationContext(), "The document could not be
generated"+e, Toast.LENGTH_SHORT).show();
}
}
});
}
In the class GeneratorDocumentsService.java I have this that could be the error, full code GeneradorDocumentosService.Java
public static InputStream loadDocumentAsStream(String filePath) throws
IOException
{
URL url = new File(filePath).toURI().toURL();
InputStream documentAsStream = url.openStream();
return documentAsStream;
}