user176105 user176105 - 7 months ago 22
HTML Question

Turn CKEDITOR into a pdf

I have a ckeditor (http://ckeditor.com/) on my site. I would like for users to be able to push a button to generate a PDF. Currently, I have them press the print function that came with ckeditor, which brings up the print window and from most browsers they can generate a PDF. But I want to make it simplier. I know that generating PDFs from html is difficult, but are there any simple solutions to do this (generate a PDF from the html that ckeditor gives)?

I've heard of a few solutions like fpdf, dompdf and html2pdf.

Answer

You can use iText and XMLWorker to create PDF from HTML code.

public void createPDF() throws DocumentException, IOException 
{
    String fileName="path you want to create the document";
    Document document=new Document();
    PdfWriter pdfWriter=PdfWriter.getInstance(document, new FileOutputStream(fileName));
    document.open();
    String finall="<h1>This is a Demo</h1>";
    InputStream is = new ByteArrayInputStream(finall.getBytes());
    XMLWorkerHelper.getInstance().parseXHtml(pdfWriter,document, is);
    document.close();

}

Here we are using XML worker so all your tags should be closed correctly. You need iText and XMLWorker JAR files.Hope this will help you.