I will explain the case of the request initiated by the client, where the diagram is displayed in the browser when the request is executed. This is a standard case, mxGraph passes the XML representation of the graph using custom graphics primitives, and they are received on the server and decoded using either Java or .NET vertices.
The reason for the need to display a graph is the presence of certain textual dimensions, which are difficult to recreate outside the browser environment.
On the client side, you need to create the required immediate XML using, say, the diagrameditor.html example as a guide:
var exportImage = function(editor) { var graph = editor.graph; var scale = graph.view.scale; var bounds = graph.getGraphBounds(); // New image export var xmlDoc = mxUtils.createXmlDocument(); var root = xmlDoc.createElement('output'); xmlDoc.appendChild(root); // Renders graph. Offset will be multiplied with state scale when painting state. var xmlCanvas = new mxXmlCanvas2D(root); xmlCanvas.translate(Math.floor(1 / scale - bounds.x), Math.floor(1 / scale - bounds.y)); xmlCanvas.scale(scale); var imgExport = new mxImageExport(); imgExport.drawState(graph.getView().getState(graph.model.root), xmlCanvas); // Puts request data together var w = Math.ceil(bounds.width * scale + 2); var h = Math.ceil(bounds.height * scale + 2); var xml = mxUtils.getXml(root); // Requests image if request is valid if (w > 0 && h > 0) { var name = 'export.png'; var format = 'png'; var bg = '&bg=#FFFFFF'; new mxXmlRequest(editor.urlImage, 'filename=' + name + '&format=' + format + bg + '&w=' + w + '&h=' + h + '&xml=' + encodeURIComponent(xml)). simulate(document, '_blank'); } };
Where editor.urlImage is the URL of the servlet that generates the image, in the case of back-end Java.
On the server side, in the case of Java, look at java / examples / com / mxgraph / examples / web / ExportServlet.java. This looks at the "format" parameter, and if "pdf", the writePdf() method is writePdf() .
This method creates a PdfWriter and displays graphic primitives in Java Swing Graphics2D using the privileged part of mxGraph Java.
This example writes the PDF result directly to the servlet output stream on this line:
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
You can match the output to any stream.
Note that you need to configure iText to display each font in the PDF. This is not always ideal for a large number of fonts. It is worth checking a few export cases to make sure the output is good enough for your requirements. We are currently studying the use of PhantomJS for export. If Java export is not good enough, submit another question regarding the use of PhantomJS, and I will talk about this in detail.
iText is provided as an example of a PDF library to use, it is easier since it is in an open source library. Perhaps this is not the most suitable library, it was not easy for us to work with this specific scenario. You can also explore other libraries for generating Java PDF files.
Also note that back-end.NET only supports bitmap generation in dotnet / aspnet / Export.ashx, there is no known open source PDF library to give as an example.