pdf-to-printer2
Version:
A utility to print PDF files from Node.js and Electron. Forked from https://github.com/artiebits/pdf-to-printer
1 lines • 4.81 kB
JavaScript
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var n in r)("object"==typeof exports?exports:t)[n]=r[n]}}(global,(function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)r.d(n,i,function(e){return t[e]}.bind(null,i));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=5)}([function(t,e,r){"use strict";const{exec:n}=r(4),i=r(8).promisify(n);t.exports=i},function(t,e,r){"use strict";const{execFile:n}=r(4);t.exports=(t,e=[],r)=>new Promise((i,o)=>{n(t,e,(t,e)=>{t?o(t):i(r?r(e):e)})})},function(t,e){t.exports=require("fs")},function(t,e){t.exports=function(t){return String(t).match(/\\?.|^$/g).reduce((t,e)=>('"'===e?t.quote^=1:t.quote||" "!==e?t.a[t.a.length-1]+=e.replace(/\\(.)/,"$1"):t.a.push(""),t),{a:[""]}).a}},function(t,e){t.exports=require("child_process")},function(t,e,r){"use strict";switch(r(6).platform()){case"darwin":case"linux":case"MacIntel":case"macintel":t.exports={print:r(7),getPrinters:r(9),getDefaultPrinter:r(10)};break;case"win32":t.exports={print:r(11),getPrinters:r(14),getDefaultPrinter:r(15)};break;default:!function(){throw new Error("Platform not supported")}()}},function(t,e){t.exports=require("os")},function(t,e,r){"use strict";const n=r(2),i=r(3),o=r(0);t.exports=async function(t,e={}){if(!t)throw"No PDF specified";if("string"!=typeof t)throw"Invalid PDF name";if(!n.existsSync(t))throw"No such file";const r=[t],{printer:s,unix:c}=e;if(s&&r.push("-d",s),c){if(!Array.isArray(c))throw"options.unix should be an array";c.map(i).reduce((t,e)=>t.concat(e),[]).forEach(t=>r.push(t))}return o("lp "+r.join(" "))}},function(t,e){t.exports=require("util")},function(t,e,r){"use strict";const n=r(0);t.exports=async function(){try{const{stdout:t}=await n("lpstat -lp");return function(t){return t.split(/^printer(.)/gm).filter(t=>""!==t.trim()).map(t=>{const e=t.trim();return{deviceId:e.substr(0,e.indexOf(" ")),name:e.split("\n").slice(1).find(t=>-1!==t.indexOf("Description")).split(":")[1].trim()}})}(t)}catch(t){throw t}}},function(t,e,r){"use strict";const n=r(0);t.exports=async function(){try{const{stdout:t}=await n("lpstat -d"),e=function(t){const e=t.indexOf(":");return-1===e?null:t.substr(e+1).trim()}(t);return e?async function(t){const{stdout:e}=await n("lpstat -lp "+t),r=function(t){return t.split("\n").slice(1).find(t=>-1!==t.indexOf("Description")).split(":")[1].trim()}(e);return{deviceId:t,name:r}}(e):null}catch(t){throw t}}},function(t,e,r){"use strict";const n=r(12),i=r(2),o=r(3),s=r(1),{fixPathForAsarUnpack:c}=r(13),u=["-print-to","-print-to-default","-print-dialog"];t.exports=(t,e={})=>{if(!t)throw"No PDF specified";if("string"!=typeof t)throw"Invalid PDF name";if(!i.existsSync(t))throw"No such file";let r=e.sumatraPdfPath||n.join(__dirname,"SumatraPDF.exe");e.sumatraPdfPath||(r=c(r));const a=[],{printer:p,win32:f}=e;if(f){if(!Array.isArray(f))throw"options.win32 should be an array";f.map(o).reduce((t,e)=>t.concat(e),[]).forEach(t=>a.push(t))}let l=!1;return a.some(t=>!!(u.indexOf(t)>-1)&&(l=!0,!0)),l||(p?a.push("-print-to",p):a.push("-print-to-default"),a.push("-silent")),a.push(t),s(r,a)}},function(t,e){t.exports=require("path")},function(t,e,r){"use strict";const n="electron"in process.versions&&process.mainModule&&process.mainModule.filename.includes("app.asar");e.fixPathForAsarUnpack=t=>n?t.replace("app.asar","app.asar.unpacked"):t},function(t,e,r){"use strict";const n=r(1);t.exports=()=>n("wmic",["printer","get","deviceid,name"],t=>{const e=t.trim().split(/[\r\n]+/).map(t=>t.trim().split(/\s{2,}/)),r=e[0].reduce((t,e,r)=>(t[e]=r,t),{});return e.slice(1).map(t=>({deviceId:t[r.DeviceID],name:t[r.Name]}))})},function(t,e,r){"use strict";const n=r(1);t.exports=()=>n("wmic",["printer","get","default,deviceid,name"],t=>{const e=t.trim().split(/[\r\n]+/).map(t=>t.trim().split(/\s{2,}/)),r=e[0].reduce((t,e,r)=>(t[e]=r,t),{}),n=e.slice(1).find(t=>"TRUE"===t[r.Default]);return!!n&&{deviceId:n[r.DeviceID],name:n[r.Name]}})}])}));