Search results

Print and Export in JavaScript Maps control

15 Oct 2021 / 4 minutes to read

Print

The rendered Maps can be printed directly from the browser by calling the print method. To use the print functionality, the Print module must be injected into the Maps using Maps.Inject(Print) method and set the allowPrint property to true.

Source
Preview
index.ts
index.html
Copied to clipboard
import { Maps, MapsTooltip, DataLabel, Print} from '@syncfusion/ej2-maps';
import { usa_map } from '../default-map-cs1/usa.ts';
Maps.Inject(MapsTooltip, DataLabel, Print);
    let maps: Maps = new 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();
    };
Copied to clipboard
<!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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <script src="usa.js"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <div id='container' style="margin-top: 125px">
        <div id='element'></div>
        
        <button id= "print" type="button" width ='15%' style="float: right">Print</button>
    </div>
    
</body>

</html>

Export

Image Export

To use the image export functionality in Maps, ImageExport module must be injected into the Maps using Maps.Inject(ImageExport) method and set the allowImageExport property to true.

Copied to clipboard
import { Maps, ImageExport } from '@syncfusion/ej2-maps';
Maps.Inject(ImageExport);
let map: Maps = new Maps({ });

The rendered Maps can be exported as an image using the export method. The method requires two parameters: image type and file name. The Maps can be exported as an image in the following formats.

  • JPEG
  • PNG
  • SVG
Source
Preview
index.ts
index.html
Copied to clipboard
import { Maps, MapsTooltip, DataLabel, ImageExport} from '@syncfusion/ej2-maps';
import { usa_map } from '../default-map-cs1/usa.ts';
Maps.Inject(MapsTooltip, DataLabel, ImageExport );

let maps: Maps = new 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');
};
Copied to clipboard
<!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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <script src="usa.js"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <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>
</body>

</html>

Exporting Maps as base 64 string of the file

The image can be exported as base64 string for the JPEG and PNG formats. The rendered Maps can be exported to image as a base64 string using the export method. There are four parameters required: image type, file name, orientation of the exported PDF document which must be set as null for image export and finally allowDownload which should be set as false to return base64 string.

Source
Preview
index.ts
index.html
Copied to clipboard
import { Maps, MapsTooltip, DataLabel, ImageExport} from '@syncfusion/ej2-maps';
import { usa_map } from '../default-map-cs1/usa.ts';
Maps.Inject(MapsTooltip, DataLabel, ImageExport);

let maps: Maps = new 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;
    });
};
Copied to clipboard
<!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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <script src="usa.js"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <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>
</body>

</html>

PDF Export

To use the PDF export functionality, PdfExport module must be injected into the Maps using Maps.Inject(PdfExport) method and set the allowPdfExport property to true.

Copied to clipboard
import { Maps, PdfExport } from '@syncfusion/ej2-maps';
Maps.Inject(PdfExport);
let map: Maps = new Maps({ });

The rendered Maps can be exported as 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 and 0 indicates portrait and 1 indicates landscape.

Source
Preview
index.ts
index.html
Copied to clipboard
import { Maps, MapsTooltip, DataLabel, PdfExport} from '@syncfusion/ej2-maps';
import { usa_map } from '../default-map-cs1/usa.ts';
Maps.Inject(MapsTooltip, DataLabel, PdfExport);

let maps: Maps = new 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);
};
Copied to clipboard
<!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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <script src="usa.js"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <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>
</body>

</html>

The exporting of the Maps as base64 string is not supported for the PDF export.

Export the tile Maps

The rendered Maps 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
  • PDF
Source
Preview
index.ts
index.html
Copied to clipboard
import { Maps, Print, ImageExport, PdfExport} from '@syncfusion/ej2-maps';
Maps.Inject(Print, ImageExport, PdfExport);
let maps: Maps = new Maps({
    allowPdfExport: true,
    allowImageExport: true,
    allowPrint: true,
    titleSettings: {
			text: 'OSM'
		},
    layers: [
        {
            layerType: 'OSM'
        }
    ]
});
maps.appendTo('#container');
document.getElementById('export').onclick = () => {
    maps.export('PNG', 'Maps');
};
document.getElementById('print').onclick = () => {
    maps.print();
};
Copied to clipboard
<!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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <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>
</body>

</html>