all files / maps/model/ export-pdf.js

100% Statements 92/92
89.86% Branches 62/69
100% Functions 11/11
100% Lines 92/92
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145     12× 12×   12×             12× 12× 12× 12× 12× 12× 12×       10×       12×   12× 12× 12× 12×               42× 42× 42× 42×   42× 42× 42× 42× 42× 30× 30×   42× 40×           32×   40× 40×             42×             33× 32× 32×     42×       12×   3430×        
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-pdf-export"], function (require, exports, ej2_base_1, ej2_pdf_export_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var PdfExport = (function () {
        function PdfExport() {
        }
        PdfExport.prototype.export = function (maps, type, fileName, allowDownload, orientation) {
            var promise = new Promise(function (resolve) {
                if (maps.isTileMap) {
                    maps.isExportInitialTileMap = true;
                }
                var canvasElement = ej2_base_1.createElement('canvas', {
                    id: 'ej2-canvas',
                    attrs: {
                        'width': maps.availableSize.width.toString(),
                        'height': maps.availableSize.height.toString()
                    }
                });
                orientation = ej2_base_1.isNullOrUndefined(orientation) ? ej2_pdf_export_1.PdfPageOrientation.Landscape : orientation;
                var svgParent = document.getElementById(maps.element.id + '_Tile_SVG_Parent');
                var svgData;
                var exportElement = maps.svgObject.cloneNode(true);
                var backgroundElement = exportElement.childNodes[0];
                var backgroundColor = backgroundElement.getAttribute('fill');
                if ((maps.theme === 'Tailwind' || maps.theme === 'Bootstrap5' || maps.theme === 'Fluent' || maps.theme === 'Material3' ||
                    maps.theme === 'Fluent2')
                    && (backgroundColor === 'rgba(255,255,255, 0.0)' || backgroundColor === 'transparent')) {
                    exportElement.childNodes[0].setAttribute('fill', 'rgba(255,255,255, 1)');
                }
                else if ((maps.theme === 'TailwindDark' || maps.theme === 'Bootstrap5Dark' || maps.theme === 'FluentDark' || maps.theme === 'Material3Dark' ||
                    maps.theme === 'Fluent2Dark' || maps.theme === 'Fluent2HighContrast')
                    && (backgroundColor === 'rgba(255,255,255, 0.0)' || backgroundColor === 'transparent')) {
                    exportElement.childNodes[0].setAttribute('fill', 'rgba(0, 0, 0, 1)');
                }
                var url = window.URL.createObjectURL(new Blob(type === 'SVG' ? [svgData] :
                    [(new XMLSerializer()).serializeToString(exportElement)], { type: 'image/svg+xml' }));
                var pdfDocument = new ej2_pdf_export_1.PdfDocument();
                var image = new Image();
                var ctx = canvasElement.getContext('2d');
                if (!maps.isTileMap) {
                    image.onload = (function () {
                        ctx.drawImage(image, 0, 0);
                        window.URL.revokeObjectURL(url);
                        Eif (type === 'PDF') {
                            var imageString = canvasElement.toDataURL('image/jpeg').replace('image/jpeg', 'image/octet-stream');
                            pdfDocument.pageSettings.orientation = orientation;
                            imageString = imageString.slice(imageString.indexOf(',') + 1);
                            pdfDocument.pages.add().graphics.drawImage(new ej2_pdf_export_1.PdfBitmap(imageString), 0, 0, (maps.availableSize.width - 60), maps.availableSize.height);
                            if (allowDownload) {
                                pdfDocument.save(fileName + '.pdf');
                                pdfDocument.destroy();
                            }
                            else {
                                resolve(null);
                            }
                        }
                    });
                    image.src = url;
                }
                else {
                    var svgParentElement = document.getElementById(maps.element.id + '_MapAreaBorder');
                    var top_1 = parseFloat(svgParentElement.getAttribute('y'));
                    var left_1 = parseFloat(svgParentElement.getAttribute('x'));
                    var xHttp = new XMLHttpRequest();
                    var tileLength_1 = maps.mapLayerPanel.tiles.length;
                    var _loop_1 = function (i) {
                        var tile = document.getElementById(maps.element.id + '_tile_' + (i - 1));
                        var tileImg = new Image();
                        tileImg.crossOrigin = 'Anonymous';
                        var background = maps.background ? maps.background : ((maps.theme === 'Tailwind' || maps.theme === 'Bootstrap5' || maps.theme === 'Fluent' || maps.theme === 'Material3') && (backgroundColor === 'rgba(255,255,255, 0.0)' || backgroundColor === 'transparent')) ? '#ffffff' :
                            (maps.theme === 'TailwindDark' || maps.theme === 'Bootstrap5Dark' || maps.theme === 'FluentDark' || maps.theme === 'Material3Dark') && (backgroundColor === 'rgba(255,255,255, 0.0)' || backgroundColor === 'transparent') ? '#000000' : '#ffffff';
                        ctx.fillStyle = background;
                        ctx.fillRect(0, 0, maps.availableSize.width, maps.availableSize.height);
                        ctx.font = maps.titleSettings.textStyle.size + ' Arial';
                        var titleElement = document.getElementById(maps.element.id + '_Map_title');
                        if (!ej2_base_1.isNullOrUndefined(titleElement)) {
                            ctx.fillStyle = titleElement.getAttribute('fill');
                            ctx.fillText(maps.titleSettings.text, parseFloat(titleElement.getAttribute('x')), parseFloat(titleElement.getAttribute('y')));
                        }
                        tileImg.onload = (function () {
                            if (i === 0 || i === tileLength_1 + 1) {
                                if (i === 0) {
                                    ctx.setTransform(1, 0, 0, 1, 0, 0);
                                    ctx.rect(0, top_1, parseFloat(svgParent.style.width), parseFloat(svgParent.style.height));
                                    ctx.clip();
                                }
                                else {
                                    ctx.setTransform(1, 0, 0, 1, left_1, top_1);
                                }
                            }
                            else {
                                ctx.setTransform(1, 0, 0, 1, parseFloat(tile.style.left) + left_1, parseFloat(tile.style.top) + top_1);
                            }
                            ctx.drawImage(tileImg, 0, 0);
                            if (i === tileLength_1 + 1) {
                                if (type === 'PDF') {
                                    localStorage.setItem('saved-image-example', canvasElement.toDataURL('image/jpeg'));
                                    var x = localStorage.getItem('saved-image-example');
                                    pdfDocument.pageSettings.orientation = orientation;
                                    x = x.slice(x.indexOf(',') + 1);
                                    pdfDocument.pages.add().graphics.drawImage(new ej2_pdf_export_1.PdfBitmap(x), 0, 0, (maps.availableSize.width - 60), maps.availableSize.height);
                                    maps.isExportInitialTileMap = false;
                                    if (allowDownload) {
                                        pdfDocument.save(fileName + '.pdf');
                                        pdfDocument.destroy();
                                    }
                                    else {
                                        resolve(null);
                                    }
                                }
                            }
                        });
                        if (i === 0 || i === tileLength_1 + 1) {
                            if (i === 0) {
                                tileImg.src = url;
                            }
                            else {
                                setTimeout(function () {
                                    var tileSvg = document.getElementById(maps.element.id + '_Tile_SVG');
                                    tileImg.src = window.URL.createObjectURL(new Blob([(new XMLSerializer()).serializeToString(tileSvg)], { type: 'image/svg+xml' }));
                                }, 300);
                            }
                        }
                        else {
                            xHttp.open('GET', tile.children[0].getAttribute('src'), true);
                            xHttp.send();
                            tileImg.src = tile.children[0].getAttribute('src');
                        }
                    };
                    for (var i = 0; i <= tileLength_1 + 1; i++) {
                        _loop_1(i);
                    }
                }
            });
            return promise;
        };
        PdfExport.prototype.getModuleName = function () {
            return 'PdfExport';
        };
        PdfExport.prototype.destroy = function () { };
        return PdfExport;
    }());
    exports.PdfExport = PdfExport;
});