Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Converting a word file(.doc ,.docx) and excel file to PDF version

Avatar

Level 4

We have a requirement to display the doc ,docx ,excel file in PDF version. For this ,I got idea from the reference link as below :
eaem-extensions/CreatePDFRendition.java at master · schoudry/eaem-extensions · GitHub

The code explains that we can add a process step in DAM update workflow and it will create a PDF version rendition for that particular asset.

I would like to have a similar thing for DOC,DOCX ,XLS,XLSX extension files .
Please help me for the above the requirement.

1 Accepted Solution

Avatar

Correct answer by
Level 10

You need to build a custom AEM service that uses an API that can convert these doc types.

For example -- itext - Java:using apache POI how to convert ms word file to pdf? - Stack Overflow

View solution in original post

5 Replies

Avatar

Correct answer by
Level 10

You need to build a custom AEM service that uses an API that can convert these doc types.

For example -- itext - Java:using apache POI how to convert ms word file to pdf? - Stack Overflow

Avatar

Level 4

Yes I tried to do using the below Classes :

  
PdfOptions options=null;
  
PdfConverter.getInstance().convert(document,out,options);
  with the correct dependency added in the POM  : fr.opensagres.poi.xwpf.converter.pdf .


It is throwing exception "NoClassDefinationFound" as it is trying to find the classes in com.adobe.granite.poi package.
I have provided the dependency artifact in Embed-Dependency as well . It didn't work for me.

Also the opensagres package related classes will work only with Apache poi 3.17 as per the below article.
apache - Docx to Pdf Converter in java - Stack Overflow

Here it is trying to pick POI references from AEM POI bundle but not from the one I specified as dependency in POM

1685220_pastedImage_6.png

Avatar

Level 10

What is the scope of apache-poi dependency in your pom.xml and is that scope being mentioned in Import-Packages?

Can you validate if it is getting packaged/bundled with your source code bundle(within core bundle or the bundle that contains your pdf code)? 

It's probably not getting packaged or the maven-bundle-plugin configurations are not appropriate hence, the code is trying to pull the Felix dependencies rather than using the bundled dependencies.

If possible, could you share the pom.xml?

Avatar

Level 10

I got this POI working to convert a DOC to PDF.

To make this work within AEM - you need to make sure that you include these JARs into AEM as OSGi bundles.

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.OutputStream;

import org.apache.poi.xwpf.converter.pdf.PdfConverter;

import org.apache.poi.xwpf.converter.pdf.PdfOptions;

import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class TestCon {

    public static void main(String[] args) throws Exception {

        String dd ="ww" ;

        try {

           String inputFile = "C:\\A\\Test.docx";

           String outputFile = "C:\\A\\TEST.pdf";

           if (args != null && args.length == 2) {

               inputFile = args[0];

               outputFile = args[1];

           }

           System.out.println("inputFile:" + inputFile + ",outputFile:" + outputFile);

           FileInputStream in = new FileInputStream(inputFile);

           XWPFDocument document = new XWPFDocument(in);

           File outFile = new File(outputFile);

           OutputStream out = new FileOutputStream(outFile);

           PdfOptions options = null;

           PdfConverter.getInstance().convert(document, out, options);

       }

       catch(Exception e)

       {

           e.printStackTrace();

       }

        System.out.println("Done!!");

       }

}

Avatar

Level 10

ALso you can remove the version information from the Manifest file in the OSGi bundle. That way - AEM Picks up the available version and you will not have a version conflict.

I have got POI API working in Java, but have not tried it within an OSGi bundle yet.