# leaflet.browser.print **Repository Path**: fishergis/leaflet.browser.print ## Basic Information - **Project Name**: leaflet.browser.print - **Description**: No description available - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-25 - **Last Updated**: 2024-09-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Map Print Plugin for Leaflet.js [](https://www.npmjs.com/package/leaflet.browser.print) ## General information A [leaflet](http://www.leafletjs.com) plugin which allows users to [print full page map](https://igor-vladyka.github.io/leaflet.browser.print/) directly from the browser * Pros: - Compatible with Leaflet v1+. - Any page size from range A0-A10, B0-B10, C0-C10, D0-D10 can se used. - North American paper sizes available as well: Letter, HalfLetter, Legal, JuniorLegal, Tabloid, Ledger - Available 4 print modes, you can chose any you want and even create your own. - Everything in browser, no external apps or dependencies, print your map in one click. - You can even override default browser print behavior and export map as image, more details you can find [here](https://github.com/Igor-Vladyka/leaflet.browser.print#download-map-as-image). - Any additional page content can be printed together with a map. - And many more...just ask! * Cons: - Doesn't change page orientation automatically in IE and FF, due to [The @page rule and forcing Landscape orientation](http://css-discuss.incutio.com/wiki/Print_Stylesheets#The_.40page_rule_and_forcing_Landscape_orientation) ### Other examples: Check out the: * [General example](https://igor-vladyka.github.io/leaflet.browser.print/) * [Localization](https://igor-vladyka.github.io/leaflet.browser.print/examples/localization.html) * [Print objects manipulations](https://igor-vladyka.github.io/leaflet.browser.print/examples/manipulations.html) * [Map legend printing](https://igor-vladyka.github.io/leaflet.browser.print/examples/print-with-legend.html) * [Export map as Image](https://igor-vladyka.github.io/leaflet.browser.print/examples/savePNG.html) ### Be careful when printing map legend Common problem with printing map with a legend is external CSS plugins that ruins everything, [here is an actual good answer why it is like that with Bootstrap plugin](https://stackoverflow.com/questions/33410724/bootstrap-print-css-removes-background-color). Please read it carefully. ### Downloads **NPM** ```` npm install --save leaflet.browser.print ```` **YARN** ```` yarn add leaflet.browser.print ```` ### Usage **Step 1.** Include the required js and css files in your document. ```html ``` **Step 2.** Add the following line of code to your map script ``` js L.control.browserPrint().addTo(map) ``` **Step 3.** You can pass a number of options to the plugin to control various settings. | Option | Type | Default | Description | | ------------- |--------------|--------------|---------------| | title | String | 'Print map' | Sets the text which appears as the tooltip of the print button | | documentTitle | String | '' | Sets the text which appears as the print page title | | position | [Leaflet control position](http://leafletjs.com/reference-1.5.0.html#control-position) | 'topleft' | Position the print button | | printModes | Array | ["Portrait", "Landscape", "Auto", "Custom"] | Collection of page print actions | | printLayer | [Leaflet tile layer](http://leafletjs.com/reference-1.5.0.html#tilelayer) | null | A tiles layer to show instead of all current active tile layers | | closePopupsOnPrint | Boolean | true | Indicates if we need to force popup closing for printed map | | contentSelector | String | "[leaflet-browser-print-content]" | Content selector for printed map, will select and dynamically inject content on printed maps. For full functionality please check "Printing additional content section" | | pagesSelector | String | "[leaflet-browser-print-pages]" | Pages selector for printed map, will select and dynamically inject additional pages content on printed maps. | | manualMode | Boolean | false | Adds a button with id='leaflet-browser-print--manualMode-button' for debugging purpose, also can be used to print map with external button. | | customPrintStyle | [Polyline options](https://leafletjs.com/reference-1.5.0.html#polyline-option)] | { color: "gray", dashArray: "5, 10", pane: "customPrintPane" } | Style for rectangle on custom print. 'customPrintPane' - is a custom pane with z-index => 9999 | Here's an example of passing through some options: ``` js var customActionToPrint = function(context, mode) { return function() { window.alert("We are printing the MAP. Let's do Custom print here!"); context._printCustom(mode); } } 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 Stamen Design, CC BY 3.0 — Map data © OpenStreetMap', subdomains: 'abcd', minZoom: 1, maxZoom: 16, ext: 'png' }), closePopupsOnPrint: false, printModes: [ L.control.browserPrint.mode.landscape("TABLOID VIEW", "tabloid"), L.control.browserPrint.mode("Alert", "User specified print action", "A6", customActionToPrint, false), L.control.browserPrint.mode.landscape(), "Portrait", L.control.browserPrint.mode.auto("Automatico", "B4"), L.control.browserPrint.mode.custom("Séléctionnez la zone", "B5") ], manualMode: false }).addTo(map); ``` ### Print Mode Details | 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 | | Auto | Track all active map layers (markers, lines, polygons, etc. ) and tries to fit them in print page in Portrait or Landscape page dimensions | | Custom | Allows you to select rectangle for printing, and then fit it in Portrait or Landscape page dimensions | General mode and shortcuts: ``` js L.control.browserPrint.mode( /*Mode from table above*/, /*Text to represent mode on button*/, /*Page Size that can basically be in range A0-A10 and B0-B10.*/ /*Custom function that can be executed to print map*/, /*Indicates if we need to force bounds invalidation(true) or just center the map and use current zoom lvl(false)*/ ); L.control.browserPrint.mode.landscape(); L.control.browserPrint.mode.portrait(); L.control.browserPrint.mode.auto(); L.control.browserPrint.mode.custom(); ``` ### Map Events | Map Event | Event Shortcut | Value | Description | Purpose | | ------------------- | --------------------------------------- | -------------------------------------- | --------------------------------------------------------------- | ------- | | browser-print-init | L.Control.BrowserPrint.Event.PrintInit | { mode } | Fire right after printing was initialized. | To support busy indicator of any type to show user loaing status. | | browser-pre-print | L.Control.BrowserPrint.Event.PrePrint | { pageSize, pageBounds, printObjects } | Fire before print started, allows manipulation with map objects.| For DOM manipulation with real map objects.| | browser-print-start | L.Control.BrowserPrint.Event.PrintStart | { printLayer, printMap, printObjects } | Fire on print started, before all print calculations is done. | For DOM manipulation with print map and print objects. | | browser-print | L.Control.BrowserPrint.Event.Print | { printLayer, printMap, printObjects } | Fire right before native print. | For DOM manipulation with print map and print objects. | | browser-print-end | L.Control.BrowserPrint.Event.PrintEnd | { printLayer, printMap, printObjects } | Fire on print end, after we refresh map to show initial view. | For DOM manipulation with real map objects after print | Example can be found here: [DEMO with print objects manipulations](https://igor-vladyka.github.io/leaflet.browser.print/examples/manipulations.html); ### Printing additional content section To add additional printing content (in addition to a map itself) we need to specify content that should be added: [Demo](https://igor-vladyka.github.io/leaflet.browser.print/); By default contentSelector: '[leaflet-browser-print-content]' so we need a content with an 'leaflet-browser-print-content' attribute. Code example: ``` html