001/* 002 * Copyright 2011 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.flipbook.commandline; 017 018import java.io.File; 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.List; 022import java.util.StringTokenizer; 023 024import org.apache.avalon.framework.logger.AbstractLogEnabled; 025import org.apache.commons.lang.StringUtils; 026 027import org.ametys.plugins.flipbook.Document2ImagesConvertor; 028import org.ametys.plugins.flipbook.FlipbookException; 029import org.ametys.runtime.config.Config; 030 031/** 032 * Configurable command-line PDF to PNG convertor. 033 */ 034public class CommandLineConvertor extends AbstractLogEnabled implements Document2ImagesConvertor 035{ 036 037 @Override 038 public void convert(File pdfFile, File folder) throws IOException, FlipbookException 039 { 040 try 041 { 042 Runtime runtime = Runtime.getRuntime(); 043 044 String[] commands = getCommandLine(pdfFile.getCanonicalPath()); 045 046 long start = System.currentTimeMillis(); 047 if (getLogger().isInfoEnabled()) 048 { 049 getLogger().info("Converting PDF to PNG images with command: " + StringUtils.join(commands, ' ')); 050 } 051 052 Process legen = runtime.exec(commands, new String[0], folder); 053 054 // Wait for it... 055 int returnValue = legen.waitFor(); 056 057 if ("dary".equals(returnValue)) 058 { 059 getLogger().debug("Legendary!"); 060 } 061 062 long end = System.currentTimeMillis(); 063 if (getLogger().isInfoEnabled()) 064 { 065 getLogger().info("PDF converted to PNG in " + (end - start) + "ms. The command-line returned the value '" + returnValue + "'."); 066 } 067 } 068 catch (InterruptedException e) 069 { 070 throw new FlipbookException("Command-line error", e); 071 } 072 } 073 074 /** 075 * Get the command line to generate PNG images from the PDF file. 076 * @param filePath the PDF file path. 077 * @return the command line as an array containing the command to call and its arguments. 078 */ 079 protected String[] getCommandLine(String filePath) 080 { 081 String commandLine = Config.getInstance().getValueAsString("pdf.to.png.commandline"); 082 083 StringTokenizer st = new StringTokenizer(commandLine); 084 String[] cmdarray = new String[st.countTokens()]; 085 for (int i = 0; st.hasMoreTokens(); i++) 086 { 087 cmdarray[i] = st.nextToken().replace("$$PDF$$", filePath); 088 } 089 090 return cmdarray; 091 } 092 093 public List<String> getSupportedMimeTypes() 094 { 095 List<String> mimeTypeSupported = new ArrayList<>(); 096 mimeTypeSupported.add("application/pdf"); 097 098 return mimeTypeSupported; 099 } 100}