Search results

Print and Export in React Maps component

02 Feb 2023 / 5 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 Inject services={[]} tag and set the allowPrint property to true.

Copied to clipboard
<MapsComponent >
    <Inject services={[Print]}/>
<MapsComponent>
Source
Preview
index.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, Inject, Print, DataLabel } from '@syncfusion/ej2-react-maps';
export function App() {
    let mapsInstance;
    function clickHandler() {
        mapsInstance.print();
    }
    return (<div>
        <ButtonComponent onClick={clickHandler}>print</ButtonComponent>
            <MapsComponent allowPrint={true} ref={g => mapsInstance = g}>
                <Inject services={[DataLabel, Print]}/>
                <LayersDirective>
                    <LayerDirective shapeData={world_map} dataLabelSettings={{
        visible: true,
        labelPath: 'name',
        smartLabelMode: 'Trim'
    }}>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, Inject, Print, DataLabel } from '@syncfusion/ej2-react-maps';
export function App() {
    let mapsInstance;
    function clickHandler(){
        mapsInstance.print();
    }
    return (<div>
        <ButtonComponent onClick= { clickHandler}>print</ButtonComponent>
            <MapsComponent  allowPrint={true} ref={g => mapsInstance = g}>
                <Inject services={[DataLabel, Print]} />
                <LayersDirective>
                    <LayerDirective shapeData={world_map}
                     dataLabelSettings={ {
                            visible: true,
                            labelPath: 'name',
                            smartLabelMode: 'Trim'
                        } }>
                    </LayerDirective>
                </LayersDirective>
            </MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

Export

Image Export

To use the image export functionality in Maps, ImageExport module must be injected into the Maps using Inject services={[ImageExport]} tag and set the allowImageExport property to true.

Copied to clipboard
<MapsComponent >
    <Inject services={[ImageExport]}/>
<MapsComponent>

The rendered Maps can be exported as an image using the export method. This 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.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
    let mapsInstance;
    function clickHandler() {
        mapsInstance.export('PNG', 'Maps');
    }
    return (<div>
    <ButtonComponent onClick={clickHandler}>Export</ButtonComponent>
        <MapsComponent allowImageExport={true} ref={g => mapsInstance = g}>
            <Inject services={[DataLabel, ImageExport]}/>
            <LayersDirective>
                <LayerDirective shapeData={world_map} dataLabelSettings={{
        visible: true,
        labelPath: 'name',
        smartLabelMode: 'Trim'
    }}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
let mapsInstance;
function clickHandler(){
    mapsInstance.export('PNG', 'Maps');
}
return (<div>
    <ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
        <MapsComponent  allowImageExport={true} ref={g => mapsInstance = g}>
            <Inject services={[DataLabel, ImageExport]} />
            <LayersDirective>
                <LayerDirective shapeData={world_map}
                 dataLabelSettings={ {
                        visible: true,
                        labelPath: 'name',
                        smartLabelMode: 'Trim'
                    } }>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

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.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
    let mapsInstance;
    function clickHandler() {
        mapsInstance.export('PNG', 'Maps', null, false).then((data) => {
            document.getElementById('data').innerHTML = data;
        });
    }
    return (<div>
    <ButtonComponent onClick={clickHandler}>Export</ButtonComponent>
        <MapsComponent allowImageExport={true} ref={g => mapsInstance = g}>
            <Inject services={[DataLabel, ImageExport]}/>
            <LayersDirective>
                <LayerDirective shapeData={world_map} dataLabelSettings={{
        visible: true,
        labelPath: 'name',
        smartLabelMode: 'Trim'
    }}>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent><div id="data"></div></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
let mapsInstance;
function clickHandler(){
    mapsInstance.export('PNG', 'Maps', null, false).then((data)=>{
        document.getElementById('data').innerHTML = data;
    })
}
return (<div>
    <ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
        <MapsComponent  allowImageExport={true} ref={g => mapsInstance = g}>
            <Inject services={[DataLabel, ImageExport]} />
            <LayersDirective>
                <LayerDirective shapeData={world_map}
                 dataLabelSettings={ {
                        visible: true,
                        labelPath: 'name',
                        smartLabelMode: 'Trim'
                    } }>
                </LayerDirective>
            </LayersDirective>
        </MapsComponent><div id="data"></div></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

PDF Export

To use the PDF export functionality, PdfExport module must be injected into the Maps using Inject services={[PdfExport]} method and set the allowPdfExport property to true.

Copied to clipboard
<MapsComponent >
<Inject services={[PdfExport]}/>
<MapsComponent>

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.jsx
index.tsx
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, PdfExport } from '@syncfusion/ej2-react-maps';
export function App() {
    let mapsInstance;
    function clickHandler() {
        mapsInstance.export('PDF', 'Maps');
    }
    return (<div>
    <ButtonComponent onClick={clickHandler}>Export</ButtonComponent>
    <MapsComponent allowPdfExport={true} ref={g => mapsInstance = g}>
        <Inject services={[DataLabel, PdfExport]}/>
        <LayersDirective>
            <LayerDirective shapeData={world_map} dataLabelSettings={{
        visible: true,
        labelPath: 'name',
        smartLabelMode: 'Trim'
    }}>
            </LayerDirective>
        </LayersDirective>
    </MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Copied to clipboard
import { world_map } from 'world-map.ts';
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, DataLabel, Inject, PdfExport } from '@syncfusion/ej2-react-maps';
export function App() {
let mapsInstance;
function clickHandler(){
    mapsInstance.export('PDF', 'Maps');
}
return (<div>
    <ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
    <MapsComponent  allowPdfExport={true} ref={g => mapsInstance = g}>
        <Inject services={[DataLabel, PdfExport]} />
        <LayersDirective>
            <LayerDirective shapeData={world_map}
             dataLabelSettings={ {
                    visible: true,
                    labelPath: 'name',
                    smartLabelMode: 'Trim'
                } }>
            </LayerDirective>
        </LayersDirective>
    </MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);

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.jsx
index.tsx
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
    let mapsInstance;
    function clickHandler() {
        mapsInstance.export('PNG', 'Maps');
    }
    return (<div>
    <ButtonComponent onClick={clickHandler}>Export</ButtonComponent>
    <MapsComponent allowImageExport={true} ref={g => mapsInstance = g} titleSettings={{ text: 'OSM' }}>
        <Inject services={[ImageExport]}/>
        <LayersDirective>
            <LayerDirective urlTemplate="https://tile.openstreetmap.org/level/tileX/tileY.png">
            </LayerDirective>
        </LayersDirective>
    </MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { MapsComponent, LayersDirective, LayerDirective, Inject, ImageExport } from '@syncfusion/ej2-react-maps';
export function App() {
let mapsInstance;
function clickHandler(){
    mapsInstance.export('PNG', 'Maps');
}
return (<div>
    <ButtonComponent onClick= { clickHandler}>Export</ButtonComponent>
    <MapsComponent  allowImageExport={true}
                             ref={g => mapsInstance = g}
                             titleSettings={ { text: 'OSM' } }>
        <Inject services={[ImageExport]} />
        <LayersDirective>
            <LayerDirective urlTemplate="https://tile.openstreetmap.org/level/tileX/tileY.png">
            </LayerDirective>
        </LayersDirective>
    </MapsComponent></div>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);