|
@@ -5,13 +5,12 @@ import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
-import br.eti.mertz.wkhtmltopdf.wrapper.options.GlobalOption;
|
|
|
|
-import lombok.Data;
|
|
|
|
|
|
+import br.eti.mertz.wkhtmltopdf.wrapper.params.Param;
|
|
|
|
+import br.eti.mertz.wkhtmltopdf.wrapper.params.Symbol;
|
|
|
|
|
|
-@Data
|
|
|
|
public class Pdf implements PdfService {
|
|
public class Pdf implements PdfService {
|
|
|
|
|
|
- static final String STDOUT = "-";
|
|
|
|
|
|
+ private static final String STDOUT = "-";
|
|
|
|
|
|
private String command;
|
|
private String command;
|
|
private List<Param> params;
|
|
private List<Param> params;
|
|
@@ -40,20 +39,6 @@ public class Pdf implements PdfService {
|
|
this.htmlInput = input;
|
|
this.htmlInput = input;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * TODO Add a HTML file, a HTML string or a page from a URL
|
|
|
|
- */
|
|
|
|
- public void addCover(String cover) {
|
|
|
|
- // TODO Auto-generated method stub
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * TODO just the TOC option from wkhtmltopdf
|
|
|
|
- */
|
|
|
|
- public void addToc() {
|
|
|
|
- // TODO Auto-generated method stub
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
public void addParam(Param param) {
|
|
public void addParam(Param param) {
|
|
params.add(param);
|
|
params.add(param);
|
|
}
|
|
}
|
|
@@ -68,13 +53,11 @@ public class Pdf implements PdfService {
|
|
* @throws IOException
|
|
* @throws IOException
|
|
* @throws InterruptedException
|
|
* @throws InterruptedException
|
|
*/
|
|
*/
|
|
- public File saveAs(String path) throws IOException, InterruptedException {
|
|
|
|
- File file = new File(path);
|
|
|
|
- getPDF(path);
|
|
|
|
- return file;
|
|
|
|
|
|
+ public void saveAs(String path) throws IOException, InterruptedException {
|
|
|
|
+ saveAs(path, getPDF());
|
|
}
|
|
}
|
|
|
|
|
|
- public File saveAs(String path, byte[] document) throws IOException {
|
|
|
|
|
|
+ private File saveAs(String path, byte[] document) throws IOException {
|
|
File file = new File(path);
|
|
File file = new File(path);
|
|
|
|
|
|
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
|
|
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
|
|
@@ -85,18 +68,26 @@ public class Pdf implements PdfService {
|
|
return file;
|
|
return file;
|
|
}
|
|
}
|
|
|
|
|
|
- public byte[] getPDF(String path) throws IOException, InterruptedException {
|
|
|
|
- Runtime runtime = Runtime.getRuntime();
|
|
|
|
|
|
+ public byte[] getPDF() throws IOException, InterruptedException {
|
|
|
|
+ return getPDF(STDOUT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private byte[] getPDF(String path) throws IOException, InterruptedException {
|
|
|
|
+
|
|
if(htmlFromString && !this.params.contains(new Param("-"))) {
|
|
if(htmlFromString && !this.params.contains(new Param("-"))) {
|
|
this.addParam(new Param("-"));
|
|
this.addParam(new Param("-"));
|
|
}
|
|
}
|
|
- String command = this.commandWithParameters() + Symbol.separator + path;
|
|
|
|
|
|
+ String command = this.commandParameters() + Symbol.separator + path;
|
|
|
|
+
|
|
|
|
+ Runtime runtime = Runtime.getRuntime();
|
|
Process process = runtime.exec(command);
|
|
Process process = runtime.exec(command);
|
|
|
|
+
|
|
if(htmlFromString) {
|
|
if(htmlFromString) {
|
|
OutputStream stdInStream = process.getOutputStream();
|
|
OutputStream stdInStream = process.getOutputStream();
|
|
stdInStream.write(htmlInput.getBytes("UTF-8"));
|
|
stdInStream.write(htmlInput.getBytes("UTF-8"));
|
|
stdInStream.close();
|
|
stdInStream.close();
|
|
}
|
|
}
|
|
|
|
+
|
|
InputStream stdOutStream = process.getInputStream();
|
|
InputStream stdOutStream = process.getInputStream();
|
|
InputStream stdErrStream = process.getErrorStream();
|
|
InputStream stdErrStream = process.getErrorStream();
|
|
process.waitFor();
|
|
process.waitFor();
|
|
@@ -120,11 +111,7 @@ public class Pdf implements PdfService {
|
|
return stdOut.toByteArray();
|
|
return stdOut.toByteArray();
|
|
}
|
|
}
|
|
|
|
|
|
- public byte[] getPDF() throws IOException, InterruptedException {
|
|
|
|
- return getPDF(STDOUT);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public String commandWithParameters() {
|
|
|
|
|
|
+ public String commandParameters() {
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
for (Param param : params) {
|
|
for (Param param : params) {
|
|
sb.append(param);
|
|
sb.append(param);
|
|
@@ -133,8 +120,4 @@ public class Pdf implements PdfService {
|
|
return command + sb.toString();
|
|
return command + sb.toString();
|
|
}
|
|
}
|
|
|
|
|
|
- public void addParam(GlobalOption option) {
|
|
|
|
- addParam(new Param(option.toString()));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
}
|
|
}
|