I have a webpage where I display a map using openstreetmap's ol3 library and other elements from primefaces:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<ui:define name="title"></ui:define>
<ui:define name="content" style="border-style: none;">
<script type="text/javascript" src="resources/js/ol.js"></script>
<script type="text/javascript" src="resources/js/map.js"></script>
<p:layoutUnit>
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true"/>
<h:form id="mainForm">
<!--content-->
<p:commandButton value="Build PDF" class="buttonFont" process="@all" actionListener="#{bean.createPDF}" ajax="false"/>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="center">
<h:panelGroup layout="block" id="map">
</h:panelGroup>
Script map.js:
var map;
var osmlayer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var position = ol.proj.transform([longitude, latitude], 'EPSG:4326', 'EPSG:3857');
var view = new ol.View({
center : position,
zoom : 12
map = new ol.Map({
target : document.getElementById('map'),
layers : [osmlayer],
controls : [ new ol.control.Zoom(), new ol.control.ScaleLine({
geodesic : true
}), new ol.control.Attribution(), new ol.control.Rotate() ],
view : view
});
I need to get an image(png/jpeg) from what's been displayed on my map and add it to a new PDF generated by itextpdf in the current ManagedBean:
@ManagedBean(name = "bean")
@ViewScoped
public class mapBean{
public void createPDF(){
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.setResponseHeader("Content-Type", "application/pdf");
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"Title.pdf\"");
try{
PdfWriter writer = PdfWriter.getInstance(document,ec.getResponseOutputStream());
document.open();
Image mapPDF = Image.getInstance(/*Set image from map*/);
mapaPDF.scaleToFit(450,200);
document.add(mapaPDF);
document.close();
FacesContext.getCurrentInstance().responseComplete();
}
catch (Exception er) {
// TODO Auto-generated catch block
er.printStackTrace();
}
}
}
You dont need to do it on the server side. You can just do it on the client side.
check this example
You can get the map image out of the canvas using
canvas.toDataURL('image/jpeg');