Ejemplo n.º 1
0
  public BufferedImage processUsingTemp(InputStream input, DjatokaDecodeParam params)
      throws DjatokaException {
    File in;
    // Copy to tmp file
    try {
      String cacheDir = OpenURLJP2KService.getCacheDir();
      if (cacheDir != null) {
        in = File.createTempFile("tmp", ".pdf", new File(cacheDir));
      } else {
        in = File.createTempFile("tmp", ".pdf");
      }
      FileOutputStream fos = new FileOutputStream(in);
      in.deleteOnExit();
      IOUtils.copyStream(input, fos);
    } catch (IOException e) {
      logger.error(e, e);
      throw new DjatokaException(e);
    }

    BufferedImage bi = process(in.getAbsolutePath(), params);

    if (in != null) {
      in.delete();
    }

    return bi;
  }
Ejemplo n.º 2
0
  /**
   * Get PDF information with pdfinfo: - "Pages: X": number of pages; - "Page X size: www.ww
   * hhh.hh": size of each page, in pts.
   *
   * @returns a map: - [Pages][n] - [Page 1][111.11 222.22] - [Page i][www.ww hhh.hh] - [Page
   *     n][999.99 1000.00]
   */
  private static Map<String, String> getPDFProperties(ImageRecord input) throws DjatokaException {
    logger.debug("Getting PDF info");

    try {
      setPDFCommandsPath();
    } catch (IllegalStateException e) {
      logger.error("Failed to set PDF commands path: ", e);
      throw e;
    }

    HashMap<String, String> pdfProperties = new HashMap<String, String>();

    String sourcePath = null;

    if (input.getImageFile() != null) {
      logger.debug("PDFInfo image file: " + input.getImageFile());
      sourcePath = input.getImageFile();
    } else if (input.getObject() != null && (input.getObject() instanceof InputStream)) {
      FileInputStream fis = null;
      fis = (FileInputStream) input.getObject();
      File in;

      // Copy to tmp file
      try {
        String cacheDir = OpenURLJP2KService.getCacheDir();
        if (cacheDir != null) {
          in = File.createTempFile("tmp", ".pdf", new File(cacheDir));
        } else {
          in = File.createTempFile("tmp", ".pdf");
        }
        in.deleteOnExit();

        FileOutputStream fos = new FileOutputStream(in);
        IOUtils.copyStream(fis, fos);
      } catch (IOException e) {
        logger.error(e, e);
        throw new DjatokaException(e);
      }
      sourcePath = in.getAbsolutePath();
    } else {
      throw new DjatokaException(
          "File not defined and Input Object Type "
              + input // .getObject().getClass().getName()
              + " is not supported");
    }

    String pdfinfoCmd[] = PDFINFO_COMMAND.clone();
    pdfinfoCmd[PDFINFO_COMMAND_POSITION_BIN] = pdfinfoPath;
    pdfinfoCmd[PDFINFO_COMMAND_POSITION_FIRSTPAGE] = "1";
    pdfinfoCmd[PDFINFO_COMMAND_POSITION_LASTPAGE] =
        "-1"; // Last page even we not knowing its number.
    pdfinfoCmd[PDFINFO_COMMAND_POSITION_FILE] = sourcePath;
    Process pdfProc = null;
    try {
      ArrayList<MatchResult> pageSizes = new ArrayList<MatchResult>();
      MatchResult pages = null;

      pdfProc = Runtime.getRuntime().exec(pdfinfoCmd);
      BufferedReader lr = new BufferedReader(new InputStreamReader(pdfProc.getInputStream()));
      String line;
      for (line = lr.readLine(); line != null; line = lr.readLine()) {
        Matcher mm1 = PAGES_PATT.matcher(line);
        if (mm1.matches()) pages = mm1.toMatchResult();
        Matcher mm2 = MEDIABOX_PATT.matcher(line);
        if (mm2.matches()) pageSizes.add(mm2.toMatchResult());
      }

      int istatus = pdfProc.waitFor();
      if (istatus != 0)
        logger.error("pdfinfo proc failed, exit status=" + istatus + ", file=" + sourcePath);

      if (pages == null) {
        logger.error(
            "Did not find 'Pages' line in output of pdfinfo command: "
                + Arrays.deepToString(pdfinfoCmd));
        pdfProperties.put("Pages", "0");
      } else {
        // int n = Integer.parseInteger(pages.group(1));
        pdfProperties.put("Pages", pages.group(1));
      }

      if (pageSizes.isEmpty()) {
        logger.error(
            "Did not find \"Page X size\" lines in output of pdfinfo command: "
                + Arrays.deepToString(pdfinfoCmd));
        throw new IllegalArgumentException("Failed to get pages size of PDF with pdfinfo.");
      } else {
        for (MatchResult mr : pageSizes) {
          String page = mr.group(1);

          float x0 = Float.parseFloat(mr.group(2));
          float y0 = Float.parseFloat(mr.group(3));
          float x1 = Float.parseFloat(mr.group(4));
          float y1 = Float.parseFloat(mr.group(5));
          float w = Math.abs(x1 - x0);
          float h = Math.abs(y1 - y0);
          // Have to scale page sizes by max dpi (MAX_DPI / DEFAULT_DENSITY). Otherwise,
          // BookReader.js will request the wrong zoom level (svc.level).
          float ws = w * MAX_DPI / DEFAULT_DENSITY;
          float hs = h * MAX_DPI / DEFAULT_DENSITY;
          String width = "" + ws; // mr.group(2);
          String height = "" + hs; // mr.group(3);
          pdfProperties.put("Page " + page, width + " " + height);
        }
      }

    } catch (Exception e) {
      logger.error("Failed getting PDF information: ", e);
      throw new DjatokaException("Failed getting PDF information: ", e);
    } finally {
      // Our exec() should just consume one of the streams, but we want to stay safe.
      // http://mark.koli.ch/2011/01/leaky-pipes-remember-to-close-your-streams-when-using-javas-runtimegetruntimeexec.html
      org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getOutputStream());
      org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getInputStream());
      org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getErrorStream());
    }

    return pdfProperties;
  }