Question

I use xdocreport 1.0.3 and I want to upload an odt template, process it and finally to get an docx processed document. How can I do this?

I tried this:

public void generateUserReport(long pcCaseId, long currentUserId) throws CMSException{
    try {
    InputStream is = new FileInputStream(CustomTemplate.TEMPLATES_PATH + "test.odt");
    IXDocReport report;
    report = XDocReportRegistry.getRegistry().loadReport(is,TemplateEngineKind.Velocity);
    IContext context = report.createContext();

    FieldsMetadata metadata = new FieldsMetadata();
    metadata.addFieldAsImage("signature");
    metadata.addFieldAsImage("logo");

     Date currentDate = new Date();
     SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");
     context.put("currentDate", df.format(currentDate));

    User user = this.userDAO.loadUser(currentUserId);
    byte[] signatureByteArr = user.getSignature();
    context.put("userName", user.getFullName());

    //TODO If exists signature, it will be added, in other case?
    if (signatureByteArr!=null){
        FileOutputStream fos = new FileOutputStream(CustomTemplate.TEMPLATES_PATH + "signature.jpg");
        report.setFieldsMetadata(metadata);
        fos.write(signatureByteArr);
        FileImageProvider signature = new FileImageProvider(new File(CustomTemplate.TEMPLATES_PATH,"signature.jpg"));
        context.put("signature", signature);
    }else{
        FileImageProvider noImage = new FileImageProvider(new File(CustomTemplate.TEMPLATES_PATH, "1px.gif"));
        context.put("signature", noImage);
    }

    FileImageProvider logo = new FileImageProvider(new File("TEMPLATES_PATH, logo.gif"));
    context.put("logo", logo);

    OutputStream out = new FileOutputStream(new File(CustomTemplate.TEMPLATES_PATH, "OutPut.docx"));
    report.process(context, out);
    System.out.println("Success");
    } catch (IOException e) {
    System.out.println("IO exception");
    } catch (XDocReportException e) {
        System.out.println("XDocException");
        e.printStackTrace();
    }
}

I get an OutPut.docx, but "de facto" it is an odt document and Microsoft Office cannot open it without error details. OpenOffice open it without any problems.

Was it helpful?

Solution

In short answer, XDocReport doesn't support odt->docx converter.

You use report.process which means that there is no conversion (docx template->docx report, odt template->odt report). In your sample, the generated report is an odt (even if you set filename with docx extension), that's why OpenOffice is enable to open it although MS Word cannot open it.

If you wish to convert the report to other format like html, pdf you must use report.convert but in your case you need a docx->odt converter that XDocReport doesn't provide. But as XDocReport is modular you could develop your own docx->odt converter and plug it with XDocReport.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top