Created
September 3, 2015 12:15
-
-
Save davidjgraph/770d97d94094b7b50220 to your computer and use it in GitHub Desktop.
Testing draw.io (mxGraph) image export using Java WebView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.beans.value.*; | |
import javafx.concurrent.Worker.State; | |
import javafx.embed.swing.SwingFXUtils; | |
import javafx.scene.Scene; | |
import javafx.scene.image.*; | |
import javafx.scene.web.WebEngine; | |
import javafx.scene.web.WebView; | |
import javafx.stage.Stage; | |
import javax.imageio.ImageIO; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import netscape.javascript.JSObject; | |
public class WebViewExport extends Application { | |
private static final String EXPORT_URL = "http://test.draw.io/exportJava.html"; | |
public WebView webView; | |
private File captureFile = new File("cap.png"); | |
public static void main(String[] args) { Application.launch(WebViewExport.class); } | |
@Override public void start(Stage stage) throws Exception { | |
webView = new WebView(); | |
String xml = "<mxGraphModel dx=\\\"702\\\" dy=\\\"825\\\" grid=\\\"0\\\" gridSize=\\\"10\\\" guides=\\\"1\\\" tooltips=\\\"1\\\" connect=\\\"1\\\" fold=\\\"1\\\" page=\\\"1\\\" pageScale=\\\"1\\\" pageWidth=\\\"826\\\" pageHeight=\\\"1169\\\" background=\\\"#D4E1F5\\\" style=\\\"default-style2\\\" math=\\\"1\\\"><root><mxCell id=\\\"0\\\"/><mxCell id=\\\"1\\\" parent=\\\"0\\\"/><mxCell id=\\\"39\\\" value=\\\"123\\\" style=\\\"ellipse;shape=doubleEllipse;whiteSpace=wrap;html=1;strokeWidth=3;fontStyle=0;fontColor=#1A1A1A;blue\\\" parent=\\\"1\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"214\\\" y=\\\"596\\\" width=\\\"60\\\" height=\\\"60\\\" as=\\\"geometry\\\"/></mxCell><mxCell id=\\\"2\\\" value=\\\"dfgdfgdg\\\" style=\\\"ellipse;shape=doubleEllipse;whiteSpace=wrap;html=1;fontFamily=Helvetica;fontSize=14;fontColor=#4F4F4F;align=center;rotation=0;gray\\\" parent=\\\"1\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"184\\\" y=\\\"66\\\" width=\\\"120\\\" height=\\\"60\\\" as=\\\"geometry\\\"/></mxCell><mxCell id=\\\"4\\\" value=\\\"sdkjf haskdjhf lkdjhf &nbsp;adswvetr fgsdfgsdfg sdfg sdf sd asdf asdfa dasg sdfgsdfg sdfg\\\" style=\\\"rounded=1;html=1;labelWidth=100;whiteSpace=wrap;align=left;verticalAlign=middle;gray;edgeStyle=orthogonalEdgeStyle;entryX=0.67;entryY=0;entryPerimeter=0\\\" parent=\\\"1\\\" source=\\\"2\\\" target=\\\"3\\\" edge=\\\"1\\\"><mxGeometry x=\\\"0.2908\\\" y=\\\"-43\\\" width=\\\"0.7952\\\" height=\\\"2\\\" relative=\\\"1\\\" as=\\\"geometry\\\"><mxPoint as=\\\"offset\\\"/><Array as=\\\"points\\\"><mxPoint x=\\\"504\\\" y=\\\"96\\\"/></Array><mxPoint x=\\\"473\\\" y=\\\"350\\\" as=\\\"targetPoint\\\"/></mxGeometry></mxCell><mxCell id=\\\"36\\\" value=\\\"1..n\\\" style=\\\"text;html=1;resizable=0;align=center;verticalAlign=middle;labelBackgroundColor=#ffffff;gray\\\" parent=\\\"4\\\" connectable=\\\"0\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"-0.5905\\\" y=\\\"-2\\\" relative=\\\"1\\\" as=\\\"geometry\\\"><mxPoint x=\\\"-66\\\" y=\\\"-2\\\" as=\\\"offset\\\"/></mxGeometry></mxCell><mxCell id=\\\"9\\\" value=\\\"This is a<br>edge label\\\" style=\\\"edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;fontSize=12;align=center;verticalAlign=middle;gray\\\" parent=\\\"1\\\" source=\\\"3\\\" target=\\\"2\\\" edge=\\\"1\\\"><mxGeometry x=\\\"0.159\\\" width=\\\"0.8857\\\" height=\\\"2\\\" relative=\\\"1\\\" as=\\\"geometry\\\"><mxPoint as=\\\"offset\\\"/><mxPoint x=\\\"244\\\" y=\\\"126\\\" as=\\\"targetPoint\\\"/><Array as=\\\"points\\\"><mxPoint x=\\\"244\\\" y=\\\"380\\\"/></Array></mxGeometry></mxCell><mxCell id=\\\"3\\\" value=\\\"This is a Label editing test.<br>`a^2 + b^2 = c^2`\\\" style=\\\"whiteSpace=wrap;html=1;rounded=1;curved=0;direction=east;blue;glass=1\\\" parent=\\\"1\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"380\\\" y=\\\"350\\\" width=\\\"185\\\" height=\\\"60\\\" as=\\\"geometry\\\"/></mxCell><mxCell id=\\\"37\\\" value=\\\"<font style="font-size: 10px">asdasd asd asd</font>\\\" style=\\\"shape=tape;whiteSpace=wrap;html=1;shadow=1;fontFamily=Helvetica;fontSize=12;fontColor=#000000;align=center;strokeColor=#B85450;strokeWidth=3;fillColor=#F8CECC;gradientColor=#EA6B66;size=0.28350515463917525\\\" parent=\\\"1\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"446\\\" y=\\\"513\\\" width=\\\"140\\\" height=\\\"97\\\" as=\\\"geometry\\\"/></mxCell><mxCell id=\\\"49\\\" style=\\\"edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;exitX=0;exitY=0.65;exitPerimeter=0\\\" parent=\\\"1\\\" source=\\\"37\\\" target=\\\"39\\\" edge=\\\"1\\\"><mxGeometry relative=\\\"1\\\" as=\\\"geometry\\\"><Array as=\\\"points\\\"><mxPoint x=\\\"399\\\" y=\\\"576\\\"/><mxPoint x=\\\"399\\\" y=\\\"626\\\"/></Array><mxPoint x=\\\"446\\\" y=\\\"561\\\" as=\\\"sourcePoint\\\"/></mxGeometry></mxCell><mxCell id=\\\"55\\\" value=\\\"\\\" style=\\\"shape=hexagon;perimeter=hexagonPerimeter;whiteSpace=wrap;html=1;gray\\\" parent=\\\"1\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"34\\\" y=\\\"66\\\" width=\\\"90\\\" height=\\\"60\\\" as=\\\"geometry\\\"/></mxCell><mxCell id=\\\"56\\\" value=\\\"\\\" style=\\\"edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;\\\" parent=\\\"1\\\" source=\\\"2\\\" target=\\\"55\\\" edge=\\\"1\\\"><mxGeometry relative=\\\"1\\\" as=\\\"geometry\\\"/></mxCell><mxCell id=\\\"57\\\" value=\\\"\\\" style=\\\"shape=hexagon;perimeter=hexagonPerimeter;whiteSpace=wrap;html=1;yellow\\\" parent=\\\"1\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"64\\\" y=\\\"410\\\" width=\\\"90\\\" height=\\\"60\\\" as=\\\"geometry\\\"/></mxCell><mxCell id=\\\"58\\\" style=\\\"edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;endArrow=classic;endFill=1\\\" parent=\\\"1\\\" source=\\\"55\\\" target=\\\"57\\\" edge=\\\"1\\\"><mxGeometry relative=\\\"1\\\" as=\\\"geometry\\\"/></mxCell><mxCell id=\\\"65\\\" style=\\\"edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;exitX=1;exitY=0.5;entryX=0.75;entryY=0;entryPerimeter=0\\\" parent=\\\"1\\\" source=\\\"50\\\" target=\\\"37\\\" edge=\\\"1\\\"><mxGeometry relative=\\\"1\\\" as=\\\"geometry\\\"/></mxCell><mxCell id=\\\"41\\\" value=\\\"test\\\" style=\\\"edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;gray;shadow=0;entryX=1;entryY=0.5;entryPerimeter=0\\\" parent=\\\"1\\\" source=\\\"3\\\" target=\\\"37\\\" edge=\\\"1\\\"><mxGeometry x=\\\"0.3165\\\" width=\\\"0.9317\\\" height=\\\"1\\\" relative=\\\"1\\\" as=\\\"geometry\\\"><mxPoint x=\\\"505\\\" y=\\\"436\\\" as=\\\"sourcePoint\\\"/><mxPoint x=\\\"648\\\" y=\\\"598\\\" as=\\\"targetPoint\\\"/><Array as=\\\"points\\\"><mxPoint x=\\\"627\\\" y=\\\"386\\\"/><mxPoint x=\\\"627\\\" y=\\\"561\\\"/><mxPoint x=\\\"586\\\" y=\\\"561\\\"/></Array><mxPoint as=\\\"offset\\\"/></mxGeometry></mxCell><mxCell id=\\\"42\\\" value=\\\"1..n\\\" style=\\\"text;html=1;resizable=0;align=center;verticalAlign=middle;labelBackgroundColor=#ffffff;fontSize=10;gray\\\" parent=\\\"41\\\" connectable=\\\"0\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"0.9317\\\" y=\\\"1\\\" relative=\\\"1\\\" as=\\\"geometry\\\"><mxPoint x=\\\"34\\\" y=\\\"-13\\\" as=\\\"offset\\\"/></mxGeometry></mxCell><mxCell id=\\\"43\\\" value=\\\"1\\\" style=\\\"text;html=1;resizable=0;align=center;verticalAlign=middle;labelBackgroundColor=#ffffff;fontSize=10;gray;shadow=0;direction=west\\\" parent=\\\"41\\\" connectable=\\\"0\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"-0.8794\\\" y=\\\"-1\\\" relative=\\\"1\\\" as=\\\"geometry\\\"><mxPoint x=\\\"7\\\" y=\\\"8\\\" as=\\\"offset\\\"/></mxGeometry></mxCell><mxCell id=\\\"51\\\" value=\\\"Edge<br>Label\\\" style=\\\"text;html=1;resizable=0;align=center;verticalAlign=middle;labelBackgroundColor=#ffffff;\\\" parent=\\\"41\\\" connectable=\\\"0\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"-0.1176\\\" y=\\\"1\\\" relative=\\\"1\\\" as=\\\"geometry\\\"><mxPoint x=\\\"-1\\\" y=\\\"14\\\" as=\\\"offset\\\"/></mxGeometry></mxCell><mxCell id=\\\"50\\\" value=\\\"ConstraintHandler<br>CurrentFocusArea\\\" style=\\\"shape=trapezoid;whiteSpace=wrap;html=1;blue;size=0.1069182389937107;opacity=50\\\" parent=\\\"1\\\" vertex=\\\"1\\\"><mxGeometry x=\\\"74\\\" y=\\\"437\\\" width=\\\"159\\\" height=\\\"64\\\" as=\\\"geometry\\\"/></mxCell></root></mxGraphModel>"; | |
WebEngine webEngine = webView.getEngine(); | |
String data = "{\"xml\":\"" + xml + "\",\"crop\":true,\"format\":\"png\",\"math\":true}"; | |
JSObject window = (JSObject) webEngine.executeScript("window"); | |
window.setMember("parentServlet", this); | |
webEngine.getLoadWorker().stateProperty().addListener( | |
new ChangeListener<State>() { | |
public void changed(ObservableValue ov, State oldState, State newState) { | |
if (newState == State.SUCCEEDED) { | |
JSObject jsobj = (JSObject)webView.getEngine().executeScript("window"); | |
jsobj.call("render", data); | |
} | |
} | |
}); | |
stage.setScene(new Scene(webView)); | |
stage.show(); | |
webEngine.load(EXPORT_URL); | |
} | |
public void setXml(String xml) | |
{ | |
// Double escape quotes | |
} | |
public void createImageCallback(double width, double height, String bg, String svg) | |
{ | |
webView.setPrefWidth(width); | |
webView.setPrefHeight(height); | |
WritableImage image = webView.snapshot(null, null); | |
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null); | |
try | |
{ | |
ImageIO.write(bufferedImage, "png", captureFile); | |
ImageView imageView = new ImageView(); | |
imageView.setImage(new Image(captureFile.toURI().toURL().toExternalForm())); | |
System.out.println("Captured WebView to: " + captureFile.getAbsoluteFile()); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment