@@ -51,7 +51,7 @@ public static void setCommandConfig(String command) throws Exception {
5151 setCommand (command );
5252 }
5353
54- public int execConvertCommand (String command , final List <String > outResources ) throws InterruptedException , IOException {
54+ public int execCommand (String command , final List <String > outResources ) throws InterruptedException , IOException {
5555 LOGGER .debug (command );
5656 final Process convertProcess = Runtime .getRuntime ().exec (command );
5757 final InputStream inputStream = convertProcess .getInputStream ();
@@ -133,44 +133,61 @@ private PDFConverterDeploy deploy(File pdfFile, String outPath, boolean splitPag
133133 }
134134
135135 outPath = FileUtils .appendFileSeparator (outputDirectory .getPath ());
136+ File pdf = new File (FileUtils .getFilePrefix (pdfFile ) + "_decrypted.pdf" );
137+ File info = new File (FileUtils .appendFileSeparator (outputDirectory .getPath ()) + "info" );
138+ int pageCount = 0 ;
139+ if (!pdf .exists () && !info .exists ()) {
140+
141+ PdfReader reader = new PdfReader (pdfPath );
142+ pageCount = reader .getNumberOfPages ();
143+
144+ // if pdf is a encrypted file unencrypted
145+ if (reader .isEncrypted ()) {
146+ LOGGER .debug ("encrypted pdf! 准备另存" );
147+ pdfPath = PDFSecurer .decrypt (reader , FileUtils .getFilePrefix (pdfFile ) + "_decrypted.pdf" ).getPath ();
148+ LOGGER .debug ("PDF解密完成" );
149+ } else {
150+ LOGGER .debug ("---文档未加密---" );
151+ }
136152
137- PdfReader reader = new PdfReader (pdfPath );
138- int pageCount = reader .getNumberOfPages ();
153+ reader .close ();
139154
140- // if pdf is a encrypted file unencrypted
141- if (reader .isEncrypted ()) {
142- LOGGER .debug ("encrypted pdf! 准备另存" );
143- pdfPath = PDFSecurer .decrypt (reader , FileUtils .getFilePrefix (pdfFile ) + "_decrypted.pdf" ).getPath ();
144- LOGGER .debug ("PDF解密完成" );
145- } else {
146- LOGGER .debug ("---文档未加密---" );
147- }
155+ if (pageCount != 0 ) {
156+ OutputStream out = null ;
157+ try {
148158
149- reader . close ();
159+ info . createNewFile ();
150160
151- if (pageCount != 0 ) {
152- OutputStream out = null ;
153- try {
154- File info = new File (FileUtils .appendFileSeparator (outputDirectory .getPath ()) + "info" );
161+ out = new FileOutputStream (info );
162+
163+ out .write ((pageCount + "" ).getBytes ("UTF-8" ));
164+ } finally {
165+ if (out != null ) {
166+ out .flush ();
167+ out .close ();
168+ }
169+ }
155170
156- info .createNewFile ();
171+ }
172+ } else {
173+ pdfPath = pdf .getPath ();
174+ FileInputStream in = new FileInputStream (info );
157175
158- out = new FileOutputStream ( info );
176+ BufferedReader reader = new BufferedReader ( new InputStreamReader ( in , "UTF-8" ) );
159177
160- out .write ((pageCount + "" ).getBytes ("UTF-8" ));
178+ try {
179+ pageCount = Integer .parseInt (reader .readLine ());
180+ } catch (Exception e ) {
161181 } finally {
162- if (out != null ) {
163- out .flush ();
164- out .close ();
165- }
182+ reader .close ();
183+ in .close ();
166184 }
167-
168185 }
169186
170187 return new PDFConverterDeploy (outputDirectory , pageCount , COMMAND .replace ("${in}" , pdfPath )
171188 .replace ("${out}" , outPath + (splitPage ? "page%.swf" : "page.swf" )) +
172189 //是否将图片转换成点阵形式
173- (poly2bitmap ? " -s poly2bitmap" : "" ));
190+ (poly2bitmap ? " -s poly2bitmap -s multiply=4 " : "" ));
174191
175192 }
176193
@@ -205,11 +222,11 @@ public PDFConvertError checkError(List<String> outResources) {
205222 return error ;
206223 }
207224
208- private void errorHandler (List < String > outResources , final File pdfFile , final String outPath , boolean splitPage , boolean poly2bitmap ) throws Exception {
225+ private void errorHandler (String command , final File pdfFile , final String outPath , boolean splitPage , boolean poly2bitmap , List < String > outResources ) throws Exception {
209226 // 如果第一次没有将图片转换成点阵图报错,则进行第二次转换,并将图片转换成点阵
210227 final PDFConvertError error = checkError (outResources );
211228 if (error .getType () == PDFConvertErrorType .COMPLEX && poly2bitmap != true ) {
212- LOGGER .debug ("---第一次转换失败,进行第二次转换---" );
229+ LOGGER .debug ("---第一次转换失败,进行第二次转换 poly2bitmap = true ---" );
213230 if (splitPage ) {
214231 new Thread () {
215232 public void run () {
@@ -222,21 +239,21 @@ public void run() {
222239 }.start ();
223240 convertAsOnePageMode (pdfFile , outPath , error .getPosition () + 1 );
224241 } else {
225- convert (pdfFile , outPath , true );
242+ convert (pdfFile , outPath , splitPage , true );
226243 }
227244 } else {
228245 throw new Exception ("Conversion failed:" + error .getType () + ";\n " + StringUtils .join (outResources , "\n " ));
229246 }
230247 }
231248
232249 public void run (String command , File pdfFile , String outPath , boolean splitPage , boolean poly2bitmap ) throws Exception {
233- LOGGER .debug ("---转换开始 ---" );
250+ LOGGER .debug ("---开始 ---" );
234251 List <String > outResources = new ArrayList <String >();
235252
236- if (execConvertCommand (command , outResources ) != 0 ) {
237- errorHandler (outResources , pdfFile , outPath , splitPage , poly2bitmap );
253+ if (execCommand (command , outResources ) != 0 ) {
254+ errorHandler (command , pdfFile , outPath , splitPage , poly2bitmap , outResources );
238255 }
239- LOGGER .debug ("---转换结束 ---" );
256+ LOGGER .debug ("---结束 ---" );
240257 }
241258
242259 public File convertAsOnePageMode (File pdfFile , String outPath ) throws Exception {
@@ -272,18 +289,17 @@ public File convertAsOnePageMode(final File pdfFile, final String outPath, final
272289 public void run () {
273290 List <String > outResources = new ArrayList <String >();
274291 try {
275- if (execConvertCommand (commandExec , outResources ) != 0 ) {
292+ if (execCommand (commandExec , outResources ) != 0 ) {
276293 PDFConvertError error = checkError (outResources );
277294 if (error .getType () == PDFConvertErrorType .COMPLEX && poly2bitmap != true ) {
278295 LOGGER .debug ("---第一次转换失败,进行第二次转换---" );
279- execConvertCommand (commandExec + " -s poly2bitmap" , outResources );
296+ execCommand (commandExec + " -s poly2bitmap" , outResources );
280297 } else if (error .getType () != PDFConvertErrorType .NONE ) {
281298 throw new Exception ("Conversion failed:" + error .getType () + ";\n " + StringUtils .join (outResources , "\n " ));
282299 }
283300 }
284301 } catch (Exception e ) {
285302 LOGGER .error (e );
286- e .printStackTrace ();
287303 }
288304
289305 }
0 commit comments