title: How to export and image from a map and insert it into a new PDF file?
body: <p>I have a webpage where I display a map using openstreetmap's ol3 library and other elements from primefaces:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
      &lt;ui:define name="title"&gt;&lt;/ui:define&gt;
      &lt;ui:define name="content" style="border-style: none;"&gt;
      &lt;script type="text/javascript" src="resources/js/ol.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="resources/js/map.js"&gt;&lt;/script&gt;
      &lt;p:layoutUnit&gt;
        &lt;p:messages id="messages" showDetail="true" autoUpdate="true" closable="true"/&gt;
        &lt;h:form id="mainForm"&gt;
          &lt;!--content--&gt;
          &lt;p:commandButton value="Build PDF" class="buttonFont" process="@all" actionListener="#{bean.createPDF}"     ajax="false"/&gt;
        &lt;/h:form&gt;  
      &lt;/p:layoutUnit&gt;
       &lt;p:layoutUnit position="center"&gt;
       &lt;h:panelGroup  layout="block" id="map"&gt;
       &lt;/h:panelGroup&gt;  
  </code></pre>
  
  <p>Script map.js:</p>
  
  <pre><code>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
              }); 
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>@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();
          }
      }
  }
  </code></pre>