GitHub - Igor-Vladyka - Leaflet - Browser.print - A Leaflet Plugin Which Allows Users To Print The Map Directly From The Browser
GitHub - Igor-Vladyka - Leaflet - Browser.print - A Leaflet Plugin Which Allows Users To Print The Map Directly From The Browser
yka/leaflet.browser.print: A leaflet plugin which allows users to print the map directly from the browser
A leaflet plugin which allows users to print the map directly from the browser
igor-vladyka.github.io/leaflet.browser.print/
MIT license
Star Notifications
jude and Igor-Vladyka Issue #125: Adding main to package.json d8052cb · last year
dist Added proper cleanup for map after print. 2 years ago
src Added proper cleanup for map after print. 2 years ago
General information
A leaflet plugin which allows users to print full page map directly from the browser
Pros:
Cons:
Doesn't change page orientation automatically in IE and FF, due to The @page rule and forcing Landscape orientation
https://github.com/Igor-Vladyka/leaflet.browser.print 1/9
23/07/2024, 15:39 GitHub - Igor-Vladyka/leaflet.browser.print: A leaflet plugin which allows users to print the map directly from the browser
Changelog
Other examples:
Check out the:
General example
Localization
Print objects manipulations
Map legend printing
Export map as Image
Downloads
NPM
YARN
SCRIPT
<script src="dist/leaflet.browser.print.min.js"></script>
Usage
Add a Control
Leaflet control
position 'topleft' Position the print button
position
Also the options for the [backend](#Use it in the Backend) can passed through.
To use the same BrowserPrint class in the backend and in the control you can pass it while creating the control:
https://github.com/Igor-Vladyka/leaflet.browser.print 2/9
23/07/2024, 15:39 GitHub - Igor-Vladyka/leaflet.browser.print: A leaflet plugin which allows users to print the map directly from the browser
L.control.browserPrint({
title: 'Just print me!',
documentTitle: 'Map printed using leaflet.browser.print plugin',
printLayer: L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http
subdomains: 'abcd',
minZoom: 1,
maxZoom: 16,
ext: 'png'
}),
closePopupsOnPrint: false,
printModes: [
L.BrowserPrint.Mode.Landscape("Tabloid",{title: "Tabloid VIEW"}),
L.browserPrint.mode("Alert",{title:"User specified print action",pageSize: "A6", action: customActionToPrint, invali
L.BrowserPrint.Mode.Landscape(),
"Portrait",
L.BrowserPrint.Mode.Auto("B4",{title: "Auto"}),
L.BrowserPrint.Mode.Custom("B5",{title:"Select area"})
],
manualMode: false
}).addTo(map);
browserControl.cancel();
browserPrint.print(L.BrowserPrint.Mode.Landscape());
browserPrint.cancel();
documentTitle String '' Sets the text which appears as the print page title
Leaflet tile
printLayer null A tiles layer to show instead of all current active tile layers
layer
closePopupsOnPrint Boolean true Indicates if we need to force popup closing for printed map
https://github.com/Igor-Vladyka/leaflet.browser.print 3/9
23/07/2024, 15:39 GitHub - Igor-Vladyka/leaflet.browser.print: A leaflet plugin which allows users to print the map directly from the browser
debug Boolean false Stops opening the print window. Only for developing use.
Here's an example of passing through some options:
var options = {
documentTitle: 'Map printed using leaflet.browser.print plugin',
printLayer: L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.or
subdomains: 'abcd',
minZoom: 1,
maxZoom: 16,
ext: 'png'
}),
closePopupsOnPrint: false,
manualMode: false
};
var browserPrint = L.browserPrint(map, options);
Print Modes
Mode Description
Portrait Print currently visual part of the map with Portrait page dimensions
Landscape Print currently visual part of the map with Landscape page dimensions
Track all active map layers (markers, lines, polygons, etc. ) and tries to fit them in print page in Portrait or Landscape page
Auto
dimensions
Custom Allows you to select rectangle for printing, and then fit it in Portrait or Landscape page dimensions if it is not explicit set
General modes:
L.BrowserPrint.Mode.Landscape();
L.BrowserPrint.Mode.Portrait();
L.BrowserPrint.Mode.Auto();
L.BrowserPrint.Mode.Custom();
For each general mode the page size and options can be passed. The default page size is A4
L.BrowserPrint.Mode.Landscape(pageSize,options);
Mode name or
orientation String calculation (Auto & How the page will be displayed landscape or portrait
Custom)
enableZoom Boolean true Zoom the map optimal, else the current zoom is taken. zoom have to be 0
action Function default mode action [Custom print button action](#Custom print button action)
https://github.com/Igor-Vladyka/leaflet.browser.print 4/9
23/07/2024, 15:39 GitHub - Igor-Vladyka/leaflet.browser.print: A leaflet plugin which allows users to print the map directly from the browser
The default margin are (height + width) / 39.9 . A number can passed for all
margin Object automatic margins or set it explicit in a object for left , right , top , bottom --> {left:
10, top: 40}
scale Double 1 Scale the map. Shows all bigger or smaller, with 1 the map looks normal
Adds a header section to the top of the page. For the available options look in
header Object
the "Header / Footer Options" table below
Adds a footer section to the bottom of the page. For the available options look in
footer Object
the "Header / Footer Options" table below
You can overwrite the CSS or change the DOM over the ids #print-header and #print-footer
mode.setOptions(options);
Map Events
{ printLayer,
browser- Fire on print started, before For DOM manipulation with
L.BrowserPrint.Event.PrintStart printMap,
print-start all print calculations is done. print map and print objects.
printObjects }
{ printLayer,
browser- For DOM manipulation with
L.BrowserPrint.Event.Print printMap, Fire right before native print.
print print map and print objects.
printObjects }
Code example:
https://github.com/Igor-Vladyka/leaflet.browser.print 5/9
23/07/2024, 15:39 GitHub - Igor-Vladyka/leaflet.browser.print: A leaflet plugin which allows users to print the map directly from the browser
<style leaflet-browser-print-content>
.grid-print-container { // grid holder that holds all content (map and any other content)
grid-template: auto 1fr auto / 1fr;
background-color: orange;
}
.grid-map-print { // map container itself
grid-row: 2;
}
.title { // Dynamic title styling
grid-row: 1;
justify-self: center;
color: white;
}
.sub-content { // Dynamic sub content styling
grid-row: 5;
padding-left: 10px;
}
</style>
<h1 class="title" leaflet-browser-print-content>Leaflet Browser print TITLE</h1>
<h3 class="sub-content" leaflet-browser-print-content>Leaflet Browser print SUB TITLE text</h3>
On print, plugin will scan DOM by contentSelector, and will add content to print may.
Angular 2+
L.BrowserPrint.Utils.registerLayer(
// Actual typeof object to compare with
L.MarkerClusterGroup,
// Any string you would like for current function for print events
'L.MarkerClusterGroup',
function (layer, utils) {
// We need to recreate cluster object with available options
// Here we use function, but we can use object aswell,
// example: new L.MarkerClusterGroup(layer._group.options);
var cluster = L.markerClusterGroup(layer._group.options);
return cluster;
});
L.TileLayer.WMS
L.TileLayer
L.ImageOverlay
L.Marker
L.Popup
L.Circle
L.CircleMarker
L.Rectangle
L.Polygon
L.MultiPolyline
L.MultiPolygon
https://github.com/Igor-Vladyka/leaflet.browser.print 6/9
23/07/2024, 15:39 GitHub - Igor-Vladyka/leaflet.browser.print: A leaflet plugin which allows users to print the map directly from the browser
L.Polyline
L.GeoJSON
L.FeatureGroup
L.LayerGroup
L.Tooltip
L.BrowserPrint.Utils.registerRenderer(L.SVG, 'L.SVG');
L.SVG
L.Canvas
If you want to override any of those, please register your own builder for them.
If you are facing OutOfMemory problem printing huge amount of objects you may consider next workaround:
// markerClusterGroup to print
var printableObjects = L.markerClusterGroup();
// We are not cloning markercluster to preserve original clasterization behavior and prevent OutOfMemory problems
// This way we will need to invalidate MarkerClusterGroup after printing
L.BrowserPrint.Utils.registerLayer(L.MarkerClusterGroup,
'L.MarkerClusterGroup',
function (layer, utils) {
return layer;
});
window.print()
You can use the options printFunction to implement any other behavior that you want. Example with domtoimage plugin to export map as
image.png:
L.control.browserPrint({
documentTitle: "printImage",
printModes: [
L.BrowserPrint.Mode.Auto("Download PNG"),
],
printFunction: saveAsImage
}).addTo(map);
https://github.com/Igor-Vladyka/leaflet.browser.print 7/9
23/07/2024, 15:39 GitHub - Igor-Vladyka/leaflet.browser.print: A leaflet plugin which allows users to print the map directly from the browser
Example:
L.control.browserPrint({
printLayer: L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommon
subdomains: 'abcd',
minZoom: 1,
maxZoom: 16,
ext: 'png'
}),
printModes: [ "Landscape" ],
manualMode: true // use true if it's debug and/or default button is okay for you, otherwise false.
}).addTo(map);
document.querySelector("#custom_print_button").addEventListener("click", function(){
var modeToUse = L.BrowserPrint.Mode.Auto();
map.printControl.print(modeToUse);
});
Important notes
Unfortunately 'Custom' mode is not working correctly for Leaflet v.0.7.7 in all IE browsers.
Acknowledgements
Thanks to Rowan Winsemius for general idea with a map print functionality.
Releases 7
+ 6 releases
patreon.com/igorvladyka
Packages
No packages published
Used by 135
+ 127
Contributors 8
Languages
https://github.com/Igor-Vladyka/leaflet.browser.print 8/9
23/07/2024, 15:39 GitHub - Igor-Vladyka/leaflet.browser.print: A leaflet plugin which allows users to print the map directly from the browser
https://github.com/Igor-Vladyka/leaflet.browser.print 9/9