survey-pdf
Version:
A UI component that uses SurveyJS form JSON schemas to render forms as PDF documents. It populates PDF fields with data collected using SurveyJS Form Library and lets you export your SurveyJS forms as editable or pre-filled PDFs.
995 lines (973 loc) • 531 kB
JavaScript
/*!
* surveyjs - SurveyJS PDF library v2.5.20
* Copyright (c) 2015-2026 Devsoft Baltic OÜ - http://surveyjs.io/
* License: MIT (http://www.opensource.org/licenses/mit-license.php)
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('survey-core'), require('jspdf'), require('node-fetch'), require('image-size')) :
typeof define === 'function' && define.amd ? define(['exports', 'survey-core', 'jspdf', 'node-fetch', 'image-size'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SurveyPDF = {}, global.Survey, global.jspdf, global["node-fetch"], global["image-size"]));
})(this, (function (exports, SurveyCore, jspdf, fetch, imageSize) { 'use strict';
function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var SurveyCore__namespace = /*#__PURE__*/_interopNamespaceDefault(SurveyCore);
var SurveyPDFModule = /*#__PURE__*/Object.freeze({
__proto__: null,
get BooleanItemBrick () { return BooleanItemBrick; },
get CheckItemBrick () { return CheckItemBrick; },
get CheckboxItemBrick () { return CheckboxItemBrick; },
get CompositeBrick () { return CompositeBrick; },
get CustomBrick () { return CustomBrick; },
get DocController () { return DocController; },
get DocOptions () { return DocOptions; },
get DrawCanvas () { return DrawCanvas; },
get DropdownBrick () { return DropdownBrick; },
get EmptyBrick () { return EmptyBrick; },
get EventHandler () { return EventHandler; },
get FlatBoolean () { return FlatBooleanCheckbox; },
get FlatCheckbox () { return FlatCheckbox; },
get FlatComment () { return FlatComment; },
get FlatCustomModel () { return FlatCustomModel; },
get FlatDropdown () { return FlatDropdown; },
get FlatExpression () { return FlatExpression; },
get FlatFile () { return FlatFile; },
get FlatHTML () { return FlatHTML; },
get FlatImage () { return FlatImage; },
get FlatImagePicker () { return FlatImagePicker; },
get FlatMatrix () { return FlatMatrix; },
get FlatMatrixDynamic () { return FlatMatrixDynamic; },
get FlatMatrixMultiple () { return FlatMatrixMultiple; },
get FlatMultipleText () { return FlatMultipleText; },
get FlatPanelDynamic () { return FlatPanelDynamic; },
get FlatQuestion () { return FlatQuestion; },
get FlatQuestionDefault () { return FlatQuestionDefault; },
get FlatRadiogroup () { return FlatRadiogroup; },
get FlatRanking () { return FlatRanking; },
get FlatRating () { return FlatRating; },
get FlatRepository () { return FlatRepository; },
get FlatSelectBase () { return FlatSelectBase; },
get FlatSignaturePad () { return FlatSignaturePad; },
get FlatSlider () { return FlatSlider; },
get FlatSurvey () { return FlatSurvey; },
get FlatTextbox () { return FlatTextbox; },
get HTMLBrick () { return HTMLBrick; },
get HorizontalAlign () { return exports.HorizontalAlign; },
get ImageBrick () { return ImageBrick; },
get LinkBrick () { return LinkBrick; },
get PagePacker () { return PagePacker; },
get PdfBrick () { return PdfBrick; },
get RadioItemBrick () { return RadioItemBrick; },
get RankingItemBrick () { return RankingItemBrick; },
get RowlineBrick () { return RowlineBrick; },
get SurveyHelper () { return SurveyHelper; },
get SurveyPDF () { return SurveyPDF; },
get TextBoldBrick () { return TextBoldBrick; },
get TextBrick () { return TextBrick; },
get TextFieldBrick () { return TextFieldBrick; },
get TitlePanelBrick () { return TitlePanelBrick; },
get VerticalAlign () { return exports.VerticalAlign; }
});
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
function __spreadArray(to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var CompositeBrick = /** @class */ (function () {
function CompositeBrick() {
var bricks = [];
for (var _i = 0; _i < arguments.length; _i++) {
bricks[_i] = arguments[_i];
}
this.bricks = [];
this.isPageBreak = false;
this._xLeft = 0.0;
this._xRight = 0.0;
this._yTop = 0.0;
this._yBot = 0.0;
this.addBrick.apply(this, bricks);
}
Object.defineProperty(CompositeBrick.prototype, "xLeft", {
get: function () { return this._xLeft; },
set: function (xLeft) {
this.shift(xLeft - this.xLeft, 0.0, 0.0, 0.0);
this._xLeft = xLeft;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CompositeBrick.prototype, "xRight", {
get: function () { return this._xRight; },
set: function (xRight) {
this.shift(0.0, xRight - this.xRight, 0.0, 0.0);
this._xRight = xRight;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CompositeBrick.prototype, "yTop", {
get: function () { return this._yTop; },
set: function (yTop) {
this.shift(0.0, 0.0, yTop - this.yTop, 0.0);
this._yTop = yTop;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CompositeBrick.prototype, "yBot", {
get: function () { return this._yBot; },
set: function (yBot) {
this.shift(0.0, 0.0, 0.0, yBot - this.yBot);
this._yBot = yBot;
},
enumerable: false,
configurable: true
});
CompositeBrick.prototype.shift = function (leftShift, rightShift, topShift, botShift) {
this.bricks.forEach(function (brick) {
brick.xLeft += leftShift;
brick.xRight += rightShift;
brick.yTop += topShift;
brick.yBot += botShift;
});
};
Object.defineProperty(CompositeBrick.prototype, "width", {
get: function () {
return this.xRight - this.xLeft;
},
enumerable: false,
configurable: true
});
Object.defineProperty(CompositeBrick.prototype, "height", {
get: function () {
return this.yBot - this.yTop;
},
enumerable: false,
configurable: true
});
CompositeBrick.prototype.render = function () {
return __awaiter(this, void 0, void 0, function () {
var i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
i = 0;
_a.label = 1;
case 1:
if (!(i < this.bricks.length)) return [3 /*break*/, 4];
return [4 /*yield*/, this.bricks[i].render()];
case 2:
_a.sent();
_a.label = 3;
case 3:
i++;
return [3 /*break*/, 1];
case 4: return [2 /*return*/];
}
});
});
};
Object.defineProperty(CompositeBrick.prototype, "isEmpty", {
get: function () {
return this.bricks.length === 0;
},
enumerable: false,
configurable: true
});
CompositeBrick.prototype.addBrick = function () {
var _a;
var bricks = [];
for (var _i = 0; _i < arguments.length; _i++) {
bricks[_i] = arguments[_i];
}
if (bricks.length != 0) {
(_a = this.bricks).push.apply(_a, bricks);
var mergeRect = SurveyHelper.mergeRects.apply(SurveyHelper, this.bricks);
this._xLeft = mergeRect.xLeft;
this._xRight = mergeRect.xRight;
this._yTop = mergeRect.yTop;
this._yBot = mergeRect.yBot;
}
};
CompositeBrick.prototype.unfold = function () {
var unfoldBricks = [];
this.bricks.forEach(function (brick) {
unfoldBricks.push.apply(unfoldBricks, brick.unfold());
});
return unfoldBricks;
};
CompositeBrick.prototype.translateX = function (func) {
this.bricks.forEach(function (brick) { return brick.translateX(func); });
var res = func(this.xLeft, this.xRight);
this._xLeft = res.xLeft;
this._xRight = res.xRight;
};
return CompositeBrick;
}());
var RowlineBrick = /** @class */ (function () {
function RowlineBrick(controller, rect, color) {
this.controller = controller;
this.color = color;
this.isPageBreak = false;
this.xLeft = rect.xLeft;
this.xRight = rect.xRight;
this.yTop = rect.yTop;
this.yBot = rect.yBot;
}
Object.defineProperty(RowlineBrick.prototype, "width", {
get: function () {
return this.xRight - this.xLeft;
},
enumerable: false,
configurable: true
});
Object.defineProperty(RowlineBrick.prototype, "height", {
get: function () {
return this.yBot - this.yTop;
},
enumerable: false,
configurable: true
});
RowlineBrick.prototype.render = function () {
return __awaiter(this, void 0, void 0, function () {
var oldDrawColor;
return __generator(this, function (_a) {
if (this.color !== null) {
oldDrawColor = this.controller.doc.getDrawColor();
this.controller.doc.setDrawColor(this.color);
this.controller.doc.line(this.xLeft, this.yTop, this.xRight, this.yTop);
this.controller.doc.setDrawColor(oldDrawColor);
}
return [2 /*return*/];
});
});
};
RowlineBrick.prototype.unfold = function () {
return [this];
};
RowlineBrick.prototype.translateX = function (_) { };
return RowlineBrick;
}());
var AdornersBaseOptions = /** @class */ (function () {
function AdornersBaseOptions(point, bricks, controller, repository, module) {
this.point = point;
this.bricks = bricks;
this.controller = controller;
this.repository = repository;
this.module = module;
}
return AdornersBaseOptions;
}());
var AdornersOptions = /** @class */ (function (_super) {
__extends(AdornersOptions, _super);
function AdornersOptions(point, bricks, question, controller, repository, module) {
var _this = _super.call(this, point, bricks, controller, repository, module) || this;
_this.question = question;
return _this;
}
return AdornersOptions;
}(AdornersBaseOptions));
var AdornersPanelOptions = /** @class */ (function (_super) {
__extends(AdornersPanelOptions, _super);
function AdornersPanelOptions(point, bricks, panel, controller, repository, module) {
var _this = _super.call(this, point, bricks, controller, repository, module) || this;
_this.panel = panel;
return _this;
}
return AdornersPanelOptions;
}(AdornersBaseOptions));
var AdornersPageOptions = /** @class */ (function (_super) {
__extends(AdornersPageOptions, _super);
function AdornersPageOptions(point, bricks, page, controller, repository, module) {
var _this = _super.call(this, point, bricks, controller, repository, module) || this;
_this.page = page;
return _this;
}
return AdornersPageOptions;
}(AdornersBaseOptions));
var FlatSurvey = /** @class */ (function () {
function FlatSurvey() {
}
FlatSurvey.generateFlatsPanel = function (survey, controller, panel, point) {
return __awaiter(this, void 0, void 0, function () {
var panelFlats, panelContentPoint, _a, _b, _c, adornersOptions;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
panelFlats = [];
panelContentPoint = SurveyHelper.clone(point);
controller.pushMargins();
controller.margins.left += controller.measureText(panel.innerIndent).width;
panelContentPoint.xLeft += controller.measureText(panel.innerIndent).width;
_b = (_a = panelFlats.push).apply;
_c = [panelFlats];
return [4 /*yield*/, this.generateFlatsPagePanel(survey, controller, panel, panelContentPoint)];
case 1:
_b.apply(_a, _c.concat([_d.sent()]));
controller.popMargins();
adornersOptions = new AdornersPanelOptions(point, panelFlats, panel, controller, FlatRepository.getInstance(), SurveyPDFModule);
return [4 /*yield*/, survey.onRenderPanel.fire(survey, adornersOptions)];
case 2:
_d.sent();
return [2 /*return*/, __spreadArray([], adornersOptions.bricks, true)];
}
});
});
};
FlatSurvey.generateFlatsPagePanel = function (survey, controller, pagePanel, point) {
return __awaiter(this, void 0, void 0, function () {
var pagePanelFlats, currPoint, compositeFlat, noFlat, pagelPanelTitleFlat, pagePanelDescFlat, rowLinePoint, _i, _a, row, width, nextMarginLeft, rowFlats, visibleElements, i, element, persWidth, _b, _c, _d, _e, _f, _g;
return __generator(this, function (_h) {
switch (_h.label) {
case 0:
if (!pagePanel.isVisible)
return [2 /*return*/];
pagePanel.onFirstRendering();
pagePanelFlats = [];
currPoint = SurveyHelper.clone(point);
if (!(pagePanel.getType() !== 'page' || survey.showPageTitles)) return [3 /*break*/, 7];
compositeFlat = new CompositeBrick();
if (!pagePanel.title) return [3 /*break*/, 4];
if (!(pagePanel instanceof SurveyCore.PanelModel && pagePanel.no)) return [3 /*break*/, 2];
return [4 /*yield*/, SurveyHelper.createTitlePanelFlat(currPoint, controller, pagePanel.no, pagePanel.getType() === 'page')];
case 1:
noFlat = _h.sent();
compositeFlat.addBrick(noFlat);
currPoint.xLeft = noFlat.xRight + controller.measureText(' ').width;
_h.label = 2;
case 2: return [4 /*yield*/, SurveyHelper.createTitlePanelFlat(currPoint, controller, pagePanel.locTitle, pagePanel.getType() === 'page')];
case 3:
pagelPanelTitleFlat = _h.sent();
compositeFlat.addBrick(pagelPanelTitleFlat);
currPoint = SurveyHelper.createPoint(pagelPanelTitleFlat);
_h.label = 4;
case 4:
if (!pagePanel.description) return [3 /*break*/, 6];
if (pagePanel.title) {
currPoint.yTop += controller.unitWidth * FlatSurvey.PANEL_DESC_GAP_SCALE;
}
return [4 /*yield*/, SurveyHelper.createDescFlat(currPoint, null, controller, pagePanel.locDescription)];
case 5:
pagePanelDescFlat = _h.sent();
compositeFlat.addBrick(pagePanelDescFlat);
currPoint = SurveyHelper.createPoint(pagePanelDescFlat);
_h.label = 6;
case 6:
if (!compositeFlat.isEmpty) {
rowLinePoint = SurveyHelper.createPoint(compositeFlat);
compositeFlat.addBrick(SurveyHelper.createRowlineFlat(rowLinePoint, controller));
pagePanelFlats.push(compositeFlat);
currPoint.yTop += controller.unitHeight * FlatSurvey.PANEL_CONT_GAP_SCALE + SurveyHelper.EPSILON;
}
_h.label = 7;
case 7:
_i = 0, _a = pagePanel.rows;
_h.label = 8;
case 8:
if (!(_i < _a.length)) return [3 /*break*/, 17];
row = _a[_i];
if (!row.visible)
return [3 /*break*/, 16];
controller.pushMargins();
width = SurveyHelper.getPageAvailableWidth(controller);
nextMarginLeft = controller.margins.left;
rowFlats = [];
visibleElements = row.elements.filter(function (el) { return el.isVisible; });
i = 0;
_h.label = 9;
case 9:
if (!(i < visibleElements.length)) return [3 /*break*/, 15];
element = visibleElements[i];
if (!element.isVisible)
return [3 /*break*/, 14];
persWidth = SurveyHelper.parseWidth(element.renderWidth, width - (visibleElements.length - 1) * controller.unitWidth, visibleElements.length);
controller.margins.left = nextMarginLeft + ((i !== 0) ? controller.unitWidth : 0);
controller.margins.right = controller.paperWidth - controller.margins.left - persWidth;
currPoint.xLeft = controller.margins.left;
nextMarginLeft = controller.margins.left + persWidth;
if (!(element instanceof SurveyCore.PanelModel)) return [3 /*break*/, 11];
_c = (_b = rowFlats.push).apply;
_d = [rowFlats];
return [4 /*yield*/, this.generateFlatsPanel(survey, controller, element, currPoint)];
case 10:
_c.apply(_b, _d.concat([_h.sent()]));
return [3 /*break*/, 14];
case 11: return [4 /*yield*/, element.waitForQuestionIsReady()];
case 12:
_h.sent();
_f = (_e = rowFlats.push).apply;
_g = [rowFlats];
return [4 /*yield*/, SurveyHelper.generateQuestionFlats(survey, controller, element, currPoint)];
case 13:
_f.apply(_e, _g.concat([_h.sent()]));
_h.label = 14;
case 14:
i++;
return [3 /*break*/, 9];
case 15:
controller.popMargins();
currPoint.xLeft = controller.margins.left;
if (rowFlats.length !== 0) {
currPoint.yTop = SurveyHelper.mergeRects.apply(SurveyHelper, rowFlats).yBot;
currPoint.xLeft = point.xLeft;
currPoint.yTop += controller.unitHeight * FlatSurvey.QUES_GAP_VERT_SCALE;
pagePanelFlats.push.apply(pagePanelFlats, rowFlats);
pagePanelFlats.push(SurveyHelper.createRowlineFlat(currPoint, controller));
currPoint.yTop += SurveyHelper.EPSILON;
}
_h.label = 16;
case 16:
_i++;
return [3 /*break*/, 8];
case 17: return [2 /*return*/, pagePanelFlats];
}
});
});
};
FlatSurvey.popRowlines = function (flats) {
while (flats.length > 0 && flats[flats.length - 1] instanceof RowlineBrick) {
flats.pop();
}
};
FlatSurvey.generateFlatTitle = function (survey, controller, point) {
return __awaiter(this, void 0, void 0, function () {
var compositeFlat, surveyTitleFlat, _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
compositeFlat = new CompositeBrick();
if (!survey.showTitle) return [3 /*break*/, 4];
if (!survey.title) return [3 /*break*/, 2];
return [4 /*yield*/, SurveyHelper.createTitleSurveyFlat(point, controller, survey.locTitle)];
case 1:
surveyTitleFlat = _c.sent();
compositeFlat.addBrick(surveyTitleFlat);
point = SurveyHelper.createPoint(surveyTitleFlat);
_c.label = 2;
case 2:
if (!survey.description) return [3 /*break*/, 4];
if (survey.title) {
point.yTop += controller.unitWidth * FlatSurvey.PANEL_DESC_GAP_SCALE;
}
_b = (_a = compositeFlat).addBrick;
return [4 /*yield*/, SurveyHelper.createDescFlat(point, null, controller, survey.locDescription)];
case 3:
_b.apply(_a, [_c.sent()]);
_c.label = 4;
case 4: return [2 /*return*/, compositeFlat];
}
});
});
};
FlatSurvey.generateFlatLogoImage = function (survey, controller, point) {
return __awaiter(this, void 0, void 0, function () {
var logoUrl, logoSize, logoFlat, shift;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logoUrl = SurveyHelper.getLocString(survey.locLogo);
return [4 /*yield*/, SurveyHelper.getCorrectedImageSize(controller, { imageLink: logoUrl, imageHeight: survey.logoHeight, imageWidth: survey.logoWidth, defaultImageWidth: '300px', defaultImageHeight: '200px' })];
case 1:
logoSize = _a.sent();
return [4 /*yield*/, SurveyHelper.createImageFlat(point, null, controller, { link: logoUrl,
width: logoSize.width, height: logoSize.height })];
case 2:
logoFlat = _a.sent();
shift = 0;
if (survey.logoPosition === 'right') {
shift = SurveyHelper.getPageAvailableWidth(controller) - logoFlat.width;
}
else if (survey.logoPosition !== 'left') {
shift = SurveyHelper.getPageAvailableWidth(controller) / 2.0 - logoFlat.width / 2.0;
}
logoFlat.xLeft += shift;
logoFlat.xRight += shift;
return [2 /*return*/, logoFlat];
}
});
});
};
FlatSurvey.generateFlats = function (survey, controller) {
return __awaiter(this, void 0, void 0, function () {
var flats, titleFlat, logoFlat, titlePoint, titleFlat, logoFlat, titleFlat, titleFlat, logoPoint, logoFlat, point, i, pageFlats, _a, _b, _c, adornersOptions;
var _d;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
flats = [];
if (!!survey.hasLogo) return [3 /*break*/, 2];
return [4 /*yield*/, this.generateFlatTitle(survey, controller, controller.leftTopPoint)];
case 1:
titleFlat = _e.sent();
if (!titleFlat.isEmpty)
flats.push([titleFlat]);
return [3 /*break*/, 11];
case 2:
if (!survey.isLogoBefore) return [3 /*break*/, 5];
return [4 /*yield*/, this.generateFlatLogoImage(survey, controller, controller.leftTopPoint)];
case 3:
logoFlat = _e.sent();
flats.push([logoFlat]);
titlePoint = SurveyHelper.createPoint(logoFlat, survey.logoPosition === 'top', survey.logoPosition !== 'top');
if (survey.logoPosition !== 'top') {
controller.pushMargins();
titlePoint.xLeft += controller.unitWidth;
controller.margins.left += logoFlat.width + controller.unitWidth;
}
else {
titlePoint.xLeft = controller.leftTopPoint.xLeft;
titlePoint.yTop += controller.unitHeight / 2.0;
}
return [4 /*yield*/, this.generateFlatTitle(survey, controller, titlePoint)];
case 4:
titleFlat = _e.sent();
if (survey.logoPosition !== 'top')
controller.popMargins();
if (!titleFlat.isEmpty)
flats[0].push(titleFlat);
return [3 /*break*/, 11];
case 5:
if (!(survey.logoPosition === 'right')) return [3 /*break*/, 8];
return [4 /*yield*/, this.generateFlatLogoImage(survey, controller, controller.leftTopPoint)];
case 6:
logoFlat = _e.sent();
flats.push([logoFlat]);
controller.pushMargins();
controller.margins.right += logoFlat.width + controller.unitWidth;
return [4 /*yield*/, this.generateFlatTitle(survey, controller, controller.leftTopPoint)];
case 7:
titleFlat = _e.sent();
if (!titleFlat.isEmpty)
flats[0].unshift(titleFlat);
controller.popMargins();
return [3 /*break*/, 11];
case 8: return [4 /*yield*/, this.generateFlatTitle(survey, controller, controller.leftTopPoint)];
case 9:
titleFlat = _e.sent();
logoPoint = controller.leftTopPoint;
if (!titleFlat.isEmpty) {
flats.push([titleFlat]);
logoPoint = SurveyHelper.createPoint(titleFlat);
logoPoint.yTop += controller.unitHeight / 2.0;
}
return [4 /*yield*/, this.generateFlatLogoImage(survey, controller, logoPoint)];
case 10:
logoFlat = _e.sent();
if (flats.length !== 0)
flats[0].push(logoFlat);
else
flats.push([logoFlat]);
_e.label = 11;
case 11:
point = controller.leftTopPoint;
if (flats.length !== 0) {
point.yTop = SurveyHelper.createPoint(SurveyHelper.mergeRects.apply(SurveyHelper, flats[0])).yTop;
flats[0].push(SurveyHelper.createRowlineFlat(point, controller));
point.yTop += controller.unitHeight * FlatSurvey.PANEL_CONT_GAP_SCALE + SurveyHelper.EPSILON;
}
i = 0;
_e.label = 12;
case 12:
if (!(i < survey.visiblePages.length)) return [3 /*break*/, 16];
survey.currentPage = survey.visiblePages[i];
pageFlats = [];
_b = (_a = pageFlats.push).apply;
_c = [pageFlats];
return [4 /*yield*/, this.generateFlatsPagePanel(survey, controller, survey.visiblePages[i], point)];
case 13:
_b.apply(_a, _c.concat([_e.sent()]));
adornersOptions = new AdornersPageOptions(point, pageFlats, survey.visiblePages[i], controller, FlatRepository.getInstance(), SurveyPDFModule);
return [4 /*yield*/, survey.onRenderPage.fire(survey, adornersOptions)];
case 14:
_e.sent();
pageFlats = __spreadArray([], adornersOptions.bricks, true);
if (i === 0 && flats.length !== 0) {
(_d = flats[0]).push.apply(_d, pageFlats);
}
else
flats.push(pageFlats);
this.popRowlines(flats[flats.length - 1]);
point.yTop = controller.leftTopPoint.yTop;
_e.label = 15;
case 15:
i++;
return [3 /*break*/, 12];
case 16: return [2 /*return*/, flats];
}
});
});
};
FlatSurvey.QUES_GAP_VERT_SCALE = 1.5;
FlatSurvey.PANEL_CONT_GAP_SCALE = 1.0;
FlatSurvey.PANEL_DESC_GAP_SCALE = 0.25;
return FlatSurvey;
}());
/**
* An object that describes a PDF brick—a simple element with specified content, size, and location. Bricks are fundamental elements used to construct a PDF document.
*
* You can access `PdfBrick` objects within functions that handle `SurveyPDF`'s [`onRenderQuestion`](https://surveyjs.io/pdf-generator/documentation/api-reference/surveypdf#onRenderQuestion), [`onRenderPanel`](https://surveyjs.io/pdf-generator/documentation/api-reference/surveypdf#onRenderPanel), and [`onRenderPage`](https://surveyjs.io/pdf-generator/documentation/api-reference/surveypdf#onRenderPage) events.
*
* [View Demo](https://surveyjs.io/pdf-generator/examples/add-markup-to-customize-pdf-forms/ (linkStyle))
*/
var PdfBrick = /** @class */ (function () {
function PdfBrick(question, controller, rect) {
this.question = question;
this.controller = controller;
/**
* The color of text within the brick.
*
* Default value: `"#404040"`
*/
this.textColor = SurveyHelper.TEXT_COLOR;
this.formBorderColor = SurveyHelper.FORM_BORDER_COLOR;
this.isPageBreak = false;
this.xLeft = rect.xLeft;
this.xRight = rect.xRight;
this.yTop = rect.yTop;
this.yBot = rect.yBot;
this.fontSize = !!controller ?
controller.fontSize : DocController.FONT_SIZE;
}
Object.defineProperty(PdfBrick.prototype, "xLeft", {
/**
* An X-coordinate for the left brick edge.
*/
get: function () {
return this._xLeft;
},
set: function (val) {
this.setXLeft(val);
},
enumerable: false,
configurable: true
});
Object.defineProperty(PdfBrick.prototype, "xRight", {
/**
* An X-coordinate for the right brick edge.
*/
get: function () {
return this._xRight;
},
set: function (val) {
this.setXRight(val);
},
enumerable: false,
configurable: true
});
Object.defineProperty(PdfBrick.prototype, "yTop", {
/**
* A Y-coordinate for the top brick edge.
*/
get: function () {
return this._yTop;
},
set: function (val) {
this.setYTop(val);
},
enumerable: false,
configurable: true
});
Object.defineProperty(PdfBrick.prototype, "yBot", {
/**
* A Y-coordinate for the bottom brick edge.
*/
get: function () {
return this._yBot;
},
set: function (val) {
this.setYBottom(val);
},
enumerable: false,
configurable: true
});
PdfBrick.prototype.translateX = function (func) {
var res = func(this.xLeft, this.xRight);
this.xLeft = res.xLeft;
this.xRight = res.xRight;
};
Object.defineProperty(PdfBrick.prototype, "width", {
/**
* The brick's width in pixels.
*/
get: function () {
return this.xRight - this.xLeft;
},
enumerable: false,
configurable: true
});
Object.defineProperty(PdfBrick.prototype, "height", {
/**
* The brick's height in pixels.
*/
get: function () {
return this.yBot - this.yTop;
},
enumerable: false,
configurable: true
});
PdfBrick.prototype.getShouldRenderReadOnly = function () {
return SurveyHelper.shouldRenderReadOnly(this.question, this.controller);
};
PdfBrick.prototype.render = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.getShouldRenderReadOnly()) return [3 /*break*/, 2];
return [4 /*yield*/, this.renderReadOnly()];
case 1:
_a.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, this.renderInteractive()];
case 3:
_a.sent();
_a.label = 4;
case 4:
this.afterRenderCallback && this.afterRenderCallback();
return [2 /*return*/];
}
});
});
};
PdfBrick.prototype.renderInteractive = function () {
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/];
}); });
};
PdfBrick.prototype.renderReadOnly = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.renderInteractive()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* Allows you to get a flat array of nested PDF bricks.
* @returns A flat array of nested PDF bricks.
*/
PdfBrick.prototype.unfold = function () {
return [this];
};
PdfBrick.prototype.getCorrectedText = function (val) {
return this.controller.isRTL ? (val || '').split('').reverse().join('') : val;
};
PdfBrick.prototype.setXLeft = function (val) {
this._xLeft = val;
};
PdfBrick.prototype.setXRight = function (val) {
this._xRight = val;
};
PdfBrick.prototype.setYTop = function (val) {
this._yTop = val;
};
PdfBrick.prototype.setYBottom = function (val) {
this._yBot = val;
};
return PdfBrick;
}());
var TextBrick = /** @class */ (function (_super) {
__extends(TextBrick, _super);
function TextBrick(question, controller, rect, text) {
var _this = _super.call(this, question, controller, rect) || this;
_this.text = text;
_this.align = {
isInputRtl: false,
isOutputRtl: controller.isRTL,
align: controller.isRTL ? 'right' : 'left',
baseline: 'middle'
};
return _this;
}
TextBrick.prototype.escapeText = function () {
while (this.text.indexOf('\t') > -1) {
this.text = this.text.replace('\t', Array(5).join(String.fromCharCode(160)));
}
return this.text;
};
TextBrick.prototype.renderInteractive = function () {
return __awaiter(this, void 0, void 0, function () {
var alignPoint, oldFontSize, oldTextColor;
return __generator(this, function (_a) {
alignPoint = this.alignPoint(this);
oldFontSize = this.controller.fontSize;
this.controller.fontSize = this.fontSize;
oldTextColor = this.controller.doc.getTextColor();
this.controller.doc.setTextColor(this.textColor);
this.controller.doc.text(this.escapeText(), alignPoint.xLeft, alignPoint.yTop, this.align);
this.controller.doc.setTextColor(oldTextColor);
this.controller.fontSize = oldFontSize;
return [2 /*return*/];
});
});
};
TextBrick.prototype.alignPoint = function (rect) {
return {
xLeft: this.controller.isRTL ? rect.xRight : rect.xLeft,
yTop: rect.yTop + (rect.yBot - rect.yTop) / 2.0
};
};
return TextBrick;
}(PdfBrick));
var FlatQuestion = /** @class */ (function () {
function FlatQuestion(survey, question, controller) {
this.survey = survey;
this.controller = controller;
this.question = question;
}
FlatQuestion.prototype.generateFlatTitle = function (point) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, SurveyHelper.createTitleFlat(point, this.question, this.controller)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
FlatQuestion.prototype.generateFlatDescription = function (point) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, SurveyHelper.createDescFlat(point, this.question, this.controller, this.question.locDescription)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
FlatQuestion.prototype.generateFlatHeader = function (point) {
return __awaiter(this, void 0, void 0, function () {
var titleFlat, compositeFlat, descPoint, _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, this.generateFlatTitle(point)];
case 1:
titleFlat = _c.sent();
compositeFlat = new CompositeBrick(titleFlat);
if (!this.question.hasDescriptionUnderTitle) return [3 /*break*/, 3];
descPoint = SurveyHelper.createPoint(titleFlat, true, false);
descPoint.yTop += FlatQuestion.DESC_GAP_SCALE * this.controller.unitHeight;
descPoint.xLeft += this.controller.unitWidth * FlatQuestion.CONTENT_INDENT_SCALE;
_b = (_a = compositeFlat).addBrick;
return [4 /*yield*/, this.generateFlatDescription(descPoint)];
case 2:
_b.apply(_a, [_c.sent()]);
_c.label = 3;
case 3: return [2 /*return*/, compositeFlat];
}
});
});
};
FlatQuestion.prototype.generateFlatsComment = function (point) {
return __awaiter(this, void 0, void 0, function () {
var text, otherTextFlat, otherPoint, _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
text = this.question.locCommentText;
return [4 /*yield*/, SurveyHelper.createTextFlat(point, this.question, this.controller, text, TextBrick)];
case 1:
otherTextFlat = _c.sent();
otherPoint = SurveyHelper.createPoint(otherTextFlat);
otherPoint.yTop += this.controller.unitHeight * SurveyHelper.GAP_BETWEEN_ROWS;
_a = CompositeBrick.bind;
_b = [void 0, otherTextFlat];
return [4 /*yield*/, SurveyHelper.createCommentFlat(otherPoint, this.question, this.controller, {
fieldName: this.question.id + '_comment',