--- /dev/null
+package org.simantics.document;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+import java.net.URL;\r
+import java.net.URLDecoder;\r
+import java.nio.charset.Charset;\r
+import java.nio.file.Files;\r
+\r
+import org.eclipse.core.runtime.FileLocator;\r
+import org.eclipse.core.runtime.IPath;\r
+import org.eclipse.core.runtime.Path;\r
+import org.osgi.framework.BundleContext;\r
+import org.simantics.Simantics;\r
+\r
+public class PhantomJSDriver {\r
+\r
+ public static File fileRef(String fileName) throws IOException {\r
+ \r
+ BundleContext context = Activator.getContext();\r
+ IPath path = new Path("/" + fileName);\r
+ URL libURL = FileLocator.find(context.getBundle(), path, null);\r
+ URL fileURL = FileLocator.toFileURL(libURL);\r
+ return new File(URLDecoder.decode(fileURL.getPath(), "UTF-8"));\r
+ \r
+ }\r
+ \r
+ public static String printCommand(String margin, String url, String outFile) throws IOException {\r
+\r
+ try {\r
+ File f = fileRef("print.js");\r
+ String template = new String(Files.readAllBytes(f.toPath()));\r
+ template = template.replace("%%url", url);\r
+ template = template.replace("%%margin", margin);\r
+ template = template.replace("%%file", outFile.replace("\\", "/"));\r
+ return template;\r
+ } catch (IOException e) {\r
+ return null;\r
+ }\r
+ \r
+ }\r
+ \r
+ public static void print(String html, DocumentSettings settings, File output) throws IOException {\r
+ \r
+ File htmlFile = Simantics.getTempfile("PhantomJSDriver", "html");\r
+ Files.write(htmlFile.toPath(), html.getBytes(Charset.forName("UTF-8")));\r
+ \r
+ String browserUrl = htmlFile.toURI().toURL().toString();\r
+ \r
+ File script = Simantics.getTempfile("PhantomJSDriver", "script");\r
+ \r
+ String margin = "{left:\"" + settings.marginLeft + "mm\", right:\"" + settings.marginRight + "mm\", top:\"" + settings.marginTop + "mm\", bottom:\"" + settings.marginBottom + "mm\"}";\r
+ \r
+ String printCommand = PhantomJSDriver.printCommand(margin, browserUrl, output.getAbsolutePath());\r
+ \r
+ Files.write(script.toPath(), printCommand.getBytes());\r
+ PhantomJSDriver.execute(script);\r
+\r
+ }\r
+ \r
+ public static boolean execute(File javascript) {\r
+ \r
+ try {\r
+ \r
+ File filePath = fileRef("phantomjs.exe");\r
+ \r
+ String[] args = { filePath.getAbsolutePath(),\r
+ javascript.getAbsolutePath() \r
+ };\r
+ Process process = new ProcessBuilder(args).start();\r
+ try {\r
+ InputStream input = process.getInputStream();\r
+ InputStreamReader reader = new InputStreamReader(input);\r
+ //StringBuilder sb = new StringBuilder();\r
+ while (true) {\r
+ try {\r
+ while (reader.ready()) {\r
+ int r = reader.read();\r
+ }\r
+ \r
+ int error = process.exitValue();\r
+ \r
+ reader.close();\r
+ return error == 0;\r
+ \r
+ } catch (IllegalThreadStateException e) {\r
+ Thread.sleep(100);\r
+ }\r
+ }\r
+ } finally {\r
+ process.destroy();\r
+ }\r
+ } catch(IOException e) {\r
+ e.printStackTrace();\r
+ } catch (InterruptedException e) {\r
+ e.printStackTrace();\r
+ }\r
+ return false;\r
+\r
+ }\r
+ \r
+}\r