Print in Angular Maps component

27 Apr 202417 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 PrintService must be injected into the Maps using providers of the Angular component and set the allowPrint property to true.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { MapsModule } from '@syncfusion/ej2-angular-maps'
import { LegendService, PrintService } from '@syncfusion/ej2-angular-maps'




import { Component, ViewChild } from '@angular/core';
import { world_map } from './world-map';
import { MapsComponent } from '@syncfusion/ej2-angular-maps';

@Component({
imports: [
         MapsModule
    ],

providers: [LegendService, PrintService],
standalone: true,
    selector: 'app-container',
    template:
    `<ejs-maps id='rn-container' #maps [allowPrint]=true [legendSettings] = 'legendSettings'>
    <e-layers>
    <e-layer  [shapeData]= 'shapeData'  [shapePropertyPath]= 'shapePropertyPath' [shapeDataPath]= 'shapeDataPath' [dataSource] = 'dataSource' [shapeSettings] = 'shapeSettings'></e-layer>
    </e-layers>
    </ejs-maps>  <button  id='print' (click)='print()'>Print</button>`
})

export class AppComponent {
    @ViewChild('maps')
    public mapObj?: MapsComponent;
    public dataSource: object[] = [
        {  "Country": "China", "Membership": "Permanent"},
        { "Country": "France", "Membership": "Permanent" },
        { "Country": "Russia", "Membership": "Permanent"},
        { "Country": "Kazakhstan", "Membership": "Non-Permanent"},
        { "Country": "Poland", "Membership": "Non-Permanent"},
        { "Country": "Sweden", "Membership": "Non-Permanent"}];
    public shapeData: object = world_map;
    public shapePropertyPath: string = 'name';
    public shapeDataPath: string= 'Country';
    public shapeSettings: object = {
        colorValuePath: 'Membership',
        colorMapping: [
            {
                value: 'Permanent', color: '#D84444'
            },
            {
                value: 'Non-Permanent', color: '#316DB5'
            }]
        };
    public legendSettings: object = {
        visible: true
    };
    public print() {
        this.mapObj?.print();
    };
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

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. 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
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { MapsModule } from '@syncfusion/ej2-angular-maps'
import { LegendService, ImageExportService } from '@syncfusion/ej2-angular-maps'




import { Component, ViewChild } from '@angular/core';
import { world_map } from './world-map';
import { MapsComponent } from '@syncfusion/ej2-angular-maps';

@Component({
imports: [
         MapsModule
    ],

providers: [LegendService, ImageExportService],
standalone: true,
    selector: 'app-container',
    template:
    `<ejs-maps id='rn-container' #maps [allowImageExport]=true [legendSettings] = 'legendSettings'>
    <e-layers>
    <e-layer  [shapeData]= 'shapeData'  [shapePropertyPath]= 'shapePropertyPath' [shapeDataPath]= 'shapeDataPath' [dataSource] = 'dataSource' [shapeSettings] = 'shapeSettings'></e-layer>
    </e-layers>
    </ejs-maps>  <button  id='print' (click)='export()'>Export</button>`
})

export class AppComponent {
     @ViewChild('maps')
    public mapObj?: MapsComponent;
    public dataSource: object[] = [
        {  "Country": "China", "Membership": "Permanent"},
        { "Country": "France", "Membership": "Permanent" },
        { "Country": "Russia", "Membership": "Permanent"},
        { "Country": "Kazakhstan", "Membership": "Non-Permanent"},
        { "Country": "Poland", "Membership": "Non-Permanent"},
        { "Country": "Sweden", "Membership": "Non-Permanent"}];
    public shapeData: object = world_map;
    public shapePropertyPath: string = 'name';
    public shapeDataPath: string= 'Country';
    public shapeSettings: object = {
        colorValuePath: 'Membership',
        colorMapping: [
            {
                value: 'Permanent', color: '#D84444'
            },
            {
                value: 'Non-Permanent', color: '#316DB5'
            }]
    };
    public legendSettings: object = {
        visible: true
    };
    public export() {
        this.mapObj?.export('JPEG', 'Maps');
    };
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Exporting Maps as base64 string of the file

We can get the image file 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.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { MapsModule } from '@syncfusion/ej2-angular-maps'
import { ImageExportService, LegendService } from '@syncfusion/ej2-angular-maps'




import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import { world_map } from './world-map';
import { MapsComponent } from '@syncfusion/ej2-angular-maps';

@Component({
imports: [
         MapsModule
    ],

providers: [ImageExportService, LegendService],
standalone: true,
    selector: 'app-container',
    template:
    `<ejs-maps id='rn-container' #maps [allowImageExport]=true [legendSettings] = 'legendSettings'>
    <e-layers>
    <e-layer  [shapeData]= 'shapeData'  [shapePropertyPath]= 'shapePropertyPath' [shapeDataPath]= 'shapeDataPath' [dataSource] = 'dataSource' [shapeSettings] = 'shapeSettings'></e-layer>
    </e-layers>
    </ejs-maps>  <button  id='print' (click)='export()'>Export</button> <div id="data"></div>`
})

export class AppComponent {
     @ViewChild('maps')
    public mapObj?: MapsComponent;
    public dataSource: object[] = [
        {  "Country": "China", "Membership": "Permanent"},
        {"Country": "France","Membership": "Permanent" },
        { "Country": "Russia","Membership": "Permanent"},
        {"Country": "Kazakhstan","Membership": "Non-Permanent"},
        { "Country": "Poland","Membership": "Non-Permanent"},
        {"Country": "Sweden","Membership": "Non-Permanent"}];
    public shapeData: object = world_map;
    public shapePropertyPath: string = 'name';
    public shapeDataPath: string= 'Country';
    public shapeSettings: object = {
        colorValuePath: 'Membership',
        colorMapping: [
            {
                value: 'Permanent', color: '#D84444'
            },
            {
                value: 'Non-Permanent', color: '#316DB5'
            }]
    };
    public legendSettings: object = {
        visible: true
    };
    public export() {
        const promise = (this.mapObj as MapsComponent).export('PNG','Maps', undefined , false);
            promise.then((data)=>{
                (document.getElementById('data') as HTMLElement).innerHTML = data;
            })
    };
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

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. The rendered map 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.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { MapsModule } from '@syncfusion/ej2-angular-maps'
import { PdfExportService, LegendService } from '@syncfusion/ej2-angular-maps'




import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import { world_map } from './world-map';
import { MapsComponent } from '@syncfusion/ej2-angular-maps';

@Component({
imports: [
         MapsModule
    ],

providers: [PdfExportService, LegendService],
standalone: true,
    selector: 'app-container',
    template:
    `<ejs-maps id='rn-container' #maps [allowPdfExport]=true [legendSettings] = 'legendSettings'>
    <e-layers>
    <e-layer  [shapeData]= 'shapeData'  [shapePropertyPath]= 'shapePropertyPath' [shapeDataPath]= 'shapeDataPath' [dataSource] = 'dataSource' [shapeSettings] = 'shapeSettings'></e-layer>
    </e-layers>
    </ejs-maps>  <button  id='print' (click)='export()'>Export</button>`
})

export class AppComponent {
    @ViewChild('maps')
    public mapObj?: MapsComponent;
    public dataSource: object[] = [
        {  "Country": "China", "Membership": "Permanent"},
        { "Country": "France", "Membership": "Permanent" },
        { "Country": "Russia", "Membership": "Permanent"},
        { "Country": "Kazakhstan", "Membership": "Non-Permanent"},
        { "Country": "Poland", "Membership": "Non-Permanent"},
        { "Country": "Sweden", "Membership": "Non-Permanent"}];
    public shapeData: object = world_map;
    public shapePropertyPath: string = 'name';
    public shapeDataPath: string= 'Country';
    public shapeSettings: object = {
        colorValuePath: 'Membership',
        colorMapping: [
            {
                value: 'Permanent', color: '#D84444'
            },
            {
                value: 'Non-Permanent', color: '#316DB5'
            }]
        };
    public legendSettings: object = {
        visible: true
    };
    public export() {
        this.mapObj?.export('PDF', 'Maps', 0);
    };
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

The exporting of the map as base64 string is not supported in 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
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { MapsModule } from '@syncfusion/ej2-angular-maps'
import { PdfExportService, ImageExportService, LegendService } from '@syncfusion/ej2-angular-maps'




import { Component, ViewChild } from '@angular/core';
import { MapsComponent } from '@syncfusion/ej2-angular-maps';

@Component({
imports: [
         MapsModule
    ],

providers: [PdfExportService, ImageExportService, LegendService],
standalone: true,
    selector: 'app-container',
    template:
    `<ejs-maps id='rn-container' #maps [allowPdfExport]=true [allowImageExport]=true [titleSettings] = 'titleSettings'>
    <e-layers>
    <e-layer  [urlTemplate]= 'urlTemplate'></e-layer>
    </e-layers>
    </ejs-maps>  <button  id='export' (click)='export()'>Export</button>`
})

export class AppComponent {
    @ViewChild('maps')
    public mapObj?: MapsComponent;
    public urlTemplate = 'https://tile.openstreetmap.org/level/tileX/tileY.png';
    public titleSettings: object = {
        text: 'OSM'
    };
    public export() {
        this.mapObj?.export('JPEG', 'Maps');
    };
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));