I have a p:editor in primefaces where the users are pasting word documents having some email templates and saving it in DB.
Now I need to convert this content into pdf. But what I am getting returned from DB is an HTML conversion of that word document.
While parsing this HTML content with iText, I am running into lot of errors beacause of invalid xhtml like below
<span style="font-family: Arial, Verdana; font-size: 13.3333px;"><img src="9#credit_cards_logos#9"></span>
With above snippet, I am getting error invalid span tag. Expected closing img tag. When I remove span tag around img, it works fine.
Now errors like this are all over the place. And it's not possible to manually go and fix all of them as it's a huge template (there are 100s of templates.)
Here is my function which I am using to parse it.
public StreamedContent getFile() throws IOException, DocumentException{
final PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance().getExternalContext()
.getResponse();
final HttpServletResponse res = PortalUtil.getHttpServletResponse(portletResponse);
res.setContentType("application/pdf");
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
res.setHeader("Content-Disposition", "attachment; filename=" + subject + ".pdf");
res.setHeader("Refresh", "1");
res.flushBuffer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream out = res.getOutputStream();
Document document = new Document(PageSize.LETTER);
PdfWriter pdfWriter =PdfWriter.getInstance(document, baos);
document.open();
document.addCreationDate();
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
//htmlWorker.parse(new StringReader(getMessage()));
worker.parseXHtml(pdfWriter, document, new StringReader(getMessage()));
document.close();
baos.writeTo(out);
out.flush();
out.close();
return null;
}
Is there a workaround to this ?
EDIT____________
Is there something like p:dataExporter(only for datatables) in primefaces which will convert the contents into pdf without the need to parse the HTML.