Example usage for com.lowagie.text.html HtmlParser HtmlParser

List of usage examples for com.lowagie.text.html HtmlParser HtmlParser

Introduction

In this page you can find the example usage for com.lowagie.text.html HtmlParser HtmlParser.

Prototype


public HtmlParser() 

Source Link

Document

Constructs an HtmlParser.

Usage

From source file:com.mirth.connect.connectors.doc.DocumentDispatcher.java

License:Open Source License

private void createRTF(InputStream inputStream, OutputStream outputStream, DocumentDispatcherProperties props)
        throws Exception {
    com.lowagie.text.Document document = null;

    try {//www .ja v a  2  s  .  c  o  m
        document = new com.lowagie.text.Document();
        //TODO verify the character encoding

        RtfWriter2.getInstance(document, outputStream);

        document.open();

        try {
            double width = Double.parseDouble(props.getPageWidth());
            double height = Double.parseDouble(props.getPageHeight());
            Unit unit = props.getPageUnit();

            /*
             * The version of iText being used only accepts points, so we need to convert to
             * twips first and then convert to points (1 point = 20 twips).
             */
            if (unit != Unit.TWIPS) {
                width = unit.convertTo(width, Unit.TWIPS);
                height = unit.convertTo(height, Unit.TWIPS);
                unit = Unit.TWIPS;
            }
            width = Math.max(width, 1);
            height = Math.max(height, 1);
            document.setPageSize(new Rectangle((float) (Math.round(width) / RtfBasicElement.TWIPS_FACTOR),
                    (float) (Math.round(height) / RtfBasicElement.TWIPS_FACTOR)));
        } catch (Exception e) {
        }

        HtmlParser parser = new HtmlParser();
        parser.go(document, inputStream);
    } finally {
        if (document != null) {
            document.close();
        }
    }
}

From source file:com.mirth.connect.connectors.doc.DocumentMessageDispatcher.java

License:Open Source License

private void writeDocument(String template, File file, MessageObject messageObject) throws Exception {
    // add tags to the template to create a valid HTML document
    StringBuilder contents = new StringBuilder();
    if (template.lastIndexOf("<html") < 0) {
        contents.append("<html>");
        if (template.lastIndexOf("<body") < 0) {
            contents.append("<body>");
            contents.append(template);/*from www . j  a  v  a 2  s.c o m*/
            contents.append("</body>");
        } else {
            contents.append(template);
        }
        contents.append("</html>");
    } else {
        contents.append(template);
    }

    if (connector.getDocumentType().toLowerCase().equals("pdf")) {
        FileOutputStream renderFos = null;

        try {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            org.w3c.dom.Document document = builder
                    .parse(new InputSource(new StringReader(contents.toString())));

            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(document, null);
            renderFos = new FileOutputStream(file);
            renderer.layout();
            renderer.createPDF(renderFos, true);
        } catch (Throwable e) {
            throw new Exception(e);
        } finally {
            if (renderFos != null) {
                renderFos.close();
            }
        }

        if (connector.isEncrypt() && (connector.getPassword() != null)) {
            FileInputStream encryptFis = null;
            FileOutputStream encryptFos = null;

            try {
                encryptFis = new FileInputStream(file);
                PdfReader reader = new PdfReader(encryptFis);
                encryptFos = new FileOutputStream(file);
                PdfEncryptor.encrypt(reader, encryptFos, true,
                        replacer.replaceValues(connector.getPassword(), messageObject), null,
                        PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY);
            } catch (Exception e) {
                throw e;
            } finally {
                if (encryptFis != null) {
                    encryptFis.close();
                }

                if (encryptFos != null) {
                    encryptFos.close();
                }
            }
        }
    } else if (connector.getDocumentType().toLowerCase().equals("rtf")) {
        com.lowagie.text.Document document = null;

        try {
            document = new com.lowagie.text.Document();
            ByteArrayInputStream bais = new ByteArrayInputStream(contents.toString().getBytes());
            RtfWriter2.getInstance(document, new FileOutputStream(file));
            document.open();
            HtmlParser parser = new HtmlParser();
            parser.go(document, bais);
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }
}