DEV Community

Vũ Việt Anh
Vũ Việt Anh

Posted on

How to convert excel xlsx to PDF in JAVA?

Error: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

public byte[] createAndConvertExcelToPdf(byte[] excelFileContent)
throws IOException, ParserConfigurationException, DocumentException {
ByteArrayInputStream inputFile = new ByteArrayInputStream(excelFileContent);
ByteArrayOutputStream outputFile = new ByteArrayOutputStream();

Document doc = ExcelToHtmlConverter.process(inputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.layout();
renderer.createPDF(outputFile);

var content = outputFile.toByteArray();
outputFile.close();
inputFile.close();
return content;

}

Top comments (0)