Skip to content

Commit

Permalink
Merge pull request #53 from rrsdev/master
Browse files Browse the repository at this point in the history
Allow a PDF to be saved directly to a file.
  • Loading branch information
jhonnymertz authored Mar 24, 2019
2 parents b8a5ce3 + 2f72a2f commit 7d94f26
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/Pdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -153,13 +155,34 @@ public void setTempDirectory(File tempDirectory) {
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());
logger.info("PDF successfully saved in {}", file.getAbsolutePath());
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);
Expand Down Expand Up @@ -191,7 +214,7 @@ public byte[] getPDF() throws IOException, InterruptedException, PDFExportExcept
}
}

private String[] getCommandAsArray() throws IOException {
protected String[] getCommandAsArray() throws IOException {
List<String> commandLine = new ArrayList<String>();

if (wrapperConfig.isXvfbEnabled()) {
Expand Down Expand Up @@ -219,7 +242,7 @@ private String[] getCommandAsArray() throws IOException {
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()]);
}
Expand Down

0 comments on commit 7d94f26

Please sign in to comment.