mxgraph的xml 导出图片
以下2种后台的方法都有一个问题子,就是文字的字体样式不好使;但是如果是简单的使用还是可以的;要想样式好使,我采用的方法是前后台联合,即前台的到base64位的图片url ,然后后台再去处理(先提供一个思路,下回写);
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.imageio.ImageIO;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import com.mxgraph.canvas.mxGraphicsCanvas2D;
import com.mxgraph.canvas.mxICanvas2D;
import com.mxgraph.io.mxCodec;
import com.mxgraph.reader.mxSaxOutputHandler;
import com.mxgraph.util.mxCellRenderer;
import com.mxgraph.util.mxUtils;
import com.mxgraph.util.mxXmlUtils;
import com.mxgraph.view.mxGraph;
@RestController
public class ImageController {
private Logger logger = LoggerFactory.getLogger(getClass());
protected String attachmentPath = "D:"+File.separator+"lyq"+File.separator+"downDoc" ;
@RequestMapping(value = "export", method = RequestMethod.POST)
public void export(String filename, int w, int h, String xml) throws Exception {
String path="";
try {
path = URLDecoder.decode(filename, "UTF-8");
xml = URLDecoder.decode(xml, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String name=path.substring(0, path.lastIndexOf("."));
//将图片保存到本地
try {
logger.info(""+w+" "+h);
logger.info(xml);
logger.info(path);
logger.info(name);
xml="<mxGraphModel dx=\"608\" dy=\"591\" grid=\"1\" gridSize=\"10\" guides=\"1\" tooltips=\"1\" connect=\"1\" arrows=\"1\" fold=\"1\" page=\"1\" pageScale=\"1\" pageWidth=\"827\" pageHeight=\"1169\" background=\"none\">\n"
+ " <root>\n"
+ " <mxCell id=\"0\" />\n"
+ " <mxCell id=\"1\" parent=\"0\" />\n"
+ " <mxCell id=\"36\" value=\"\" style=\"rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFFFF;fontSize=16;\" parent=\"1\" vertex=\"1\">\n"
+ " <mxGeometry x=\"1240\" y=\"50\" width=\"960\" height=\"320\" as=\"geometry\" />\n"
+ " </mxCell>\n"
+ " <mxCell id=\"2\" value=\"\" style=\"rounded=0;w