android-studioconverterspdf-conversiondocuments4j

Documents4j java library in Android App to convert Docx to Pdf


I am trying to use Documents4j java library in my Android App to convert Docx format document to PDF file but the output pdf file is damaged or corrupted. The output pdf file is empty with 0 bytes. I am using the below code to convert Docx to pdf.

    String uniqueString = UUID.randomUUID().toString();
    File outputFile = new File(Environment.getExternalStorageDirectory() + "/meer_"  + uniqueString+".pdf");

    File inputWord = new File(input);

    try  {
        InputStream docxInputStream = new FileInputStream(inputWord);
        OutputStream outputStream = new FileOutputStream(outputFile);
        IConverter converter = LocalConverter.builder().build();

        converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
        outputStream.close();
        System.out.println("success");
    } catch (Exception e) {
        e.printStackTrace();
    }


    if(outputFile.exists()){
        openPdf(outputFile);
    }

Solution

  • documents4j functions by delegating the conversion from the Java application to an instance of MS Word. This instance can run on a server which you can reach via HTTP(S) but the local converter will of course not work on Android which is unable to run MS Word.