We are facing out of memory : Java heap space error while converting docx to html when the document size goes above 300KB and the server hangs. We don't have control on the server parameters.
Code is given below. Would be helpful if there are any solutions.
// Convert .docx file to html file and return .html file as File Object
public File convertDocxToHtml(File inputFile)
{
//String str=inputFile.getName().replace(".", " ");
String inputfilepath = inputFile.getAbsolutePath();
int index = inputFile.getName().indexOf(".");
String outputfilepath = tempDir + "\\"+inputFile.getName().substring(0, index)+".html";
File outputFile = null ;
try
{
//XHTMLImporter.setHyperlinkStyle("Hyperlink");
if (inputfilepath.endsWith("docx"))
{
WordprocessingMLPackage docx = WordprocessingMLPackage.load(new File(inputfilepath));
AbstractHtmlExporter exporter = new HtmlExporterNG2();
OutputStream os = new java.io.FileOutputStream(outputfilepath);
HtmlSettings htmlSettings = new HtmlSettings();
htmlSettings.setImageDirPath(outputfilepath + "_Images");
htmlSettings.setImageTargetUri(outputfilepath.substring(inputfilepath.lastIndexOf("\\") + 1) + "_Images");
javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(os);
exporter.html(docx, result, htmlSettings);
StringBuilder stringBuilder = new StringBuilder();
outputFile = new File(outputfilepath);
// get the StringBuilder class from input file
stringBuilder = this.getFileContents(outputFile);
//stringBuilder = stringBuilder.replace(stringBuilder.indexOf("<style>"),stringBuilder.indexOf("</style>")+8 , "");
//String htmlPage = stringBuilder.toString().replaceAll("%0A ", "");
String htmlPage = stringBuilder.toString().replaceAll("%0A ", "");
stringBuilder=null;
String htmlPage1 = htmlPage.replaceAll("height: 5mm;", "height: auto;");
FileUtils.writeStringToFile(new File(outputfilepath), htmlPage1);
}
else
{
LOGGER.error(" Error : ConvertDocxToHTML.java convertDocxToHtml(File inputFile) : Extenssion is Other than .docx");
}
}
catch (Exception e)
{
e.printStackTrace();
LOGGER.error(" Error : ConvertDocxToHTML.java convertDocxToHtml(File inputFile) : "+ e.getMessage());
}
return outputFile;
}