|
@@ -2,6 +2,7 @@ package br.eti.mertz.wkhtmltopdf.wrapper;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
@@ -12,11 +13,13 @@ import lombok.Data;
|
|
|
@Data
|
|
|
public class Pdf implements PdfService {
|
|
|
|
|
|
- private String wkhtmlpdf;
|
|
|
+ private String command;
|
|
|
private List<Param> params;
|
|
|
+ private String htmlInput = null;
|
|
|
+ private boolean htmlFromString = false;
|
|
|
|
|
|
public Pdf(String wkhtmltopdf, List<Param> params) {
|
|
|
- this.wkhtmlpdf = wkhtmltopdf;
|
|
|
+ this.command = wkhtmltopdf;
|
|
|
this.params = params;
|
|
|
}
|
|
|
|
|
@@ -32,11 +35,9 @@ public class Pdf implements PdfService {
|
|
|
this("wkhtmltopdf", new ArrayList<Param>());
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * TODO Add a HTML file, a HTML string or a page from a URL
|
|
|
- */
|
|
|
- public void addPage(String page) {
|
|
|
- // TODO Auto-generated method stub
|
|
|
+ public void addHtmlInput(String input) {
|
|
|
+ this.htmlFromString = true;
|
|
|
+ this.htmlInput = input;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -71,20 +72,29 @@ public class Pdf implements PdfService {
|
|
|
*/
|
|
|
public File saveAs(String path) throws IOException, InterruptedException {
|
|
|
Runtime rt = Runtime.getRuntime();
|
|
|
- Process proc = rt.exec(this.toString() + path);
|
|
|
+ String command = this.commandWithParameters() + Symbol.separator + path;
|
|
|
+ Process proc = rt.exec(command);
|
|
|
+ if(htmlFromString) {
|
|
|
+ OutputStream stdin = proc.getOutputStream();
|
|
|
+ stdin.write(htmlInput.getBytes());
|
|
|
+ stdin.close();
|
|
|
+ }
|
|
|
|
|
|
proc.waitFor();
|
|
|
+ if(proc.exitValue() != 0) {
|
|
|
+ throw new RuntimeException("Process (" + command + ") exited with status code " + proc.exitValue());
|
|
|
+ }
|
|
|
|
|
|
return new File(path);
|
|
|
}
|
|
|
|
|
|
- public String toString() {
|
|
|
+ public String commandWithParameters() {
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
for (Param param : params) {
|
|
|
sb.append(param);
|
|
|
}
|
|
|
|
|
|
- return new StringBuilder(wkhtmlpdf).append(sb.toString()).toString();
|
|
|
+ return command + sb.toString();
|
|
|
}
|
|
|
|
|
|
public void addParam(GlobalOption option) {
|