public String clean ( String input , String inputEncoding , String outputEncoding ) throws ConversionException { InputStream stringAsStream ; try { stringAsStream = new ByteArrayInputStream ( input . getBytes ( inputEncoding != null ? inputEncoding : "UTF-8" ) ) ; } catch ( UnsupportedEncodingException e ) { throw ConversionException . HTML_TO_PDF_EXCEPTION ; } ByteArrayOutputStream outputAsStream = new ByteArrayOutputStream ( ) ; Tidy htmlCleaner = new Tidy ( ) ; htmlCleaner . setInputEncoding ( inputEncoding != null ? inputEncoding : "UTF-8" ) ; htmlCleaner . setOutputEncoding ( outputEncoding != null ? outputEncoding : "UTF-8" ) ; htmlCleaner . setXHTML ( true ) ; htmlCleaner . parse ( stringAsStream , outputAsStream ) ; try { String result = outputAsStream . toString ( outputEncoding != null ? outputEncoding : "UTF-8" ) ; return result . trim ( ) ; } catch ( UnsupportedEncodingException e ) { throw ConversionException . HTML_TO_PDF_EXCEPTION ; } }
Clean - up HTML code with specified input and output encodings .