Browse Source

wkhtmltopdf exits w/ non-zero status but standard output still might contain a valid pdf (e.g. after `Exit with code 1 due to network error: ProtocolUnknownError`). Let's throw an error with full info.

Evgeni Gordeev 7 years ago
parent
commit
48a5600f4d

+ 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);
+    }
+}

+ 11 - 9
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
@@ -101,7 +104,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 +120,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 +137,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 +157,7 @@ public class Pdf {
                 FileUtils.writeStringToFile(temp, page.getSource(), "UTF-8");
 
                 commandLine.add(temp.getAbsolutePath());
-            }
-            else {
+            } else {
                 commandLine.add(page.getSource());
             }
         }