Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a PDF to be saved directly to a file. #53

Merged
merged 1 commit into from
Mar 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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