pdfile
Version:
PDFile is a Node.js library for generating high-quality, dynamic PDFs using Handlebars templates and Puppeteer. It supports multi-page PDFs and offers full customization. Perfect for web developers, it enables easy creation of multi-page PDFs by simply de
3 lines (2 loc) • 13.2 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t,r=require("puppeteer"),n=(e=require("handlebars"))&&"object"==typeof e&&"default"in e?e.default:e,o=require("fs"),a=require("stream");function i(e){var t,r,n,o=2;for("undefined"!=typeof Symbol&&(r=Symbol.asyncIterator,n=Symbol.iterator);o--;){if(r&&null!=(t=e[r]))return t.call(e);if(n&&null!=(t=e[n]))return new u(t.call(e));r="@@asyncIterator",n="@@iterator"}throw new TypeError("Object is not async iterable")}function u(e){function t(e){if(Object(e)!==e)return Promise.reject(new TypeError(e+" is not an object."));var t=e.done;return Promise.resolve(e.value).then((function(e){return{value:e,done:t}}))}return(u=function(e){this.s=e,this.n=e.next}).prototype={s:null,n:null,next:function(){return t(this.n.apply(this.s,arguments))},return:function(e){var r=this.s.return;return void 0===r?Promise.resolve({value:e,done:!0}):t(r.apply(this.s,arguments))},throw:function(e){var r=this.s.return;return void 0===r?Promise.reject(e):t(r.apply(this.s,arguments))}},new u(e)}function c(){c=function(){return t};var e,t={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(e,t,r){e[t]=r.value},a="function"==typeof Symbol?Symbol:{},i=a.iterator||"@@iterator",u=a.asyncIterator||"@@asyncIterator",s=a.toStringTag||"@@toStringTag";function f(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{f({},"")}catch(e){f=function(e,t,r){return e[t]=r}}function l(e,t,r,n){var a=Object.create((t&&t.prototype instanceof m?t:m).prototype),i=new F(n||[]);return o(a,"_invoke",{value:j(e,r,i)}),a}function h(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}t.wrap=l;var p="suspendedStart",v="executing",d="completed",y={};function m(){}function g(){}function w(){}var b={};f(b,i,(function(){return this}));var x=Object.getPrototypeOf,k=x&&x(x(N([])));k&&k!==r&&n.call(k,i)&&(b=k);var E=w.prototype=m.prototype=Object.create(b);function P(e){["next","throw","return"].forEach((function(t){f(e,t,(function(e){return this._invoke(t,e)}))}))}function L(e,t){function r(o,a,i,u){var c=h(e[o],e,a);if("throw"!==c.type){var s=c.arg,f=s.value;return f&&"object"==typeof f&&n.call(f,"__await")?t.resolve(f.__await).then((function(e){r("next",e,i,u)}),(function(e){r("throw",e,i,u)})):t.resolve(f).then((function(e){s.value=e,i(s)}),(function(e){return r("throw",e,i,u)}))}u(c.arg)}var a;o(this,"_invoke",{value:function(e,n){function o(){return new t((function(t,o){r(e,n,t,o)}))}return a=a?a.then(o,o):o()}})}function j(t,r,n){var o=p;return function(a,i){if(o===v)throw Error("Generator is already running");if(o===d){if("throw"===a)throw i;return{value:e,done:!0}}for(n.method=a,n.arg=i;;){var u=n.delegate;if(u){var c=O(u,n);if(c){if(c===y)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===p)throw o=d,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=v;var s=h(t,r,n);if("normal"===s.type){if(o=n.done?d:"suspendedYield",s.arg===y)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(o=d,n.method="throw",n.arg=s.arg)}}}function O(t,r){var n=r.method,o=t.iterator[n];if(o===e)return r.delegate=null,"throw"===n&&t.iterator.return&&(r.method="return",r.arg=e,O(t,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var a=h(o,t.iterator,r.arg);if("throw"===a.type)return r.method="throw",r.arg=a.arg,r.delegate=null,y;var i=a.arg;return i?i.done?(r[t.resultName]=i.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,y):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function S(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function _(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function F(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(S,this),this.reset(!0)}function N(t){if(t||""===t){var r=t[i];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,a=function r(){for(;++o<t.length;)if(n.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return a.next=a}}throw new TypeError(typeof t+" is not iterable")}return g.prototype=w,o(E,"constructor",{value:w,configurable:!0}),o(w,"constructor",{value:g,configurable:!0}),g.displayName=f(w,s,"GeneratorFunction"),t.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===g||"GeneratorFunction"===(t.displayName||t.name))},t.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,w):(e.__proto__=w,f(e,s,"GeneratorFunction")),e.prototype=Object.create(E),e},t.awrap=function(e){return{__await:e}},P(L.prototype),f(L.prototype,u,(function(){return this})),t.AsyncIterator=L,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new L(l(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},P(E),f(E,s,"Generator"),f(E,i,(function(){return this})),f(E,"toString",(function(){return"[object Generator]"})),t.keys=function(e){var t=Object(e),r=[];for(var n in t)r.push(n);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=N,F.prototype={constructor:F,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(_),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function o(n,o){return u.type="throw",u.arg=t,r.next=n,o&&(r.method="next",r.arg=e),!!o}for(var a=this.tryEntries.length-1;a>=0;--a){var i=this.tryEntries[a],u=i.completion;if("root"===i.tryLoc)return o("end");if(i.tryLoc<=this.prev){var c=n.call(i,"catchLoc"),s=n.call(i,"finallyLoc");if(c&&s){if(this.prev<i.catchLoc)return o(i.catchLoc,!0);if(this.prev<i.finallyLoc)return o(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return o(i.catchLoc,!0)}else{if(!s)throw Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return o(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=t&&t<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=t,a?(this.method="next",this.next=a.finallyLoc,y):this.complete(i)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),y},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),_(r),y}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;_(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:N(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),y}},t}function s(e,t,r,n,o,a,i){try{var u=e[a](i),c=u.value}catch(e){return void r(e)}u.done?t(c):Promise.resolve(c).then(n,o)}function f(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var a=e.apply(t,r);function i(e){s(a,n,o,i,u,"next",e)}function u(e){s(a,n,o,i,u,"throw",e)}i(void 0)}))}}function l(){return(l=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var h=function(){var e=f(c().mark((function e(n){return c().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t){e.next=4;break}return e.next=3,r.launch(l({headless:!0,args:["--no-sandbox","--disable-setuid-sandbox","--disable-dev-shm-usage"]},n));case 3:t=e.sent;case 4:return e.abrupt("return",t);case 5:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),p=function(){var e=f(c().mark((function e(){return c().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!t){e.next=4;break}return e.next=3,t.close();case 3:t=null;case 4:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),v=function(e,t){if(n.registerHelper("ifCond",(function(t,r,n,o){switch(r){case"==":return t==n?o.fn(e):o.inverse(e);case"===":return t===n?o.fn(e):o.inverse(e);case"!=":return t!=n?o.fn(e):o.inverse(e);case"!==":return t!==n?o.fn(e):o.inverse(e);case"<":return t<n?o.fn(e):o.inverse(e);case"<=":return t<=n?o.fn(e):o.inverse(e);case">":return t>n?o.fn(e):o.inverse(e);case">=":return t>=n?o.fn(e):o.inverse(e);case"&&":return t&&n?o.fn(e):o.inverse(e);case"||":return t||n?o.fn(e):o.inverse(e);default:return o.inverse(o)}})),t){var r=JSON.parse(t);Object.entries(r).forEach((function(e){var t=e[0],r=new Function("return ("+e[1]+")")();n.registerHelper(t,r)}))}else n.registerHelper({eq:function(e,t){return e===t},ne:function(e,t){return e!==t},lt:function(e,t){return e<t},gt:function(e,t){return e>t},lte:function(e,t){return e<=t},gte:function(e,t){return e>=t},and:function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return t.every(Boolean)},or:function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return t.slice(0,-1).some(Boolean)}})},d=function(){var e=f(c().mark((function e(t){var r,u,s,f,p,d,y,m,g,w,b,x,k,E,P,L,j,O,S,_,F,N,G,T,A,B,q,I,D;return c().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.templateFilePath,u=t.dataPerPage,f=void 0===(s=t.pdfFilePath)?void 0:s,d=void 0!==(p=t.useStream)&&p,y=t.helpersFilePath,m=t.puppeteerOptions,g=t.pdfOptions,w=void 0,e.prev=2,f||d){e.next=5;break}throw new Error('"pdfFilePath" must be specified unless "useStream" is set to true.');case 5:if(!f||!d){e.next=7;break}throw new Error('Conflicting parameters: "pdfFilePath" and "useStream" cannot both be used simultaneously.');case 7:if("[]"!==JSON.stringify(u)){e.next=9;break}throw new Error("No data was passed to inject into PDF");case 9:return e.next=11,h(m);case 11:if(b=e.sent,!y){e.next=47;break}x=o.createReadStream(y,{encoding:"utf8"}),k="",E=!1,P=!1,e.prev=17,j=i(x);case 19:return e.next=21,j.next();case 21:if(!(E=!(O=e.sent).done)){e.next=27;break}k+=O.value;case 24:E=!1,e.next=19;break;case 27:e.next=33;break;case 29:e.prev=29,e.t0=e.catch(17),P=!0,L=e.t0;case 33:if(e.prev=33,e.prev=34,!E||null==j.return){e.next=38;break}return e.next=38,j.return();case 38:if(e.prev=38,!P){e.next=41;break}throw L;case 41:return e.finish(38);case 42:return e.finish(33);case 43:x.close(),v(u,k),e.next=48;break;case 47:v(u);case 48:S=o.createReadStream(r,{encoding:"utf8"}),_="",F=!1,N=!1,e.prev=52,T=i(S);case 54:return e.next=56,T.next();case 56:if(!(F=!(A=e.sent).done)){e.next=62;break}_+=A.value;case 59:F=!1,e.next=54;break;case 62:e.next=68;break;case 64:e.prev=64,e.t1=e.catch(52),N=!0,G=e.t1;case 68:if(e.prev=68,e.prev=69,!F||null==T.return){e.next=73;break}return e.next=73,T.return();case 73:if(e.prev=73,!N){e.next=76;break}throw G;case 76:return e.finish(73);case 77:return e.finish(68);case 78:return S.close(),B=n.compile(_),q=l({format:"A4",margin:{left:"10mm",top:"10mm",right:"10mm",bottom:"10mm"},printBackground:!0},g),e.next=83,b.newPage();case 83:return w=e.sent,I=u.map((function(e){return B(e)})).join(""),e.next=87,w.setContent(I,{waitUntil:"networkidle0"});case 87:return e.next=89,w.evaluateHandle("document.fonts.ready");case 89:if(!d){e.next=96;break}return e.next=92,w.pdf(q);case 92:return e.abrupt("return",a.Readable.from(e.sent));case 96:if(f){e.next=98;break}throw new Error("PDF file path must be provided.");case 98:return e.next=100,w.pdf(l({format:"A4",margin:{left:"10mm",top:"10mm",right:"10mm",bottom:"10mm"},printBackground:!0},g));case 100:return D=e.sent,e.next=103,new Promise((function(e,t){var r=o.createWriteStream(f);r.write(D),r.end(),r.on("finish",(function(){return e()})),r.on("error",(function(e){return t(e)}))}));case 103:return e.abrupt("return",f);case 104:e.next=112;break;case 106:if(e.prev=106,e.t2=e.catch(2),console.log(e.t2),!(e.t2 instanceof Error)){e.next=111;break}throw new Error("PDF generation failed: "+e.t2.message);case 111:throw new Error("An unknown error occurred during PDF generation.");case 112:if(e.prev=112,!w){e.next=116;break}return e.next=116,w.close();case 116:return e.finish(112);case 117:case"end":return e.stop()}}),e,null,[[2,106,112,117],[17,29,33,43],[34,,38,42],[52,64,68,78],[69,,73,77]])})));return function(t){return e.apply(this,arguments)}}();exports.closeBrowser=p,exports.createBrowser=h,exports.generatePdf=d;
//# sourceMappingURL=pdfile.cjs.production.min.js.map