Pārlūkot izejas kodu

Merge pull request #53 from rrsdev/master

Allow a PDF to be saved directly to a file.
Jhonny Mertz 6 gadi atpakaļ
vecāks
revīzija
7d94f2670c

+ 25 - 2
src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/Pdf.java

@@ -54,6 +54,8 @@ public class Pdf {
 
     private File tempDirectory;
 
+    private String outputFilename = null;
+
     private List<Integer> successValues = new ArrayList<Integer>(Arrays.asList(0));
 
     public Pdf() {
@@ -153,6 +155,13 @@ public class Pdf {
         this.tempDirectory = tempDirectory;
     }
 
+    /**
+     * Executes the wkhtmltopdf into standard out and captures the results.
+     * @param path The path to the file where the PDF will be saved.
+     * @return
+     * @throws IOException
+     * @throws InterruptedException
+     */
     public File saveAs(String path) throws IOException, InterruptedException {
         File file = new File(path);
         FileUtils.writeByteArrayToFile(file, getPDF());
@@ -160,6 +169,20 @@ public class Pdf {
         return file;
     }
 
+    /**
+     * Executes the wkhtmltopdf saving the results directly to the specified file path.
+     * @param path The path to the file where the PDF will be saved.
+     * @return
+     * @throws IOException
+     * @throws InterruptedException
+     */
+    public File saveAsDirect(String path)throws IOException, InterruptedException  {
+        File file = new File(path);
+        outputFilename = file.getAbsolutePath();
+        getPDF();
+        return file;
+    }
+
     public byte[] getPDF() throws IOException, InterruptedException, PDFExportException {
 
         ExecutorService executor = Executors.newFixedThreadPool(2);
@@ -191,7 +214,7 @@ public class Pdf {
         }
     }
 
-    private String[] getCommandAsArray() throws IOException {
+    protected String[] getCommandAsArray() throws IOException {
         List<String> commandLine = new ArrayList<String>();
 
         if (wrapperConfig.isXvfbEnabled()) {
@@ -219,7 +242,7 @@ public class Pdf {
                 commandLine.add(page.getSource());
             }
         }
-        commandLine.add(STDINOUT);
+        commandLine.add( (null != outputFilename) ? outputFilename : STDINOUT);
         logger.debug("Command generated: {}", commandLine.toString());
         return commandLine.toArray(new String[commandLine.size()]);
     }