Browse Source

Do not persist file on harddrive but redirect from stdOut into byteArray, save to file if explicitly requested

Patrick Brückner 10 năm trước cách đây
mục cha
commit
1c7cc9f94f

+ 41 - 16
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/Pdf.java

@@ -1,8 +1,6 @@
 package br.eti.mertz.wkhtmltopdf.wrapper;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.OutputStream;
+import java.io.*;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -65,28 +63,55 @@ public class Pdf implements PdfService {
 	}
 
 	/**
-	 * TODO save file and returns
-	 * 
 	 * @throws IOException
 	 * @throws InterruptedException
 	 */
 	public File saveAs(String path) throws IOException, InterruptedException {
-		Runtime rt = Runtime.getRuntime();
-        String command = this.commandWithParameters() + Symbol.separator + path;
-        Process proc = rt.exec(command);
+        byte[] pdf = this.getPDF();
+        File file = new File(path);
+
+        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
+        bufferedOutputStream.write(pdf);
+        bufferedOutputStream.flush();
+        bufferedOutputStream.close();
+
+        return file;
+	}
+
+    public byte[] getPDF() throws IOException, InterruptedException {
+        Runtime runtime = Runtime.getRuntime();
+        if(htmlFromString && !this.params.contains(new Param("-"))) {
+            this.addParam(new Param("-"));
+        }
+        String command = this.commandWithParameters() + Symbol.separator + "-";
+        Process process = runtime.exec(command);
         if(htmlFromString) {
-            OutputStream stdin = proc.getOutputStream();
-            stdin.write(htmlInput.getBytes());
-            stdin.close();
+            OutputStream stdInStream = process.getOutputStream();
+            stdInStream.write(htmlInput.getBytes());
+            stdInStream.close();
         }
+        InputStream stdOutStream = process.getInputStream();
+        InputStream stdErrStream = process.getErrorStream();
+        process.waitFor();
 
-		proc.waitFor();
-        if(proc.exitValue() != 0) {
-            throw new RuntimeException("Process (" + command + ") exited with status code " + proc.exitValue());
+        ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
+        ByteArrayOutputStream stdErr = new ByteArrayOutputStream();
+
+        for(int i = 0; i < stdOutStream.available(); i++) {
+            stdOut.write((char) stdOutStream.read());
+        }
+        stdOutStream.close();
+        for(int i = 0; i < stdErrStream.available(); i++) {
+            stdErr.write((char) stdErrStream.read());
         }
+        stdErrStream.close();
 
-		return new File(path);
-	}
+        if(process.exitValue() != 0) {
+            throw new RuntimeException("Process (" + command + ") exited with status code " + process.exitValue() + ":\n"+new String(stdErr.toByteArray()));
+        }
+
+        return stdOut.toByteArray();
+    }
 
 	public String commandWithParameters() {
 		StringBuilder sb = new StringBuilder();

+ 1 - 0
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/PdfService.java

@@ -13,4 +13,5 @@ public interface PdfService {
 	
 	File saveAs(String path) throws IOException, InterruptedException;
 
+	byte[] getPDF() throws IOException, InterruptedException;
 }