UNPKG

survey-pdf

Version:

Renders JSON-driven SurveyJS forms and their responses as PDF documents in the browser or Node.js: fillable interactive PDF forms (AcroForm) or static printouts.

131 lines (126 loc) 6.62 kB
/*! * surveyjs - SurveyJS PDF library v3.0.2 * Copyright (c) 2015-2026 Devsoft Baltic OÜ - http://surveyjs.io/ * License: MIT (http://www.opensource.org/licenses/mit-license.php) */ import { B as BaseImageUtils, a1 as parseColorCssFunction, r as rgbaToHex, a as registerImageUtils, b as registerVariablesManagerCreator } from './pdf-shared.mjs'; export { C as CheckItemBrick, c as CompositeBrick, d as CustomBrick, D as DocController, e as DocOptions, f as DrawCanvas, g as DropdownBrick, E as EmptyBrick, h as EventHandler, F as FlatBoolean, i as FlatCheckbox, j as FlatComment, k as FlatCustomModel, l as FlatDropdown, m as FlatExpression, n as FlatFile, o as FlatHTML, p as FlatImage, q as FlatImagePicker, s as FlatMatrix, t as FlatMatrixDynamic, u as FlatMatrixMultiple, v as FlatMultipleText, w as FlatPage, x as FlatPanel, y as FlatPanelDynamic, z as FlatQuestion, A as FlatQuestionDefault, G as FlatRadiogroup, H as FlatRanking, I as FlatRating, J as FlatRepository, K as FlatSelectBase, L as FlatSignaturePad, M as FlatSlider, N as FlatSurvey, O as FlatTextbox, P as HTMLBrick, Q as HorizontalAlign, R as ImageBrick, S as LinkBrick, T as PagePacker, U as PdfBrick, V as RadioItemBrick, W as RankingItemBrick, X as RowlineBrick, Y as SurveyHelper, Z as SurveyPDF, _ as TextBrick, $ as TextFieldBrick, a0 as VerticalAlign } from './pdf-shared.mjs'; import 'survey-core'; import 'jspdf'; class ImageUtils extends BaseImageUtils { async _getImageInfo(url) { const pxToPt = 72.0 / 96.0; const image = new Image(); image.crossOrigin = 'anonymous'; const response = await new Promise((resolve, reject) => { image.onload = () => { const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); const dpr = window.devicePixelRatio || 1; canvas.height = image.naturalHeight * dpr; canvas.width = image.naturalWidth * dpr; ctx === null || ctx === void 0 ? void 0 : ctx.drawImage(image, 0, 0, image.naturalWidth * dpr, image.naturalHeight * dpr); const dataUrl = canvas.toDataURL(); resolve({ data: dataUrl, width: image.naturalWidth * pxToPt, height: image.naturalHeight * pxToPt, id: this.getImageId() }); }; image.onerror = () => { reject(); }; image.src = url; }); return response; } getCoverCanvasOptions(imageWidth, imageHeight, targetWidth, targetHeight) { const aspectRatio = imageWidth / imageHeight; const targetAspectRatio = targetWidth / targetHeight; let drawWidth, drawHeight; if (targetAspectRatio > aspectRatio) { drawWidth = targetWidth; drawHeight = targetWidth / aspectRatio; } else { drawHeight = targetHeight; drawWidth = targetHeight * aspectRatio; } return { canvasWidth: targetWidth, canvasHeight: targetHeight, imageX: (targetWidth - drawWidth) / 2, imageY: (targetHeight - drawHeight) / 2, imageWidth: drawWidth, imageHeight: drawHeight }; } async applyImageFit(imageInfo, imageFit, targetWidth, targetHeight) { if (imageFit == 'cover') { try { const image = new Image(); if (!imageInfo.width || !imageInfo.height || !imageInfo.data || !targetWidth || !targetHeight) return imageInfo; image.src = imageInfo.data instanceof Uint8Array ? URL.createObjectURL(new Blob([imageInfo.data])) : imageInfo.data; await image.decode(); const canvasOptions = this.getCoverCanvasOptions(imageInfo.width, imageInfo.height, targetWidth, targetHeight); const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); canvas.width = canvasOptions.canvasWidth; canvas.height = canvasOptions.canvasHeight; ctx === null || ctx === void 0 ? void 0 : ctx.drawImage(image, canvasOptions.imageX, canvasOptions.imageY, canvasOptions.imageWidth, canvasOptions.imageHeight); return { data: canvas.toDataURL(), width: targetWidth, height: targetHeight }; } catch { return imageInfo; } } else { return super.applyImageFit(imageInfo, imageFit, targetWidth, targetHeight); } } } class VariablesManager { setup(variables) { this.variables = variables; this.hash = {}; this.container = document.createElement('div'); this.container.style.display = 'none'; Object.keys(variables).forEach(property => { var _a; (_a = this.container) === null || _a === void 0 ? void 0 : _a.style.setProperty(property, variables[property]); }); this.computedStyle = getComputedStyle(this.container); } startCollectingVariables() { var _a; if (this.container && !((_a = this.container) === null || _a === void 0 ? void 0 : _a.isConnected)) { document.body.appendChild(this.container); } } stopCollectingVariables() { var _a; (_a = this.container) === null || _a === void 0 ? void 0 : _a.remove(); } getVariable(name, calcCallback) { if (!this.container || !this.variables || !this.hash || !this.computedStyle) { throw new Error('VariablesManager not initialized'); } if (!this.hash[name]) { this.hash[name] = calcCallback(this.computedStyle.getPropertyValue(name), this.container); } return this.hash[name]; } getSizeVariable(name) { return this.getVariable(name, (variableValue, container) => { container.style.width = variableValue; const val = getComputedStyle(container).width; if (val.match(/px$/)) { return Number.parseFloat(val) * 72.0 / 96.0; } else { return Number.parseFloat(val); } }); } getColorVariable(name) { return this.getVariable(name, (variableValue, container) => { container.style.color = variableValue; let val = getComputedStyle(container).color; val = parseColorCssFunction(val); return rgbaToHex(val); }); } } registerImageUtils(new ImageUtils()); registerVariablesManagerCreator(() => new VariablesManager()); //# sourceMappingURL=survey.pdf.mjs.map