2014-04-27 42 views
8

我是LeafletJS中的新人。我正嘗試在LeafletJS中打印地圖。我打印的代碼是這樣的:打印地圖在LeafletJS

 printProvider = L.print.provider({ 
     capabilities: printConfig, 
     method: 'GET', 
     dpi: 254, 
     autoLoad: true, 
     // outputFormat: 'pdf', 
     customParams: { 
      mapTitle: 'Print Test', 
      comment: 'Testing Leaflet printing' 
     } 
    }); 
    // Create a print control with the configured provider and add to the map 
    printControl = L.control.print({ 
     provider: printProvider 
    }); 
    map.addControl(printControl); 

但是當我點擊打印按鈕錯誤就這樣產生了

Proxy Error. 
The proxy server received an invalid response from an upstream server. 
The proxy server could not handle the request GET /mapfish-print/pdf/print.pdf. 

Reason: Error reading from remote server 

誰能幫助我?

回答

6

您可以使用jQuery Plugin打印地圖。

的代碼是一樣簡單

$('#map').print(); 

這裏是codepen

1

您是否已將此行添加到您的html?

<script src="http://apps2.geosmart.co.nz/mapfish-print/pdf/info.json?var=printConfig"></script> 

另外我認爲你必須啓用CORS

1

我想你已經錯過了將一些主機添加到mapfish打印服務器中config.yaml中允許的主機列表中。

如果你想使您可以添加所有主機:

- !ipMatch 
    host:0.0.0.0 
    mask:0.0.0.0 
0
//Printing 
function printMyMap(){ 
    let myMapHTML= document.getElementById("myMap"); 
    let mywindow = window.open("", "PrintTheMap","width=600,height=800"); 

let header = '<html><head><link rel="stylesheet" href="/your path/Site.css" media="print" /> <link rel="stylesheet" href="/your path/leaflet.css" /> </head> 
//Adding the header to the window 
mywindow.document.write(header); 
//Adding the map into the body 
mywindow.document.write("<body>"+myMapHTML+"<body>"); 
mywindow.document.close(); // necessary for IE >= 10 
mywindow.focus(); // necessary for IE >= 10 
mywindow.print(); 
mywindow.close(); 

}