private static byte[] pdfToPng(final byte[] pdf) throws IOException {
    synchronized (GHOST_SCRIPT_ARGS) {
      if (ghostScript == null) {
        for (final String p : new String[] { "gs", "gswin32c" }) {
          final String prog = Programs.which(p);
          if (prog != null) {
            ghostScript = prog;
            break;
          }
        }
        if (ghostScript == null) throw new IOException(
            "Can't find GhostScript (gs/gswin32c), please check"
                + " your PATH environment variable.");
      }
    }

    byte[] png;
    try {
      png = IOUtils.exec(ghostScript, GHOST_SCRIPT_ARGS, pdf, tempDir);
    } catch (final InterruptedException e) {
      throw new IOException(e);
    }

    return png;
  }