all files / pivotview/actions/ pdf-export-helper.js

100% Statements 204/204
97.78% Branches 176/180
100% Functions 18/18
100% Lines 204/204
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310     17× 17× 17× 17× 17× 17×       23× 41× 41× 32×   13×   12× 11×             11×   41×     40× 10×                   30×     29×   28×           12× 12× 11× 11×   11×   11× 11× 11× 11× 11×   11× 11×                                                         21×     21×   21× 21×   21×   21×   21×   21×       12× 12×   11×   20×                     16×   15×           10×     103×   99×             360×   359× 359× 359× 359× 359× 359×        
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 PDFExportHelper = (function () {
        function PDFExportHelper() {
        }
        PDFExportHelper.prototype.drawHeader = function (pdfExportProperties, pdfDocument) {
            var clientSize = pdfDocument.pageSettings.size;
            var header = pdfExportProperties.header;
            var position = new ej2_pdf_export_1.PointF(0, (header && header.fromTop) ? header.fromTop : 0);
            var size = new ej2_pdf_export_1.SizeF((clientSize.width - 80), ((header && header.height) ? header.height * 0.75 : 50));
            var bounds = new ej2_pdf_export_1.RectangleF(position, size);
            pdfDocument.template.top = this.drawPageTemplate(new ej2_pdf_export_1.PdfPageTemplateElement(bounds), header);
        };
        PDFExportHelper.prototype.drawFooter = function (pdfExportProperties, pdfDocument) {
            var clientSize = pdfDocument.pageSettings.size;
            var footer = pdfExportProperties.footer;
            var position = new ej2_pdf_export_1.PointF(0, ((clientSize.width - 80) - ((footer && footer.fromBottom) ?
                footer.fromBottom * 0.75 : 0)));
            var size = new ej2_pdf_export_1.SizeF((clientSize.width - 80), ((footer && footer.height) ? footer.height * 0.75 : 50));
            var bounds = new ej2_pdf_export_1.RectangleF(position, size);
            pdfDocument.template.bottom = this.drawPageTemplate(new ej2_pdf_export_1.PdfPageTemplateElement(bounds), footer);
        };
        PDFExportHelper.prototype.drawPageTemplate = function (template, element) {
            for (var _i = 0, _a = element.contents; _i < _a.length; _i++) {
                var content = _a[_i];
                this.processContentValidation(content);
                switch (content.type) {
                    case 'Text':
                        if (content.value === '' || ej2_base_1.isNullOrUndefined(content.value) || typeof content.value !== 'string') {
                            throw new Error('please enter the valid input value in text content...');
                        }
                        this.drawText(template, content);
                        break;
                    case 'PageNumber':
                        this.drawPageNumber(template, content);
                        break;
                    case 'Image':
                        if (ej2_base_1.isNullOrUndefined(content.src) || content.src === '') {
                            throw new Error('please enter the valid base64 string in image content...');
                        }
                        this.drawImage(template, content);
                        break;
                    case 'Line':
                        this.drawLine(template, content);
                        break;
                }
            }
            return template;
        };
        PDFExportHelper.prototype.processContentValidation = function (content) {
            if (ej2_base_1.isNullOrUndefined(content.type)) {
                throw new Error('please set valid content type...');
            }
            else {
                if (content.type === 'Line') {
                    if (ej2_base_1.isNullOrUndefined(content.points)) {
                        throw new Error('please enter valid points in ' + content.type + ' content...');
                    }
                    else {
                        if (ej2_base_1.isNullOrUndefined(content.points.x1) || typeof content.points.x1 !== 'number') {
                            throw new Error('please enter valid x1 co-ordinate in ' + content.type + ' points...');
                        }
                        if (ej2_base_1.isNullOrUndefined(content.points.y1) || typeof content.points.y1 !== 'number') {
                            throw new Error('please enter valid y1 co-ordinate in ' + content.type + ' points...');
                        }
                        if (ej2_base_1.isNullOrUndefined(content.points.x2) || typeof content.points.x2 !== 'number') {
                            throw new Error('please enter valid x2 co-ordinate in ' + content.type + ' points...');
                        }
                        if (ej2_base_1.isNullOrUndefined(content.points.y2) || typeof content.points.y2 !== 'number') {
                            throw new Error('please enter valid y2 co-ordinate in ' + content.type + ' points...');
                        }
                    }
                }
                else {
                    if (ej2_base_1.isNullOrUndefined(content.position)) {
                        throw new Error('please enter valid position in ' + content.type + ' content...');
                    }
                    else {
                        if (ej2_base_1.isNullOrUndefined(content.position.x) || typeof content.position.x !== 'number') {
                            throw new Error('please enter valid x co-ordinate in ' + content.type + ' position...');
                        }
                        if (ej2_base_1.isNullOrUndefined(content.position.y) || typeof content.position.y !== 'number') {
                            throw new Error('please enter valid y co-ordinate in ' + content.type + ' position...');
                        }
                    }
                }
            }
        };
        PDFExportHelper.prototype.drawText = function (pageTemplate, content) {
            var font = this.getFontFromContent(content);
            var brush = this.getBrushFromContent(content);
            var pen = null;
            if (!ej2_base_1.isNullOrUndefined(content.style) && !ej2_base_1.isNullOrUndefined(content.style.textPenColor)) {
                var penColor = this.hexDecToRgb(content.style.textPenColor);
                pen = new ej2_pdf_export_1.PdfPen(new ej2_pdf_export_1.PdfColor(penColor.r, penColor.g, penColor.b));
            }
            if (brush == null && pen == null) {
                brush = new ej2_pdf_export_1.PdfSolidBrush(new ej2_pdf_export_1.PdfColor(0, 0, 0));
            }
            var value = content.value.toString();
            var x = content.position.x * 0.75;
            var y = content.position.y * 0.75;
            var format = new ej2_pdf_export_1.PdfStringFormat();
            if (!ej2_base_1.isNullOrUndefined(content.stringFormat)) {
                format = content.stringFormat;
            }
            var result = this.setContentFormat(content, format);
            if (result !== null && !ej2_base_1.isNullOrUndefined(result.format) && !ej2_base_1.isNullOrUndefined(result.size)) {
                pageTemplate.graphics.drawString(value, font, pen, brush, x, y, result.size.width, result.size.height, result.format);
            }
            else {
                pageTemplate.graphics.drawString(value, font, pen, brush, x, y, format);
            }
        };
        PDFExportHelper.prototype.drawPageNumber = function (documentHeader, content) {
            var font = this.getFontFromContent(content);
            var brush = null;
            if (!ej2_base_1.isNullOrUndefined(content.style) && !ej2_base_1.isNullOrUndefined(content.style.textBrushColor)) {
                var brushColor = this.hexDecToRgb(content.style.textBrushColor);
                brush = new ej2_pdf_export_1.PdfSolidBrush(new ej2_pdf_export_1.PdfColor(brushColor.r, brushColor.g, brushColor.b));
            }
            else {
                brush = new ej2_pdf_export_1.PdfSolidBrush(new ej2_pdf_export_1.PdfColor(0, 0, 0));
            }
            var pageNumber = new ej2_pdf_export_1.PdfPageNumberField(font, brush);
            pageNumber.numberStyle = this.getPageNumberStyle(content.pageNumberType);
            var compositeField;
            var format;
            if (!ej2_base_1.isNullOrUndefined(content.format)) {
                var total = '$total';
                var current = '$current';
                if (content.format.indexOf(total) !== -1 && content.format.indexOf(current) !== -1) {
                    var pageCount = new ej2_pdf_export_1.PdfPageCountField(font);
                    pageCount.numberStyle = this.getPageNumberStyle(content.pageNumberType);
                    if (content.format.indexOf(total) > content.format.indexOf(current)) {
                        format = content.format.replace(current, '0');
                        format = format.replace(total, '1');
                    }
                    else {
                        format = content.format.replace(current, '1');
                        format = format.replace(total, '0');
                    }
                    compositeField = new ej2_pdf_export_1.PdfCompositeField(font, brush, format, pageNumber, pageCount);
                }
                else if (content.format.indexOf(current) !== -1 && content.format.indexOf(total) === -1) {
                    format = content.format.replace(current, '0');
                    compositeField = new ej2_pdf_export_1.PdfCompositeField(font, brush, format, pageNumber);
                }
                else {
                    var pageCount = new ej2_pdf_export_1.PdfPageCountField(font);
                    format = content.format.replace(total, '0');
                    compositeField = new ej2_pdf_export_1.PdfCompositeField(font, brush, format, pageCount);
                }
            }
            else {
                format = '{0}';
                compositeField = new ej2_pdf_export_1.PdfCompositeField(font, brush, format, pageNumber);
            }
            var x = content.position.x * 0.75;
            var y = content.position.y * 0.75;
            var result = this.setContentFormat(content, compositeField.stringFormat);
            if (result !== null && !ej2_base_1.isNullOrUndefined(result.format) && !ej2_base_1.isNullOrUndefined(result.size)) {
                compositeField.stringFormat = result.format;
                compositeField.bounds = new ej2_pdf_export_1.RectangleF(x, y, result.size.width, result.size.height);
            }
            compositeField.draw(documentHeader.graphics, x, y);
        };
        PDFExportHelper.prototype.drawImage = function (documentHeader, content) {
            var x = content.position.x * 0.75;
            var y = content.position.y * 0.75;
            var width = (!ej2_base_1.isNullOrUndefined(content.size) && !ej2_base_1.isNullOrUndefined(content.size.width)) ?
                (content.size.width * 0.75) : undefined;
            var height = (!ej2_base_1.isNullOrUndefined(content.size) && !ej2_base_1.isNullOrUndefined(content.size.height)) ?
                (content.size.height * 0.75) : undefined;
            var image = new ej2_pdf_export_1.PdfBitmap(content.src);
            if (!ej2_base_1.isNullOrUndefined(width)) {
                documentHeader.graphics.drawImage(image, x, y, width, height);
            }
            else {
                documentHeader.graphics.drawImage(image, x, y);
            }
        };
        PDFExportHelper.prototype.drawLine = function (documentHeader, content) {
            var x1 = content.points.x1 * 0.75;
            var y1 = content.points.y1 * 0.75;
            var x2 = content.points.x2 * 0.75;
            var y2 = content.points.y2 * 0.75;
            var pen = this.getPenFromContent(content);
            Eif (!ej2_base_1.isNullOrUndefined(content.style)) {
                Eif (!ej2_base_1.isNullOrUndefined(content.style.penSize) && typeof content.style.penSize === 'number') {
                    pen.width = content.style.penSize * 0.75;
                }
                pen.dashStyle = this.getDashStyle(content.style.dashStyle);
            }
            documentHeader.graphics.drawLine(pen, x1, y1, x2, y2);
        };
        PDFExportHelper.prototype.getFontFromContent = function (content) {
            var fontSize = (!ej2_base_1.isNullOrUndefined(content.font) && !ej2_base_1.isNullOrUndefined(content.font['fontSize'])) ?
                (content.font['fontSize'] * 0.75) : (!ej2_base_1.isNullOrUndefined(content.style) &&
                !ej2_base_1.isNullOrUndefined(content.style.fontSize)) ? (content.style.fontSize * 0.75) : 9.75;
            var fontFamily = (!ej2_base_1.isNullOrUndefined(content.font) && !ej2_base_1.isNullOrUndefined(content.font['pdfFontFamily']))
                ? content.font['pdfFontFamily'] : ej2_pdf_export_1.PdfFontFamily.TimesRoman;
            var fontStyle = ej2_pdf_export_1.PdfFontStyle.Regular;
            if (!ej2_base_1.isNullOrUndefined(content.font) && !ej2_base_1.isNullOrUndefined(content.font.bold)) {
                fontStyle |= ej2_pdf_export_1.PdfFontStyle.Bold;
            }
            if (!ej2_base_1.isNullOrUndefined(content.font) && !ej2_base_1.isNullOrUndefined(content.font.italic)) {
                fontStyle |= ej2_pdf_export_1.PdfFontStyle.Italic;
            }
            if (!ej2_base_1.isNullOrUndefined(content.font) && !ej2_base_1.isNullOrUndefined(content.font.underline)) {
                fontStyle |= ej2_pdf_export_1.PdfFontStyle.Underline;
            }
            if (!ej2_base_1.isNullOrUndefined(content.font) && !ej2_base_1.isNullOrUndefined(content.font.strikeout)) {
                fontStyle |= ej2_pdf_export_1.PdfFontStyle.Strikeout;
            }
            return new ej2_pdf_export_1.PdfStandardFont(fontFamily, fontSize, fontStyle);
        };
        PDFExportHelper.prototype.getPenFromContent = function (content) {
            var pen = new ej2_pdf_export_1.PdfPen(new ej2_pdf_export_1.PdfColor(0, 0, 0));
            Eif (!ej2_base_1.isNullOrUndefined(content.style) && content.style !== null && !ej2_base_1.isNullOrUndefined(content.style.penColor)) {
                var penColor = this.hexDecToRgb(content.style.penColor);
                pen = new ej2_pdf_export_1.PdfPen(new ej2_pdf_export_1.PdfColor(penColor.r, penColor.g, penColor.b));
            }
            return pen;
        };
        PDFExportHelper.prototype.getBrushFromContent = function (content) {
            var brush = null;
            if (!ej2_base_1.isNullOrUndefined(content.style) && !ej2_base_1.isNullOrUndefined(content.style.textBrushColor)) {
                var brushColor = this.hexDecToRgb(content.style.textBrushColor);
                brush = new ej2_pdf_export_1.PdfSolidBrush(new ej2_pdf_export_1.PdfColor(brushColor.r, brushColor.g, brushColor.b));
            }
            return brush;
        };
        PDFExportHelper.prototype.setContentFormat = function (content, format) {
            if (!ej2_base_1.isNullOrUndefined(content.size)) {
                var width = content.size.width * 0.75;
                var height = content.size.height * 0.75;
                format = new ej2_pdf_export_1.PdfStringFormat(ej2_pdf_export_1.PdfTextAlignment.Left, ej2_pdf_export_1.PdfVerticalAlignment.Middle);
                if (!ej2_base_1.isNullOrUndefined(content.style) && !ej2_base_1.isNullOrUndefined(content.style.hAlign)) {
                    switch (content.style.hAlign) {
                        case 'Right':
                            format.alignment = ej2_pdf_export_1.PdfTextAlignment.Right;
                            break;
                        case 'Center':
                            format.alignment = ej2_pdf_export_1.PdfTextAlignment.Center;
                            break;
                        case 'Justify':
                            format.alignment = ej2_pdf_export_1.PdfTextAlignment.Justify;
                            break;
                    }
                }
                if (!ej2_base_1.isNullOrUndefined(content.style) && !ej2_base_1.isNullOrUndefined(content.style.vAlign)) {
                    switch (content.style.vAlign) {
                        case 'Bottom':
                            format.lineAlignment = ej2_pdf_export_1.PdfVerticalAlignment.Bottom;
                            break;
                        case 'Top':
                            format.lineAlignment = ej2_pdf_export_1.PdfVerticalAlignment.Top;
                            break;
                    }
                }
                return { format: format, size: new ej2_pdf_export_1.SizeF(width, height) };
            }
            return null;
        };
        PDFExportHelper.prototype.getPageNumberStyle = function (pageNumberType) {
            switch (pageNumberType) {
                case 'LowerLatin':
                    return 2;
                case 'LowerRoman':
                    return 3;
                case 'UpperLatin':
                    return 4;
                case 'UpperRoman':
                    return 5;
                default:
                    return 1;
            }
        };
        PDFExportHelper.prototype.getDashStyle = function (dashType) {
            switch (dashType) {
                case 'Dash':
                    return 1;
                case 'Dot':
                    return 2;
                case 'DashDot':
                    return 3;
                case 'DashDotDot':
                    return 4;
                default:
                    return 0;
            }
        };
        PDFExportHelper.prototype.hexDecToRgb = function (hexDec) {
            if (hexDec === null || hexDec === '' || hexDec.length !== 7) {
                throw new Error('please set valid hex value for color..');
            }
            hexDec = hexDec.substring(1);
            var bigint = parseInt(hexDec, 16);
            var r = (bigint >> 16) & 255;
            var g = (bigint >> 8) & 255;
            var b = bigint & 255;
            return { r: r, g: g, b: b };
        };
        return PDFExportHelper;
    }());
    exports.PDFExportHelper = PDFExportHelper;
});