Coder Social home page Coder Social logo

rajaswathi-ag / leaflet.browser.print Goto Github PK

View Code? Open in Web Editor NEW

This project forked from igor-vladyka/leaflet.browser.print

0.0 0.0 0.0 286 KB

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

License: MIT License

HTML 21.58% JavaScript 78.42%

leaflet.browser.print's Introduction

Map Print Plugin for Leaflet.js

npm version

General information

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

  • Pros:

    • Compatible with Leaflet v0.7.7 and 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.
    • Any additional page content can be printed together with a map.
    • And many more...just ask!
  • Cons:

Other examples:

Check out the:

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. 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.

	<script src="dist/leaflet.browser.print.min.js"></script>

Step 2. Add the following line of code to your map script

	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 'topleft' Position the print button
printModes Array ["Portrait", "Landscape", "Auto", "Custom"] Collection of page print actions
printLayer Leaflet tile layer 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.

Here's an example of passing through some options:

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 <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
					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:

	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();

Custom Print Mode Configurations

	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.mode("Alert", "User specified print action", "A6", customActionToPrint, false),

Map Events

Map Event Value Description Purpose
browser-pre-print { pageSize, pageBounds, printObjects } Fire before print started, allows manipulation with map objects. For DOM manipulation with real map objects.
browser-print-start { printLayer, printMap, printObjects } Fire on print started, before all print calculations is done. For DOM manipulation with print map and print objects.
browser-print { printLayer, printMap, printObjects } Fire right before native print. For DOM manipulation with print map and print objects.
browser-print-end { 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;

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; By default contentSelector: '[leaflet-browser-print-content]' so we need a content with an 'leaflet-browser-print-content' attribute.

Code example:

<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.

We are using CSS-GRID to position all controls on a print page.
Therefor it's not supportable in all browsers, for more information please visit [caniuse.com](https://caniuse.com/#feat=css-grid).

Angular 2+

See chapter 4 of https://github.com/Asymmetrik/ngx-leaflet-tutorial-plugins/tree/master/Leaflet.BrowserPrint

New print layers/renderers registration

To add missing print layers you need to explicitly indicate layer, it's identifier and construction function that will return actual layer object.

Example of WMS registration:

L.Control.BrowserPrint.Utils.registerLayer(
	L.TileLayer.WMS,
	"L.TileLayer.WMS",
	function(layer, utils) {
		// We need to clone options to properly handle multiple renderers.
		return L.tileLayer.wms(layer._url, utils.cloneOptions(layer.options));
	}
);

List of pre-registered layers available for printing:

  • L.MarkerClusterGroup
  • L.TileLayer.WMS
  • L.TileLayer
  • L.ImageOverlay
  • L.Marker
  • L.Popup
  • L.Circle
  • L.CircleMarker
  • L.Rectangle
  • L.Polygon
  • L.MultiPolyline
  • L.MultiPolygon
  • L.Polyline
  • L.GeoJSON
  • L.FeatureGroup
  • L.LayerGroup
  • L.Tooltip

Example of renderer registration:

L.Control.BrowserPrint.Utils.registerRenderer(L.SVG, 'L.SVG');

List of registered renderers

  • L.SVG
  • L.Canvas

If you want to override any of those, please register your own builder for them.

Download Map as Image

To download map as PNG you have to use external plugin to do the job. Current plugin will do only 1 job - prepare map for printing. To print actual map we use in-browser print mechanism:

window.print()

You can override it to support any other behavior as you want. Example with domtoimage plugin to export map as image.png:

window.print = 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();
				});
};

Full example you can find here.

Custom print button action

Example of how you can use default button or style/specify your own button to call actual print logic. First of all you need to create print plugin with at least one print option to be able to attach it to the map, later you can use any other, even dynamically created print mode with your custom print button.

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://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
                    	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.control.browserPrint.mode.auto();
		map.printControl.print(modeToUse);
	});

And add next css to hide onmap menu:

	.leaflet-control-browser-print {display: none;}

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.

Thanks to Jan Pieter Waagmeester for an idea that helped with map print functionality.

Also thanks to IcoMoon for the print icon.

leaflet.browser.print's People

Contributors

igor-vladyka avatar sgrillon14 avatar beedyg avatar julien-noblet avatar appleshowc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.