|
@@ -1,80 +1,61 @@
|
|
|
package br.eti.mertz.wkhtmltopdf.wrapper;
|
|
|
|
|
|
+import br.eti.mertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
|
|
|
+import br.eti.mertz.wkhtmltopdf.wrapper.configurations.WrapperConfigBuilder;
|
|
|
+import br.eti.mertz.wkhtmltopdf.wrapper.page.Page;
|
|
|
+import br.eti.mertz.wkhtmltopdf.wrapper.page.PageType;
|
|
|
+import br.eti.mertz.wkhtmltopdf.wrapper.params.Param;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
import java.io.*;
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
|
|
|
-import br.eti.mertz.wkhtmltopdf.wrapper.options.GlobalOption;
|
|
|
-import lombok.Data;
|
|
|
-
|
|
|
-@Data
|
|
|
public class Pdf implements PdfService {
|
|
|
|
|
|
- static final String STDOUT = "-";
|
|
|
-
|
|
|
- private String command;
|
|
|
- private List<Param> params;
|
|
|
- private String htmlInput = null;
|
|
|
- private boolean htmlFromString = false;
|
|
|
-
|
|
|
- public Pdf(String wkhtmltopdf, List<Param> params) {
|
|
|
- this.command = wkhtmltopdf;
|
|
|
- this.params = params;
|
|
|
- }
|
|
|
-
|
|
|
- public Pdf(List<Param> params) {
|
|
|
- this("wkhtmltopdf", params);
|
|
|
- }
|
|
|
-
|
|
|
- public Pdf(Param... params) {
|
|
|
- this("wkhtmltopdf", Arrays.asList(params));
|
|
|
- }
|
|
|
-
|
|
|
- public Pdf() {
|
|
|
- this("wkhtmltopdf", new ArrayList<Param>());
|
|
|
- }
|
|
|
-
|
|
|
- public void addHtmlInput(String input) {
|
|
|
- this.htmlFromString = true;
|
|
|
- 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) {
|
|
|
- params.add(param);
|
|
|
- }
|
|
|
-
|
|
|
- public void addParam(Param... params) {
|
|
|
- for (Param param : params) {
|
|
|
- addParam(param);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @throws IOException
|
|
|
- * @throws InterruptedException
|
|
|
- */
|
|
|
- public File saveAs(String path) throws IOException, InterruptedException {
|
|
|
- File file = new File(path);
|
|
|
- getPDF(path);
|
|
|
- return file;
|
|
|
- }
|
|
|
+ private static final String STDINOUT = "-";
|
|
|
+
|
|
|
+ private WrapperConfig wrapperConfig;
|
|
|
+
|
|
|
+ private List<Param> params;
|
|
|
+
|
|
|
+ private List<Page> pages;
|
|
|
+
|
|
|
+ private boolean hasToc = false;
|
|
|
+
|
|
|
+ public Pdf(WrapperConfig wrapperConfig) {
|
|
|
+ this.wrapperConfig = wrapperConfig;
|
|
|
+ this.params = new ArrayList<Param>();
|
|
|
+ this.pages = new ArrayList<Page>();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Pdf() {
|
|
|
+ this(new WrapperConfigBuilder().build());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addPage(String source, PageType type) {
|
|
|
+ this.pages.add(new Page(source, type));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addToc() {
|
|
|
+ this.hasToc = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addParam(Param param) {
|
|
|
+ params.add(param);
|
|
|
+ }
|
|
|
|
|
|
- public File saveAs(String path, byte[] document) throws IOException {
|
|
|
+ public void addParam(Param... params) {
|
|
|
+ for (Param param : params) {
|
|
|
+ addParam(param);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void saveAs(String path) throws IOException, InterruptedException {
|
|
|
+ saveAs(path, getPDF());
|
|
|
+ }
|
|
|
+
|
|
|
+ private File saveAs(String path, byte[] document) throws IOException {
|
|
|
File file = new File(path);
|
|
|
|
|
|
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
|
|
@@ -85,18 +66,19 @@ public class Pdf implements PdfService {
|
|
|
return file;
|
|
|
}
|
|
|
|
|
|
- public byte[] getPDF(String path) throws IOException, InterruptedException {
|
|
|
+ public byte[] getPDF() throws IOException, InterruptedException {
|
|
|
+
|
|
|
Runtime runtime = Runtime.getRuntime();
|
|
|
- if(htmlFromString && !this.params.contains(new Param("-"))) {
|
|
|
- this.addParam(new Param("-"));
|
|
|
- }
|
|
|
- String command = this.commandWithParameters() + Symbol.separator + path;
|
|
|
- Process process = runtime.exec(command);
|
|
|
- if(htmlFromString) {
|
|
|
- OutputStream stdInStream = process.getOutputStream();
|
|
|
- stdInStream.write(htmlInput.getBytes("UTF-8"));
|
|
|
- stdInStream.close();
|
|
|
+ Process process = runtime.exec(getCommandAsArray());
|
|
|
+
|
|
|
+ for (Page page : pages) {
|
|
|
+ if (page.getType().equals(PageType.htmlAsString)) {
|
|
|
+ OutputStream stdInStream = process.getOutputStream();
|
|
|
+ stdInStream.write(page.getSource().getBytes("UTF-8"));
|
|
|
+ stdInStream.close();
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
InputStream stdOutStream = process.getInputStream();
|
|
|
InputStream stdErrStream = process.getErrorStream();
|
|
|
process.waitFor();
|
|
@@ -104,37 +86,52 @@ public class Pdf implements PdfService {
|
|
|
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
|
|
|
ByteArrayOutputStream stdErr = new ByteArrayOutputStream();
|
|
|
|
|
|
- while(stdOutStream.available()>0) {
|
|
|
+ while (stdOutStream.available() > 0) {
|
|
|
stdOut.write((char) stdOutStream.read());
|
|
|
}
|
|
|
stdOutStream.close();
|
|
|
- while(stdErrStream.available()>0) {
|
|
|
+ while (stdErrStream.available() > 0) {
|
|
|
stdErr.write((char) stdErrStream.read());
|
|
|
}
|
|
|
stdErrStream.close();
|
|
|
|
|
|
- if(process.exitValue() != 0) {
|
|
|
- throw new RuntimeException("Process (" + command + ") exited with status code " + process.exitValue() + ":\n"+new String(stdErr.toByteArray()));
|
|
|
+ if (process.exitValue() != 0) {
|
|
|
+ throw new RuntimeException("Process (" + getCommand() + ") exited with status code " + process.exitValue() + ":\n" + new String(stdErr.toByteArray()));
|
|
|
}
|
|
|
|
|
|
return stdOut.toByteArray();
|
|
|
}
|
|
|
|
|
|
- public byte[] getPDF() throws IOException, InterruptedException {
|
|
|
- return getPDF(STDOUT);
|
|
|
- }
|
|
|
+ private String[] getCommandAsArray() {
|
|
|
+ List<String> commandLine = new ArrayList<String>();
|
|
|
+ commandLine.add(wrapperConfig.getWkhtmltopdfCommand());
|
|
|
+
|
|
|
+ if (hasToc)
|
|
|
+ commandLine.add("toc");
|
|
|
|
|
|
- public String commandWithParameters() {
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- for (Param param : params) {
|
|
|
- sb.append(param);
|
|
|
- }
|
|
|
+ for (Param p : params) {
|
|
|
+ commandLine.add(p.getKey());
|
|
|
|
|
|
- return command + sb.toString();
|
|
|
- }
|
|
|
+ String value = p.getValue();
|
|
|
+
|
|
|
+ if (value != null) {
|
|
|
+ commandLine.add(p.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- public void addParam(GlobalOption option) {
|
|
|
- addParam(new Param(option.toString()));
|
|
|
- }
|
|
|
+ for (Page page : pages) {
|
|
|
+ if (page.getType().equals(PageType.htmlAsString)) {
|
|
|
+ commandLine.add(STDINOUT);
|
|
|
+ } else {
|
|
|
+ commandLine.add(page.getSource());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ commandLine.add(STDINOUT);
|
|
|
+ return commandLine.toArray(new String[commandLine.size()]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getCommand() {
|
|
|
+ return StringUtils.join(getCommandAsArray(), " ");
|
|
|
+ }
|
|
|
|
|
|
}
|