code: 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;
          }
      }
docs: Clean-up HTML code with specified input and output encodings.
  
  @param input HTML text
  @param inputEncoding A valid java encoding name. Uses UTF-8 if null.
  @param outputEncoding A valid java encoding name. Uses UTF-8 if null.
  @return
  @throws ConversionException