Print in JavaScript Maps component
18 Nov 201817 minutes to read
The rendered map can be printed directly from the browser by calling the print method. To use the print functionality in Maps, set the allowPrint property must be set to true.
var maps = new ej.maps.Maps({
allowPrint: true,
layers: [
{
dataLabelSettings: {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
},
shapeData: usa_map,
shapeSettings: {
autofill: true
},
tooltipSettings: {
visible: true,
valuePath: 'name'
},
}
]
});
maps.appendTo('#element');
document.getElementById('print').onclick = () => {
maps.print();
};<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Maps</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="usa.js"></script>
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="margin-top: 125px">
<div id="element"></div>
<button id="print" type="button" width="15%" style="float: right">Print</button>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Export
Image Export
To use the image export functionality, set the allowImageExport property to true. The rendered Maps can be exported as an image using the export method. The method requires two parameters: image type and file name. The map can be exported as an image in the following formats.
- JPEG
- PNG
- SVG
var maps = new ej.maps.Maps({
allowImageExport: true,
layers: [
{
dataLabelSettings: {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
},
shapeData: usa_map,
shapeSettings: {
autofill: true
},
tooltipSettings: {
visible: true,
valuePath: 'name'
},
}
]
});
maps.appendTo('#element');
document.getElementById('export').onclick = () => {
maps.export('PNG', 'Maps');
};<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Maps</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="usa.js"></script>
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="margin-top: 125px">
<div id="element"></div>
<button id="export" type="button" width="15%" style="float: right">Export</button>
<div id="data"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Exporting Maps as base 64 string of the file
The map can be exported as a base64 string for the JPEG and PNG formats. The rendered map can be exported as a base64 string using the export method. This method requires four parameters: image type, file name, orientation (set to null for image export), and allowDownload (set to false to return the base64 string).
var maps = new ej.maps.Maps({
allowImageExport: true,
layers: [
{
dataLabelSettings: {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
},
shapeData: usa_map,
shapeSettings: {
autofill: true
},
tooltipSettings: {
visible: true,
valuePath: 'name'
}
}
]
});
maps.appendTo('#element');
document.getElementById('export').onclick = () => {
maps.export('JPEG', 'Maps', null, false).then((data) => {
document.getElementById('data').innerHTML = data;
});
};<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Maps</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="usa.js"></script>
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="margin-top: 125px">
<div id="element"></div>
<button id="export" type="button" width="15%" style="float: right">Export</button>
<div id="data"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>PDF Export
The rendered map can be exported as a PDF using the export method. The export method requires three parameters: file type, file name, and orientation of the PDF document. The orientation setting is optional, where 0 indicates portrait orientation and 1 indicates landscape orientation.
var maps = new ej.maps.Maps({
allowPdfExport: true,
layers: [
{
dataLabelSettings: {
visible: true,
labelPath: 'name',
smartLabelMode: 'Trim'
},
shapeData: usa_map,
shapeSettings: {
autofill: true
},
tooltipSettings: {
visible: true,
valuePath: 'name'
}
}
]
});
maps.appendTo('#element');
document.getElementById('export').onclick = () => {
maps.export('PDF', 'Maps', 0);
};<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Maps</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="usa.js"></script>
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="margin-top: 125px">
<div id="element"></div>
<button id="export" type="button" width="15%" style="float: right">Export</button>
<div id="data"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>The exporting of the Maps as base64 string is not supported for the PDF export.
Export the tile Maps
The rendered map with providers such as OSM, Bing and Google static Maps can be exported using the export method. It supports the following export formats.
- JPEG
- PNG
var maps = new ej.maps.Maps({
allowImageExport: true,
allowPdfExport: true,
allowPrint: true,
titleSettings:{
text:'OSM'
},
layers: [
{
urlTemplate:"https://tile.openstreetmap.org/level/tileX/tileY.png"
}
]
});
maps.appendTo('#container');
document.getElementById('export').onclick = () => {
maps.export('PNG', 'Maps');
};
document.getElementById('print').onclick = () => {
maps.print();
};<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Maps</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container" style="width:600px;height:500px">
</div>
<button id="print" type="button">Print</button><br><br>
<button id="export" type="button">Export</button>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>