如何从地图导出和图像并将其插入到新的PDF文件中?

时间:2016-05-03 20:54:06

标签: javascript jsf-2 openstreetmap openlayers-3 itextpdf

我有一个网页,我使用openstreetmap的ol3库和来自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>  

脚本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
            }); 

我需要从地图上显示的图像中获取图像(png / jpeg),并将其添加到当前ManagedBean中由itextpdf生成的新PDF中:

@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();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您不需要在服务器端执行此操作。 你可以在客户端做到这一点。

检查此example

您可以使用画布获取地图图像 canvas.toDataURL('image/jpeg');

答案 1 :(得分:0)

在javascript中,我创建了一个函数来获取地图的画布并将其插入h:inputHidden

&#13;
&#13;
function imagePDF(){
			canvas = document.getElementsByTagName('canvas')[0];
			var imagen = canvas.toDataURL('image/png');
			document.getElementById('mainForm:imagen').value = imagen;
		}
&#13;
&#13;
&#13;

我在p:commandButton

中调用此函数

&#13;
&#13;
<?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">
        <h:inputHidden id="imagen" value="#{reporteController.imagen}" />
        <!--content-->
        <p:commandButton value="Build PDF" class="buttonFont" process="@all,mainForm:imagen" onclick="imagePDF();" actionListener="#{bean.createPDF}" ajax="false"/>
      </h:form>  
    </p:layoutUnit>
     <p:layoutUnit position="center">
     <h:panelGroup  layout="block" id="map">
     </h:panelGroup>  
&#13;
&#13;
&#13;

在ManagedBean中我退出了h:inputHidden的内容并继续进行画布的解码过程并得到一个字节数组,我可以将其插入到PDF中:

@ManagedBean(name = "bean")
@ViewScoped
public class mapBean{

    public String getImagen() {
        return imagen;
    }

    public void setImagen(String imagen) {
        this.imagen = imagen;
    }

    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();
           BASE64Decoder decoder = new BASE64Decoder();
            byte[] decodedBytes = decoder.decodeBuffer(getImagen().split("^data:image/(png|jpg);base64,")[1]);
           Image mapPDF = Image.getInstance(decodedBytes);
           mapaPDF.scaleToFit(450,200);
           document.add(mapaPDF);
           document.close();
           FacesContext.getCurrentInstance().responseComplete();
        }
        catch (Exception er) {
        // TODO Auto-generated catch block
        er.printStackTrace();
        }
    }
}