public class OpenXMLFormats2PDFViaITextConverter extends AbstractConverterEntriesSupport {

  private static final OpenXMLFormats2PDFViaITextConverter INSTANCE =
      new OpenXMLFormats2PDFViaITextConverter();

  /** Logger for this class */
  private static final Logger LOGGER =
      LogUtils.getLogger(OpenXMLFormats2PDFViaITextConverter.class.getName());

  public static OpenXMLFormats2PDFViaITextConverter getInstance() {
    return INSTANCE;
  }

  // @Override
  public void convert(final IEntryInputStreamProvider inProvider, OutputStream out, Options options)
      throws XDocConverterException {
    try {
      org.apache.poi.xwpf.converter.core.openxmlformats.IOpenXMLFormatsPartProvider provider =
          new AbstractOpenXMLFormatsPartProvider() {

            public InputStream getEntryInputStream(String entryName) {
              return inProvider.getEntryInputStream(entryName);
            }
          };
      FastPdfConverter.getInstance().convert(provider, out, toPdfOptions(options));
    } catch (Exception e) {
      LOGGER.severe(e.getMessage());
      throw new XDocConverterException(e);
    }
  }

  public PdfOptions toPdfOptions(Options options) {
    if (options == null) {
      return null;
    }
    Object value = options.getSubOptions(PdfOptions.class);
    if (value instanceof PdfOptions) {
      return (PdfOptions) value;
    }
    PdfOptions pdfOptions = PdfOptions.create();
    // Populate font encoding
    String fontEncoding = OptionsHelper.getFontEncoding(options);
    if (StringUtils.isNotEmpty(fontEncoding)) {
      pdfOptions.fontEncoding(fontEncoding);
    }
    return pdfOptions;
  }

  public MimeMapping getMimeMapping() {

    return MimeMappingConstants.PDF_MIME_MAPPING;
  }
}
public class XWPF2PDFViaITextConverter extends AbstractConverterNoEntriesSupport {

  private static final XWPF2PDFViaITextConverter INSTANCE = new XWPF2PDFViaITextConverter();

  /** Logger for this class */
  private static final Logger LOGGER =
      LogUtils.getLogger(XWPF2PDFViaITextConverter.class.getName());

  public static XWPF2PDFViaITextConverter getInstance() {
    return INSTANCE;
  }

  public void convert(InputStream in, OutputStream out, Options options)
      throws XDocConverterException {

    try {
      XWPFDocument document = new XWPFDocument(in);
      PdfConverter.getInstance().convert(document, out, toPdfOptions(options));
    } catch (Exception e) {
      LOGGER.severe(e.getMessage());
      throw new XDocConverterException(e);
    }
  }

  public PdfOptions toPdfOptions(Options options) {
    if (options == null) {
      return null;
    }
    Object value = options.getSubOptions(PdfOptions.class);
    if (value instanceof PdfOptions) {
      return (PdfOptions) value;
    }
    PdfOptions pdfOptions = PdfOptions.create();
    // Populate font encoding
    String fontEncoding = OptionsHelper.getFontEncoding(options);
    if (StringUtils.isNotEmpty(fontEncoding)) {
      pdfOptions.fontEncoding(fontEncoding);
    }
    return pdfOptions;
  }

  public MimeMapping getMimeMapping() {

    return MimeMappingConstants.PDF_MIME_MAPPING;
  }

  @Override
  public boolean isDefault() {
    return true;
  }
}
/**
 * HTML text styling transformer to transform HTML to another document kind (odt, docx, etc) syntax.
 * The ODT, DOCX is represented with the given {@link IDocumentHandler}.
 */
public class HTMLTextStylingTransformer extends AbstractTextStylingTransformer {

  public static final ITextStylingTransformer INSTANCE = new HTMLTextStylingTransformer();

  private static final String START_XML =
      "<?xml version=\"1.0\" ?>"
          + StringEscapeUtils.Entities.HTML40.generateDocType("entities")
          + "<root>";

  private static final String END_XML = "</root>";

  /** Logger for this class */
  private static final Logger LOGGER =
      LogUtils.getLogger(HTMLTextStylingTransformer.class.getName());

  private static final String[] searchList = {"\r", "\n", "\t", "&nbsp;"};

  private static final String[] replacementList = {"", "", "", " "};

  @Override
  protected void doTransform(String content, IDocumentHandler documentHandler) throws Exception {

    // pre-process content : can be used to integrate a markup based html generator like markdown
    content = generateXhtmlFromContent(content);

    // remove special characters \n, \r
    String xml = StringUtils.replaceEach(content, searchList, replacementList);
    // add root element if xml doesn't contain xml root element.
    xml = new StringBuilder(START_XML).append(xml).append(END_XML).toString();

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine(xml);
    }
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setContentHandler(new HTMLTextStylingContentHandler(documentHandler));
    xmlReader.parse(new InputSource(new StringReader(xml)));
  }

  protected String generateXhtmlFromContent(String content) throws Exception {
    return content;
  }
}