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.
6 lines (4 loc) • 5.92 kB
JavaScript
/*!
* For license information please see pdf-form-filler.min.js.LICENSE.txt
*/
(function(d,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(d=typeof globalThis<"u"?globalThis:d||self,h(d.PDFFormFiller={}))})(this,(function(d){"use strict";var h=function(a,t){return h=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,n){e.__proto__=n}||function(e,n){for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])},h(a,t)};function m(a,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");h(a,t);function e(){this.constructor=a}a.prototype=t===null?Object.create(t):(e.prototype=t.prototype,new e)}function y(a,t,e,n){function i(r){return r instanceof e?r:new e(function(u){u(r)})}return new(e||(e=Promise))(function(r,u){function c(s){try{o(n.next(s))}catch(f){u(f)}}function l(s){try{o(n.throw(s))}catch(f){u(f)}}function o(s){s.done?r(s.value):i(s.value).then(c,l)}o((n=n.apply(a,t||[])).next())})}function v(a,t){var e={label:0,sent:function(){if(r[0]&1)throw r[1];return r[1]},trys:[],ops:[]},n,i,r,u=Object.create((typeof Iterator=="function"?Iterator:Object).prototype);return u.next=c(0),u.throw=c(1),u.return=c(2),typeof Symbol=="function"&&(u[Symbol.iterator]=function(){return this}),u;function c(o){return function(s){return l([o,s])}}function l(o){if(n)throw new TypeError("Generator is already executing.");for(;u&&(u=0,o[0]&&(e=0)),e;)try{if(n=1,i&&(r=o[0]&2?i.return:o[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,o[1])).done)return r;switch(i=0,r&&(o=[o[0]&2,r.value]),o[0]){case 0:case 1:r=o;break;case 4:return e.label++,{value:o[1],done:!1};case 5:e.label++,i=o[1],o=[0];continue;case 7:o=e.ops.pop(),e.trys.pop();continue;default:if(r=e.trys,!(r=r.length>0&&r[r.length-1])&&(o[0]===6||o[0]===2)){e=0;continue}if(o[0]===3&&(!r||o[1]>r[0]&&o[1]<r[3])){e.label=o[1];break}if(o[0]===6&&e.label<r[1]){e.label=r[1],r=o;break}if(r&&e.label<r[2]){e.label=r[2],e.ops.push(o);break}r[2]&&e.ops.pop(),e.trys.pop();continue}o=t.call(a,e)}catch(s){o=[6,s],i=0}finally{n=r=0}if(o[0]&5)throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}}typeof SuppressedError=="function"&&SuppressedError;var D=(function(){function a(t){this.map=t,this.res={}}return a.prototype.mapDataRecursive=function(t,e){var n=this;if(t!=null){if(typeof t!="object"){if(Array.isArray(e)){e.forEach(function(i){n.mapDataRecursive(t,i)});return}typeof e!="object"?this.res[e]=t:this.res[e[t].field]=e[t].value;return}if(Array.isArray(t)){t.forEach(function(i,r){n.mapDataRecursive(i,Array.isArray(e)?e[r]:e)});return}Object.keys(t).forEach(function(i){n.mapDataRecursive(t[i],e[i])})}},a.prototype.mapData=function(t){return this.res={},this.mapDataRecursive(t,this.map),this.res},a})(),w=(function(){function a(t){t&&(this.data=t.data,this.fieldMap=t.fieldMap,this.pdfTemplate=t.pdfTemplate,this.pdfLibraryAdapter=t.pdfLibraryAdapter)}return a.prototype.getPDFBytes=function(){return y(this,void 0,void 0,function(){var t,e,n;return v(this,function(i){switch(i.label){case 0:return t=new D(this.fieldMap),e=t.mapData(this.data),[4,(n=this.pdfLibraryAdapter)===null||n===void 0?void 0:n.fillForm(this.pdfTemplate,e)];case 1:return[2,i.sent()]}})})},a.prototype.fromCharCode=function(t){for(var e=[],n=65535,i=0;i<t.length;i+=n){var r=Array.prototype.slice.call(t,i,i+n);e.push(String.fromCharCode.apply(null,r))}return e.join("")},a.prototype.raw=function(t){return y(this,void 0,void 0,function(){var e,n;return v(this,function(i){switch(i.label){case 0:return[4,this.getPDFBytes()];case 1:return e=i.sent(),!t||!e?[2,e]:t=="dataurlstring"?[2,"data:application/pdf;base64,"+btoa(this.fromCharCode(e))]:(n=new Blob([e],{type:"application/pdf"}),t=="blob"?[2,n]:t=="bloburl"?[2,URL.createObjectURL(n)]:[2,e])}})})},a.prototype.save=function(){return y(this,arguments,void 0,function(t){var e;return t===void 0&&(t="FilledForm.pdf"),v(this,function(n){switch(n.label){case 0:return[4,this.getPDFBytes()];case 1:return e=n.sent(),e?[4,this.saveToFile(e,t)]:[2];case 2:return n.sent(),[2]}})})},a})(),P=(function(a){m(t,a);function t(){return a!==null&&a.apply(this,arguments)||this}return t.prototype.saveToFile=function(e,n){return y(this,void 0,void 0,function(){var i,r;return v(this,function(u){return i=new Blob([e],{type:"application/pdf"}),r=document.createElement("a"),r.href=URL.createObjectURL(i),r.download=n,document.body.appendChild(r),r.click(),document.body.removeChild(r),URL.revokeObjectURL(r.href),[2]})})},t})(w),g=(function(){function a(t){this.pdfLibrary=t}return a.prototype.fillForm=function(t,e){return y(this,void 0,void 0,function(){var n,i,r,u,c,l,o,s,f;return v(this,function(b){switch(b.label){case 0:return n=this.pdfLibrary,i=n.PDFDocument,r=n.PDFTextField,u=n.PDFCheckBox,c=n.PDFRadioGroup,l=n.PDFDropdown,[4,i.load(t)];case 1:return o=b.sent(),s=o.getForm(),f=s.getFields(),f.forEach(function(p){var L=p.getName(),F=e[L];F!=null&&(p instanceof r?p.setText(F):p instanceof u?F?p.check():p.uncheck():(p instanceof c||p instanceof l)&&p.select(F.toString()))}),[4,o.save()];case 2:return[2,b.sent()]}})})},a})(),A=(function(){function a(t){this.pdfLibrary=t}return a.prototype.fillForm=function(t,e){return y(this,void 0,void 0,function(){var n,i,r,u,c,l;return v(this,function(o){switch(o.label){case 0:return n=this.pdfLibrary,[4,n.getDocument(t).promise];case 1:i=o.sent(),r=i.numPages,u=1,o.label=2;case 2:return u<=r?[4,i.getPage(u)]:[3,6];case 3:return c=o.sent(),[4,c.getAnnotations()];case 4:l=o.sent(),l.forEach(function(s){if(s.fieldType!=null){var f=e[s.fieldName];if(f){if(s.radioButton&&s.buttonValue!=f)return;i.annotationStorage.setValue(s.id,{value:f})}}}),o.label=5;case 5:return u++,[3,2];case 6:return[4,i.saveDocument()];case 7:return[2,o.sent()]}})})},a})();d.PDFFormFiller=P,d.PDFJSAdapter=A,d.PDFLibAdapter=g}));