Преглед изворни кода

Implement passing html string instead of from URI
Add command return code checking

Patrick Brückner пре 10 година
родитељ
комит
a9ac816c4e

+ 1 - 1
pom.xml

@@ -3,7 +3,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>br.eti.mertz</groupId>
 	<artifactId>java-wkhtmltopdf-wrapper</artifactId>
-	<version>0.0.1</version>
+	<version>0.0.2-SNAPSHOT</version>
 
 	<dependencies>
 		<dependency>

+ 3 - 3
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/Command.java

@@ -4,11 +4,11 @@ package br.eti.mertz.wkhtmltopdf.wrapper;
 public class Command {
 	
 	public static void main(String[] args) {
+
 		Pdf pdf = new Pdf();
-		
-		pdf.addParam(new Param("enable-javascript"), new Param("html-header", "file:///lala.html"));
-		
+		pdf.addParam(new Param("--enable-javascript"), new Param("--html-header", "file:///lala.html"));
 		System.out.println(pdf);
+
 	}
 
 }

+ 20 - 10
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/Pdf.java

@@ -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) {

+ 4 - 4
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/PdfService.java

@@ -5,12 +5,12 @@ import java.io.IOException;
 
 public interface PdfService {
 	
-	public void addPage(String page);
+	void addHtmlInput(String page);
 	
-	public void addCover(String cover);
+	void addCover(String cover);
 	
-	public void addToc();
+	void addToc();
 	
-	public File saveAs(String path) throws IOException, InterruptedException;
+	File saveAs(String path) throws IOException, InterruptedException;
 
 }

+ 2 - 2
src/main/java/br/eti/mertz/wkhtmltopdf/wrapper/Symbol.java

@@ -2,11 +2,11 @@ package br.eti.mertz.wkhtmltopdf.wrapper;
 
 public enum Symbol {
 
-	separator(" "), param("--");
+	separator(" "), param("");
 
 	private String symbol;
 
-	private Symbol(String symbol) {
+	Symbol(String symbol) {
 		this.symbol = symbol;
 	}