예제 #1
0
  @Test
  public void testConverter() throws Exception {
    String converterName = cs.getConverterName("text/plain", "application/pdf");
    assertEquals("any2pdf", converterName);

    checkConverterAvailability(converterName);
    checkCommandAvailability("soffice");

    BlobHolder pdfBH = getBlobFromPath("test-docs/hello.txt");
    Map<String, Serializable> parameters = new HashMap<>();

    BlobHolder result = cs.convert(converterName, pdfBH, parameters);
    assertNotNull(result);

    List<Blob> blobs = result.getBlobs();
    assertNotNull(blobs);
    assertEquals(1, blobs.size());

    Blob mainBlob = result.getBlob();
    String text = DocumentUTUtils.readPdfText(mainBlob.getFile());
    assertTrue(text.contains("Hello") || text.contains("hello"));
  }
예제 #2
0
  @Override
  public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters)
      throws ConversionException {
    try {
      // Make sure the toThumbnail command is available
      CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
      CommandAvailability commandAvailability = cles.getCommandAvailability(THUMBNAIL_COMMAND);
      if (!commandAvailability.isAvailable()) {
        return null;
      }
      // get the input and output of the command
      Blob blob = blobHolder.getBlob();

      Blob targetBlob = Blobs.createBlobWithExtension(".png");
      targetBlob.setMimeType("image/png");
      try (CloseableFile source = blob.getCloseableFile()) {
        CmdParameters params = new CmdParameters();
        String size;
        if (parameters != null && parameters.containsKey(THUMBNAIL_SIZE_PARAMETER_NAME)) {
          size = (String) parameters.get(THUMBNAIL_SIZE_PARAMETER_NAME);
        } else {
          size = THUMBNAIL_DEFAULT_SIZE;
        }
        params.addNamedParameter(THUMBNAIL_SIZE_PARAMETER_NAME, size);
        params.addNamedParameter("inputFilePath", source.getFile());
        params.addNamedParameter("outputFilePath", targetBlob.getFile());

        ExecResult res = cles.execCommand(THUMBNAIL_COMMAND, params);
        if (!res.isSuccessful()) {
          throw res.getError();
        }
      }
      return new SimpleCachableBlobHolder(targetBlob);
    } catch (CommandNotAvailable | IOException | ClientException | CommandException e) {
      throw new ConversionException("Thumbnail conversion failed", e);
    }
  }