fixing print

This commit is contained in:
Pablo Luna 2011-04-06 19:05:47 +01:00
parent d2b8c3fd7c
commit 002dba3094
2 changed files with 43 additions and 25 deletions

View File

@ -18,6 +18,7 @@
package com.wisemapping.controller; package com.wisemapping.controller;
import com.wisemapping.exporter.ExportException;
import com.wisemapping.exporter.ExportFormat; import com.wisemapping.exporter.ExportFormat;
import com.wisemapping.exporter.ExporterFactory; import com.wisemapping.exporter.ExporterFactory;
import com.wisemapping.model.MindMap; import com.wisemapping.model.MindMap;
@ -29,13 +30,20 @@ import org.apache.batik.transcoder.TranscoderException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException; import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import org.xml.sax.SAXException;
import sun.misc.BASE64Encoder;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerException;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
public class ExportController extends BaseMultiActionController { public class ExportController extends BaseMultiActionController {
private static final String IMG_EXPORT_FORMAT = "IMG_EXPORT_FORMAT"; private static final String IMG_EXPORT_FORMAT = "IMG_EXPORT_FORMAT";
@ -137,9 +145,20 @@ public class ExportController extends BaseMultiActionController {
final MindMap mindMap = service.getMindmapById(mindmapId); final MindMap mindMap = service.getMindmapById(mindmapId);
final String mapSvg = request.getParameter(MAP_SVG_PARAMETER); final String mapSvg = request.getParameter(MAP_SVG_PARAMETER);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
exportImage(response, mapSvg, bos, false);
} catch (Throwable e) {
logger.error("Unexpexted error generating the image", e);
}
BASE64Encoder encoder = new BASE64Encoder();
String content = encoder.encode(bos.toByteArray());
final String exportContent = "data:image/png;base64,"+content;
bos.close();
ModelAndView view = new ModelAndView("mindmapPrint", "mindmap", mindMap); ModelAndView view = new ModelAndView("mindmapPrint", "mindmap", mindMap);
view.addObject(MAP_SVG_PARAMETER, mapSvg); view.addObject(MAP_SVG_PARAMETER, exportContent);
return view; return view;
} }
@ -150,8 +169,18 @@ public class ExportController extends BaseMultiActionController {
final String mapIdStr = request.getParameter(MAP_ID_PARAMETER); final String mapIdStr = request.getParameter(MAP_ID_PARAMETER);
final String mapSvg = request.getParameter(MAP_SVG_PARAMETER); final String mapSvg = request.getParameter(MAP_SVG_PARAMETER);
final ServletOutputStream outputStream = response.getOutputStream();
final MindmapService service = getMindmapService(); exportImage(response, mapSvg, outputStream, true);
} catch (Throwable e) {
logger.error("Unexpexted error generating the image", e);
}
return null;
}
private void exportImage(HttpServletResponse response, String mapSvg, OutputStream outputStream, boolean setOutput) throws TranscoderException, IOException, ParserConfigurationException, SAXException, XMLStreamException, TransformerException, JAXBException, ExportException {
//Image Format //Image Format
ExportFormat imageFormat = ExportFormat.PNG; ExportFormat imageFormat = ExportFormat.PNG;
@ -164,16 +193,10 @@ public class ExportController extends BaseMultiActionController {
setBaseBaseImgUrl(imageFormat, imageProperties); setBaseBaseImgUrl(imageFormat, imageProperties);
// Set format content type... // Set format content type...
if(setOutput)
response.setContentType(imageFormat.getContentType()); response.setContentType(imageFormat.getContentType());
// Write content ... // Write content ...
final ServletOutputStream outputStream = response.getOutputStream();
ExporterFactory.export(imageProperties, null, outputStream, mapSvg); ExporterFactory.export(imageProperties, null, outputStream, mapSvg);
} catch (Throwable e) {
logger.error("Unexpexted error generating the image", e);
}
return null;
} }
} }

View File

@ -9,11 +9,6 @@
%> %>
<!DOCTYPE HTML PUBLIC> <!DOCTYPE HTML PUBLIC>
<%@ include file="/jsp/init.jsp" %> <%@ include file="/jsp/init.jsp" %>
<c:url value="export.htm" var="exportUrl">
<c:param name="action" value="image"/>
<c:param name="mapId" value="${mindmap.id}"/>
<c:param name="mapSvg" value="${mapSvg}"/>
</c:url>
<html> <html>
<head> <head>
<title> <title>
@ -27,7 +22,7 @@
<div id="headerTitle">${mindmap.title}<span id="headerSubTitle">&nbsp;(<%=todayString%>)</span></div> <div id="headerTitle">${mindmap.title}<span id="headerSubTitle">&nbsp;(<%=todayString%>)</span></div>
</div> </div>
<center> <center>
<img src="${exportUrl}" alt="${mindmap.title}"/> <img src="${mapSvg}" alt="${mindmap.title}"/>
</center> </center>
</body> </body>
</html> </html>