/** Convenience method to convert the document to PDF */
 public static void toPDF(WordprocessingMLPackage wmlPackage, OutputStream outputStream)
     throws Docx4JException {
   FOSettings settings = createFOSettings();
   settings.setWmlPackage(wmlPackage);
   settings.setApacheFopMime("application/pdf");
   toFO(settings, outputStream, FLAG_NONE);
 }
  private String setupApacheFopConfiguration(FOSettings settings) throws Docx4JException {

    String ret = settings.getApacheFopConfiguration();
    WordprocessingMLPackage wmlPackage = (WordprocessingMLPackage) settings.getWmlPackage();
    if (ret == null) {
      ret =
          FopConfigUtil.createDefaultConfiguration(
              wmlPackage.getFontMapper(), wmlPackage.getMainDocumentPart().fontsInUse());
    }
    return ret;
  }
 private String setupApacheFopMime(FOSettings settings) {
   String ret = settings.getApacheFopMime();
   if (ret == null) {
     ret = MimeConstants.MIME_PDF;
   }
   return ret;
 }
  @Override
  public void render(
      String foDocument,
      FOSettings settings,
      boolean twoPass,
      List<SectionPageInformation> pageNumberInformation,
      OutputStream outputStream)
      throws Docx4JException {

    String apacheFopConfiguration = setupApacheFopConfiguration(settings);
    String apacheFopMime = setupApacheFopMime(settings);
    StreamSource foDocumentSrc = new StreamSource(new StringReader(foDocument));
    FopPlaceholderLookup placeholderLookup = null;
    FormattingResults formattingResults = null;
    FopFactory fopFactory = null;
    try {
      fopFactory = getFopFactory(apacheFopConfiguration);
    } catch (FOPException e) {
      throw new Docx4JException(
          "Exception creating fop factory for rendering: " + e.getMessage(), e);
    }
    if (twoPass) {
      // 1st pass in 2 pass
      log.debug("1st pass in 2 pass");

      StartEvent startEvent =
          new StartEvent(settings.getWmlPackage(), WellKnownProcessSteps.FOP_RENDER_PASS1);
      startEvent.publish();

      placeholderLookup = new FopPlaceholderLookup(pageNumberInformation);
      formattingResults = calcResults(fopFactory, apacheFopMime, foDocumentSrc, placeholderLookup);
      placeholderLookup.setResults(formattingResults);
      foDocumentSrc = new StreamSource(new StringReader(foDocument));

      new EventFinished(startEvent).publish();
    }

    // 1st pass in 1 pass or 2nd pass in 2 pass

    if (TEXTBOX_POSTPROCESSING_REQUIRED) {

      //			System.out.println("\n\n performing TEXTBOX_POSTPROCESSING \n\n");

      DOMResult result = new DOMResult();
      org.docx4j.XmlUtils.transform(foDocumentSrc, xslt_POSTPROCESSING, null, result);

      org.w3c.dom.Document docResult = ((org.w3c.dom.Document) result.getNode());
      String modifiedFO = XmlUtils.w3CDomNodeToString(docResult);
      log.debug("After moving! \n" + modifiedFO);

      foDocumentSrc = new StreamSource(new StringReader(modifiedFO));
    }

    StartEvent startEvent =
        new StartEvent(settings.getWmlPackage(), WellKnownProcessSteps.FOP_RENDER_PASS1);
    startEvent.publish();

    render(fopFactory, apacheFopMime, foDocumentSrc, placeholderLookup, outputStream);

    new EventFinished(startEvent).publish();
  }