Răsfoiți Sursa

Merge pull request #33 from EvgeniGordeev/pass-input-stream-on-error

Pass input stream on error
Jhonny Mertz 7 ani în urmă
părinte
comite
f28735f982

+ 46 - 0
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/PDFExportException.java

@@ -0,0 +1,46 @@
+package com.github.jhonnymertz.wkhtmltopdf.wrapper;
+
+/**
+ * PDFExportError.
+ *
+ * @author evgeni.gordeev
+ * @version 1.1.3
+ */
+public class PDFExportException extends RuntimeException {
+
+    private String command;
+
+    private int exitStatus;
+
+    private byte[] out;
+
+    private byte[] err;
+
+    public PDFExportException(String command, int exitStatus, byte[] err, byte[] out) {
+        this.command = command;
+        this.exitStatus = exitStatus;
+        this.err = err;
+        this.out = out;
+    }
+
+    public String getCommand() {
+        return command;
+    }
+
+    public int getExitStatus() {
+        return exitStatus;
+    }
+
+    public byte[] getOut() {
+        return out;
+    }
+
+    public byte[] getErr() {
+        return err;
+    }
+
+    @Override
+    public String getMessage() {
+        return "Process (" + this.command + ") exited with status code " + this.exitStatus + ":\n" + new String(err);
+    }
+}

+ 18 - 10
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/Pdf.java

@@ -15,10 +15,13 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 import java.util.UUID;
-import java.util.concurrent.*;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Represents a Pdf file
@@ -39,6 +42,8 @@ public class Pdf {
 
     private boolean hasToc = false;
 
+    private int timeout = 10;
+
     public Pdf() {
         this(new WrapperConfig());
     }
@@ -94,6 +99,10 @@ public class Pdf {
         this.tocParams.add(param, params);
     }
 
+    public void setTimeout(int timeout) {
+        this.timeout = timeout;
+    }
+
     public File saveAs(String path) throws IOException, InterruptedException {
         File file = new File(path);
         FileUtils.writeByteArrayToFile(file, getPDF());
@@ -101,7 +110,7 @@ public class Pdf {
         return file;
     }
 
-    public byte[] getPDF() throws IOException, InterruptedException {
+    public byte[] getPDF() throws IOException, InterruptedException, PDFExportException {
 
         ExecutorService executor = Executors.newFixedThreadPool(2);
 
@@ -117,9 +126,8 @@ public class Pdf {
             if (process.exitValue() != 0) {
                 byte[] errorStream = getFuture(outputStreamToByteArray);
                 logger.error("Error while generating pdf: {}", new String(errorStream));
-                throw new RuntimeException("Process (" + getCommand() + ") exited with status code " + process.exitValue() + ":\n" + new String(errorStream));
-            }
-            else{
+                throw new PDFExportException(getCommand(), process.exitValue(), errorStream, getFuture(inputStreamToByteArray));
+            } else {
                 logger.debug("Wkhtmltopdf output:\n{}", new String(getFuture(outputStreamToByteArray)));
             }
 
@@ -135,8 +143,9 @@ public class Pdf {
     private String[] getCommandAsArray() throws IOException {
         List<String> commandLine = new ArrayList<String>();
 
-        if (wrapperConfig.isXvfbEnabled())
+        if (wrapperConfig.isXvfbEnabled()) {
             commandLine.addAll(wrapperConfig.getXvfbConfig().getCommandLine());
+        }
 
         commandLine.add(wrapperConfig.getWkhtmltopdfCommand());
 
@@ -154,8 +163,7 @@ public class Pdf {
                 FileUtils.writeStringToFile(temp, page.getSource(), "UTF-8");
 
                 commandLine.add(temp.getAbsolutePath());
-            }
-            else {
+            } else {
                 commandLine.add(page.getSource());
             }
         }
@@ -173,7 +181,7 @@ public class Pdf {
 
     private byte[] getFuture(Future<byte[]> future) {
         try {
-            return future.get(10, TimeUnit.SECONDS);
+            return future.get(this.timeout, TimeUnit.SECONDS);
         } catch (Exception e) {
             throw new RuntimeException(e);
         }