UNPKG

leaflet.browser.print

Version:

A leaflet plugin which allows users to print the map directly from the browser

68 lines (57 loc) 2.46 kB
<!DOCTYPE html> <html> <head> <title>Leaflet browser print plugin | Save as PNG</title> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://unpkg.com/leaflet@latest/dist/leaflet.css"/> <script src="https://unpkg.com/leaflet@latest/dist/leaflet.js"></script> <script src="../dist/leaflet.browser.print.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/dom-to-image/2.6.0/dom-to-image.js"></script> <script type="text/javascript" src="data/us-states.js"></script> <style> html, body, #map { width: 100%; height: 100%; margin: 0px; padding: 0px;} </style> <style> .info { padding: 6px 8px; font: 14px/16px Arial, Helvetica, sans-serif; background: white; background: rgba(255,255,255,0.8); box-shadow: 0 0 15px rgba(0,0,0,0.2); border-radius: 5px; } .info h4 { margin: 0 0 5px; color: #777; } .legend { text-align: left; line-height: 18px; color: #555; } .legend > * { display: flex; margin-bottom: 5px; } .legend i { width: 18px; height: 18px; float: left; margin-right: 8px; opacity: 0.7; } </style> </head> <body> <div id='map'></div> <script type="text/javascript"> var map = L.map("map").setView([37.8, -96], 4); </script> <script type="text/javascript" src="savePNGMap.js"></script> <script type="text/javascript"> L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' }).addTo(map); var saveAsImage = function () { return domtoimage.toPng(document.body) .then(function (dataUrl) { var link = document.createElement('a'); link.download = map.printControl.options.documentTitle || "exportedMap" + '.png'; link.href = dataUrl; link.click(); }); } L.control.browserPrint({ documentTitle: "printImage", printModes: [ L.BrowserPrint.Mode.Auto("Download PNG"), ], printFunction: saveAsImage }).addTo(map); map.on("browser-print-start", function(e){ /*on print start we already have a print map and we can create new control and add it to the print map to be able to print custom information */ L.legendControl({position: 'bottomright'}).addTo(e.printMap); }); </script> </body> </html>