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