{"id":207,"date":"2024-02-25T17:44:27","date_gmt":"2024-02-25T17:44:27","guid":{"rendered":"https:\/\/www.acugis.com\/gis-tutorials\/?page_id=207"},"modified":"2024-02-25T17:48:04","modified_gmt":"2024-02-25T17:48:04","slug":"leaflet-print","status":"publish","type":"page","link":"https:\/\/www.acugis.com\/gis-tutorials\/web-mapping-with-leafletjs\/leaflet-print\/","title":{"rendered":"Leaflet Broswer Print"},"content":{"rendered":"\n<p>In this tutorial, we&#8217;ll add Print and PDF functionality to our leaflet map using the Leaflet Browser Print plugin.<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/Igor-Vladyka\/leaflet.browser.print\">https:\/\/github.com\/Igor-Vladyka\/leaflet.browser.print<\/a><\/p>\n\n\n\n<p>To use Leaflet Browser Print plugin, download the leaflet.browser.print.min.js file and place it in your document root.<\/p>\n\n\n\n<p>Let&#8217;s start with our <a href=\"https:\/\/www.acugis.com\/gis-tutorials\/web-mapping-with-leafletjs\/basic-leaflet-demo\/\" data-type=\"page\" data-id=\"171\">basic Leaflet map<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n    &lt;head>\n    &lt;base target=\"_top\">\n    &lt;meta charset=\"utf-8\">\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n    \n    &lt;title>Basic Leaflet Map&lt;\/title>\n    \n    &lt;!-- Leafletjs and CSS -->    \n    &lt;script src=\"https:\/\/unpkg.com\/leaflet@1.9.4\/dist\/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2\/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\">&lt;\/script>\n    \n        &lt;link rel=\"stylesheet\" href=\"https:\/\/unpkg.com\/leaflet@1.9.4\/dist\/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD\/miZyoHS5obTRR9BMY=\" crossorigin=\"\"\/>\n\n    &lt;!-- Inline CSS for map -->\n    \n    &lt;style>\n        html, body {\n            height: 100%;\n            margin: 0;\n        }\n        .leaflet-container {\n            height: 80%;\n            width: 80%;\n            max-width: 100%;\n            max-height: 100%;\n        }\n    &lt;\/style>\n\n    \n    &lt;\/head>\n    \n    &lt;body>\n\n    &lt;div id=\"map\">&lt;\/div>\n    \n    &lt;script>\n        \n    \/\/ Add OpenStreetMap basemap\n        var map = L.map('map').setView([37.0902, -95.7129], 4);\n\n        var osm = L.tileLayer('https:\/\/tile.openstreetmap.org\/{z}\/{x}\/{y}.png', {\n            maxZoom: 19,\n            attribution: '&amp;copy; &lt;a href=\"http:\/\/www.openstreetmap.org\/copyright\">OpenStreetMap&lt;\/a>'\n        }).addTo(map);\n\n    \n    &lt;\/script>\n\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<p>Add the following to the &lt;head> section of your html file.:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script src=\"leaflet.browser.print.min.js\">&lt;\/script>\n<\/pre>\n\n\n\n<p>Add the following block of JavaScript to your html page.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">L.control.browserPrint({\n\ttitle: 'Just print me!',\n\tdocumentTitle: 'My Leaflet Map',\n\tprintLayer: L.tileLayer('https:\/\/tile.openstreetmap.org\/{z}\/{x}\/{y}.png', {\n\t\t\t\t\tattribution: 'Map tiles by &lt;a href=\"http:\/\/openstreetmap.com\">OpenStreetMap&lt;\/a>',\n\t\t\t\t\tsubdomains: 'abcd',\n\t\t\t\t\tminZoom: 1,\n\t\t\t\t\tmaxZoom: 16,\n\t\t\t\t\text: 'png'\n\t\t\t\t}),\n\tclosePopupsOnPrint: false,\n\tprintModes: [\n            L.BrowserPrint.Mode.Landscape(),\n            \"Portrait\",\n            L.BrowserPrint.Mode.Auto(\"B4\",{title: \"Auto\"}),\n            L.BrowserPrint.Mode.Custom(\"B5\",{title:\"Select area\"})\n\t],\n\tmanualMode: false\n}).addTo(map);\n<\/pre>\n\n\n\n<p>With our JavaScript in place, our html code now looks like below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n    &lt;head>\n    &lt;base target=\"_top\">\n    &lt;meta charset=\"utf-8\">\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n    \n    &lt;title>Basic Leaflet Map&lt;\/title>\n    \n    &lt;!-- Leafletjs and CSS -->    \n    &lt;script src=\"https:\/\/unpkg.com\/leaflet@1.9.4\/dist\/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2\/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\">&lt;\/script>\n    \n    &lt;link rel=\"stylesheet\" href=\"https:\/\/unpkg.com\/leaflet@1.9.4\/dist\/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD\/miZyoHS5obTRR9BMY=\" crossorigin=\"\"\/>\n\t\t\n\t&lt;!-- Leaflet Print JavaScript -->\t\n\t&lt;script src=\"leaflet.browser.print.min.js\">&lt;\/script>\n\n    &lt;!-- Inline CSS for map -->\n    \n    &lt;style>\n        html, body {\n            height: 100%;\n            margin: 0;\n        }\n        .leaflet-container {\n            height: 80%;\n            width: 80%;\n            max-width: 100%;\n            max-height: 100%;\n        }\n    &lt;\/style>\n\n    \n    &lt;\/head>\n    \n    &lt;body>\n\n    &lt;div id=\"map\">&lt;\/div>\n    \n    &lt;script>\n        \n    \/\/ Add OpenStreetMap basemap\n        var map = L.map('map').setView([37.0902, -95.7129], 4);\n\n        var osm = L.tileLayer('https:\/\/tile.openstreetmap.org\/{z}\/{x}\/{y}.png', {\n            maxZoom: 19,\n            attribution: '&amp;copy; &lt;a href=\"http:\/\/www.openstreetmap.org\/copyright\">OpenStreetMap&lt;\/a>'\n        }).addTo(map);\n\n\t\tL.control.browserPrint({\n\t\t\ttitle: 'Just print me!',\n\t\t\tdocumentTitle: 'My Leaflet Map',\n\t\t\tprintLayer: L.tileLayer('https:\/\/tile.openstreetmap.org\/{z}\/{x}\/{y}.png', {\n\t\t\t\t\tattribution: 'Map tiles by &lt;a href=\"http:\/\/openstreetmap.com\">OpenStreetMap&lt;\/a>',\n\t\t\t\t\tsubdomains: 'abcd',\n\t\t\t\t\tminZoom: 1,\n\t\t\t\t\tmaxZoom: 16,\n\t\t\t\t\text: 'png'\n\t\t\t\t}),\n\t\tclosePopupsOnPrint: false,\n\t\tprintModes: [\n            \tL.BrowserPrint.Mode.Landscape(),\n            \t\"Portrait\",\n            \tL.BrowserPrint.Mode.Auto(\"B4\",{title: \"Auto\"}),\n            \tL.BrowserPrint.Mode.Custom(\"B5\",{title:\"Select area\"})\n\t\t\t],\n\t\t\tmanualMode: false\n\t\t}).addTo(map);\n\n    \n    &lt;\/script>\n\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<p>Now, when we view our map, we see the Print controls:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"505\" src=\"https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/print-1024x505.png\" alt=\"\" class=\"wp-image-208\" srcset=\"https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/print-1024x505.png 1024w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/print-300x148.png 300w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/print-768x379.png 768w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/print.png 1148w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p>When generated, you are given the option to Print or save as PDF<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"529\" src=\"https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/print-pdf-1024x529.png\" alt=\"\" class=\"wp-image-213\" srcset=\"https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/print-pdf-1024x529.png 1024w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/print-pdf-300x155.png 300w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/print-pdf-768x397.png 768w, https:\/\/www.acugis.com\/gis-tutorials\/wp-content\/uploads\/2024\/02\/print-pdf.png 1298w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we&#8217;ll add Print and PDF functionality to our leaflet map using the Leaflet Browser Print plugin. https:\/\/github.com\/Igor-Vladyka\/leaflet.browser.print To use Leaflet Browser Print plugin, download the leaflet.browser.print.min.js file and place it in your document root. Let&#8217;s start with our basic Leaflet map Add the following to the &lt;head> section of your html file.: &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.acugis.com\/gis-tutorials\/web-mapping-with-leafletjs\/leaflet-print\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Leaflet Broswer Print&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":166,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-207","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":2,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/207\/revisions"}],"predecessor-version":[{"id":214,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/207\/revisions\/214"}],"up":[{"embeddable":true,"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/pages\/166"}],"wp:attachment":[{"href":"https:\/\/www.acugis.com\/gis-tutorials\/wp-json\/wp\/v2\/media?parent=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}