Example usage for org.jsoup.helper W3CDom W3CDom

List of usage examples for org.jsoup.helper W3CDom W3CDom

Introduction

In this page you can find the example usage for org.jsoup.helper W3CDom W3CDom.

Prototype

W3CDom

Source Link

Usage

From source file:org.niord.core.fm.pdf.HtmlToPdfRenderer.java

/**
 * Renders the PDF from the HTML file/*from   w  ww . j  a  v a 2s. c om*/
 */
public void render() throws Exception {

    // Clean up HTML. Sometimes they paste html into fields that contain illegal tags, e.g. "<o:p></o:p>"
    cleanUpHtml();

    // Update all SVG elements
    updateSvgElements();

    // Convert the JSoup Document to an XML Document
    W3CDom w3cDom = new W3CDom();
    org.w3c.dom.Document xhtmlContent = w3cDom.fromJsoup(doc);

    // NB: We cannot use JTidy to clean the document, as it will remove all SVG tags :-(
    // TextUtils.cleanHtml(html);

    // Generate PDF from the HTML
    ITextRenderer renderer = new ITextRenderer();

    // Add support for SVG in PDF generation
    ChainingReplacedElementFactory chainingReplacedElementFactory = new ChainingReplacedElementFactory();
    chainingReplacedElementFactory
            .addReplacedElementFactory(renderer.getSharedContext().getReplacedElementFactory());
    chainingReplacedElementFactory.addReplacedElementFactory(new SVGReplacedElementFactory());
    renderer.getSharedContext().setReplacedElementFactory(chainingReplacedElementFactory);

    // Check if we need to encrypt the PDF
    if (StringUtils.isNotBlank(pdfEncryptionPassword)) {
        renderer.setPDFEncryption(new PDFEncryption(null, pdfEncryptionPassword.getBytes()));
    }

    renderer.setDocument(xhtmlContent, baseUri);
    renderer.layout();
    renderer.createPDF(pdf);
}