enhanced-printer 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -10,6 +10,5 @@
10
10
  * - Automatic use of printer defaults when settings not specified
11
11
  * - Query job status and printer information
12
12
  */
13
- export { print, getPrinters, getDefaultPrinter, getJobStatus, getPrinterJobs, getJobs, // New method for getting jobs with custom names
14
- cancelJob } from './print-manager';
13
+ export { print, getPrinters, getDefaultPrinter, getJobInfo, getJobs, cancelJob } from './print-manager';
15
14
  export { PrintOptions, PrintResult, PrinterInfo, JobInfo } from './types';
package/lib/index.js CHANGED
@@ -12,12 +12,11 @@
12
12
  * - Query job status and printer information
13
13
  */
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.cancelJob = exports.getJobs = exports.getPrinterJobs = exports.getJobStatus = exports.getDefaultPrinter = exports.getPrinters = exports.print = void 0;
15
+ exports.cancelJob = exports.getJobs = exports.getJobInfo = exports.getDefaultPrinter = exports.getPrinters = exports.print = void 0;
16
16
  var print_manager_1 = require("./print-manager");
17
17
  Object.defineProperty(exports, "print", { enumerable: true, get: function () { return print_manager_1.print; } });
18
18
  Object.defineProperty(exports, "getPrinters", { enumerable: true, get: function () { return print_manager_1.getPrinters; } });
19
19
  Object.defineProperty(exports, "getDefaultPrinter", { enumerable: true, get: function () { return print_manager_1.getDefaultPrinter; } });
20
- Object.defineProperty(exports, "getJobStatus", { enumerable: true, get: function () { return print_manager_1.getJobStatus; } });
21
- Object.defineProperty(exports, "getPrinterJobs", { enumerable: true, get: function () { return print_manager_1.getPrinterJobs; } });
20
+ Object.defineProperty(exports, "getJobInfo", { enumerable: true, get: function () { return print_manager_1.getJobInfo; } });
22
21
  Object.defineProperty(exports, "getJobs", { enumerable: true, get: function () { return print_manager_1.getJobs; } });
23
22
  Object.defineProperty(exports, "cancelJob", { enumerable: true, get: function () { return print_manager_1.cancelJob; } });
@@ -10,6 +10,14 @@ export declare function storeJobName(printerName: string, jobId: number, customN
10
10
  * Get custom job name for a job
11
11
  */
12
12
  export declare function getCustomJobName(printerName: string, jobId: number): string | null;
13
+ /**
14
+ * Get system job ID from a custom job name (reverse lookup)
15
+ * Returns the jobId and printerName if found, null otherwise
16
+ */
17
+ export declare function getSystemJobId(customJobName: string, printerName?: string): {
18
+ jobId: number;
19
+ printerName: string;
20
+ } | null;
13
21
  /**
14
22
  * Clear all job name mappings
15
23
  */
@@ -6,6 +6,7 @@
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.storeJobName = storeJobName;
8
8
  exports.getCustomJobName = getCustomJobName;
9
+ exports.getSystemJobId = getSystemJobId;
9
10
  exports.clearJobNames = clearJobNames;
10
11
  // In-memory storage for job name mappings
11
12
  const jobNameMap = new Map();
@@ -41,6 +42,27 @@ function getCustomJobName(printerName, jobId) {
41
42
  }
42
43
  return mapping.customName;
43
44
  }
45
+ /**
46
+ * Get system job ID from a custom job name (reverse lookup)
47
+ * Returns the jobId and printerName if found, null otherwise
48
+ */
49
+ function getSystemJobId(customJobName, printerName) {
50
+ const now = Date.now();
51
+ for (const [key, mapping] of jobNameMap.entries()) {
52
+ // Skip expired mappings
53
+ if (now - mapping.timestamp > MAX_MAPPING_AGE) {
54
+ continue;
55
+ }
56
+ if (mapping.customName === customJobName) {
57
+ // If printerName is provided, match it too
58
+ if (printerName && mapping.printerName !== printerName) {
59
+ continue;
60
+ }
61
+ return { jobId: mapping.jobId, printerName: mapping.printerName };
62
+ }
63
+ }
64
+ return null;
65
+ }
44
66
  /**
45
67
  * Clean mappings older than MAX_MAPPING_AGE
46
68
  */
@@ -4,9 +4,14 @@ import { JobInfo } from './types';
4
4
  */
5
5
  export declare function getJobIdByName(printerName: string, jobName: string, maxRetries?: number, retryDelay?: number): Promise<number | null>;
6
6
  /**
7
- * Gets detailed information about a print job
7
+ * Gets detailed information about a print job using system job ID
8
8
  */
9
- export declare function getJobInfo(printerName: string, jobId: number): Promise<JobInfo | null>;
9
+ export declare function getJobInfoBySystemId(printerName: string, jobId: number): Promise<JobInfo | null>;
10
+ /**
11
+ * Gets detailed information about a print job using custom job name
12
+ * Resolves the custom job name to the system job ID, then queries the spooler
13
+ */
14
+ export declare function getJobInfo(customJobName: string, printerName?: string): Promise<JobInfo | null>;
10
15
  /**
11
16
  * Gets all print jobs for a printer with enhanced details
12
17
  */
@@ -1,10 +1,53 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getJobIdByName = getJobIdByName;
4
+ exports.getJobInfoBySystemId = getJobInfoBySystemId;
4
5
  exports.getJobInfo = getJobInfo;
5
6
  exports.getAllJobs = getAllJobs;
6
7
  const child_process_1 = require("child_process");
7
8
  const job_name_mapper_1 = require("./job-name-mapper");
9
+ /**
10
+ * Windows print job status flags (bitmask values)
11
+ * See: https://learn.microsoft.com/en-us/windows/win32/printdocs/job-info-2
12
+ */
13
+ const JOB_STATUS_FLAGS = {
14
+ 0x0001: 'Paused',
15
+ 0x0002: 'Error',
16
+ 0x0004: 'Deleting',
17
+ 0x0008: 'Spooling',
18
+ 0x0010: 'Printing',
19
+ 0x0020: 'Offline',
20
+ 0x0040: 'PaperOut',
21
+ 0x0080: 'Printed',
22
+ 0x0100: 'Deleted',
23
+ 0x0200: 'BlockedDeviceQueue',
24
+ 0x0400: 'UserIntervention',
25
+ 0x0800: 'Restart',
26
+ 0x1000: 'Complete',
27
+ 0x2000: 'Retained',
28
+ 0x4000: 'RenderingLocally',
29
+ };
30
+ /**
31
+ * Decodes a numeric job status bitmask into a human-readable string.
32
+ * e.g. 8210 (0x2012) => "Error, Printing, Retained"
33
+ */
34
+ function decodeJobStatus(status) {
35
+ // If it's already a string, return it as-is
36
+ if (typeof status === 'string') {
37
+ return status || 'Unknown';
38
+ }
39
+ const numericStatus = Number(status);
40
+ if (isNaN(numericStatus) || numericStatus === 0) {
41
+ return 'None';
42
+ }
43
+ const flags = [];
44
+ for (const [bit, name] of Object.entries(JOB_STATUS_FLAGS)) {
45
+ if (numericStatus & Number(bit)) {
46
+ flags.push(name);
47
+ }
48
+ }
49
+ return flags.length > 0 ? flags.join(', ') : 'Unknown';
50
+ }
8
51
  /**
9
52
  * Retrieves job ID by document name using PowerShell
10
53
  */
@@ -22,9 +65,9 @@ async function getJobIdByName(printerName, jobName, maxRetries = 3, retryDelay =
22
65
  return null;
23
66
  }
24
67
  /**
25
- * Gets detailed information about a print job
68
+ * Gets detailed information about a print job using system job ID
26
69
  */
27
- async function getJobInfo(printerName, jobId) {
70
+ async function getJobInfoBySystemId(printerName, jobId) {
28
71
  const script = `
29
72
  Get-PrintJob -PrinterName "${escapePowerShellString(printerName)}" |
30
73
  Where-Object {$_.Id -eq ${jobId}} |
@@ -42,9 +85,9 @@ async function getJobInfo(printerName, jobId) {
42
85
  return {
43
86
  jobId: result.Id,
44
87
  jobName: result.DocumentName || '',
45
- customJobName: customName || result.DocumentName || '', // Use custom name if available
88
+ customJobName: customName || result.DocumentName || '',
46
89
  printerName: printerName,
47
- status: result.JobStatus || 'Unknown',
90
+ status: decodeJobStatus(result.JobStatus),
48
91
  pages: result.TotalPages || 0,
49
92
  size: result.Size,
50
93
  userName: result.UserName,
@@ -55,6 +98,18 @@ async function getJobInfo(printerName, jobId) {
55
98
  return null;
56
99
  }
57
100
  }
101
+ /**
102
+ * Gets detailed information about a print job using custom job name
103
+ * Resolves the custom job name to the system job ID, then queries the spooler
104
+ */
105
+ async function getJobInfo(customJobName, printerName) {
106
+ // Resolve custom job name to system job ID
107
+ const mapping = (0, job_name_mapper_1.getSystemJobId)(customJobName, printerName);
108
+ if (!mapping) {
109
+ return null;
110
+ }
111
+ return getJobInfoBySystemId(mapping.printerName, mapping.jobId);
112
+ }
58
113
  /**
59
114
  * Gets all print jobs for a printer with enhanced details
60
115
  */
@@ -77,7 +132,7 @@ async function getAllJobs(printerName) {
77
132
  jobName: job.DocumentName || '',
78
133
  customJobName: customName || job.DocumentName || '', // Use custom name if available
79
134
  printerName: printerName,
80
- status: job.JobStatus || 'Unknown',
135
+ status: decodeJobStatus(job.JobStatus),
81
136
  pages: job.TotalPages || 0,
82
137
  size: job.Size,
83
138
  userName: job.UserName,
@@ -12,19 +12,14 @@ export declare function getPrinters(): Promise<PrinterInfo[]>;
12
12
  */
13
13
  export declare function getDefaultPrinter(): Promise<string | null>;
14
14
  /**
15
- * Gets information about a specific print job
15
+ * Gets detailed information about a print job using custom job name
16
16
  */
17
- export declare function getJobStatus(printerName: string, jobId: number): Promise<JobInfo | null>;
17
+ export declare function getJobInfo(customJobName: string, printerName?: string): Promise<JobInfo | null>;
18
18
  /**
19
19
  * Gets all print jobs for a specific printer
20
20
  */
21
- export declare function getPrinterJobs(printerName: string): Promise<JobInfo[]>;
22
- /**
23
- * Gets all print jobs with custom job names for a specific printer
24
- * This is an alias for getPrinterJobs with clearer intent
25
- */
26
21
  export declare function getJobs(printerName: string): Promise<JobInfo[]>;
27
22
  /**
28
- * Cancels a print job
23
+ * Cancels a print job using custom job name
29
24
  */
30
- export declare function cancelJob(printerName: string, jobId: number): Promise<boolean>;
25
+ export declare function cancelJob(customJobName: string, printerName?: string): Promise<boolean>;
@@ -3,8 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.print = print;
4
4
  exports.getPrinters = getPrinters;
5
5
  exports.getDefaultPrinter = getDefaultPrinter;
6
- exports.getJobStatus = getJobStatus;
7
- exports.getPrinterJobs = getPrinterJobs;
6
+ exports.getJobInfo = getJobInfo;
8
7
  exports.getJobs = getJobs;
9
8
  exports.cancelJob = cancelJob;
10
9
  const pdf_to_printer_1 = require("pdf-to-printer");
@@ -120,30 +119,28 @@ async function getDefaultPrinter() {
120
119
  return defaultPrinter ? defaultPrinter.name : null;
121
120
  }
122
121
  /**
123
- * Gets information about a specific print job
122
+ * Gets detailed information about a print job using custom job name
124
123
  */
125
- async function getJobStatus(printerName, jobId) {
126
- return (0, job_tracker_1.getJobInfo)(printerName, jobId);
124
+ async function getJobInfo(customJobName, printerName) {
125
+ return (0, job_tracker_1.getJobInfo)(customJobName, printerName);
127
126
  }
128
127
  /**
129
128
  * Gets all print jobs for a specific printer
130
129
  */
131
- async function getPrinterJobs(printerName) {
132
- return (0, job_tracker_1.getAllJobs)(printerName);
133
- }
134
- /**
135
- * Gets all print jobs with custom job names for a specific printer
136
- * This is an alias for getPrinterJobs with clearer intent
137
- */
138
130
  async function getJobs(printerName) {
139
131
  return (0, job_tracker_1.getAllJobs)(printerName);
140
132
  }
141
133
  /**
142
- * Cancels a print job
134
+ * Cancels a print job using custom job name
143
135
  */
144
- async function cancelJob(printerName, jobId) {
136
+ async function cancelJob(customJobName, printerName) {
137
+ // Resolve custom job name to system job ID
138
+ const mapping = (0, job_name_mapper_1.getSystemJobId)(customJobName, printerName);
139
+ if (!mapping) {
140
+ return false;
141
+ }
145
142
  const script = `
146
- Remove-PrintJob -PrinterName "${escapePowerShellString(printerName)}" -ID ${jobId}
143
+ Remove-PrintJob -PrinterName "${escapePowerShellString(mapping.printerName)}" -ID ${mapping.jobId}
147
144
  `;
148
145
  try {
149
146
  await executePowerShell(script);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "enhanced-printer",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Advanced PDF printing library with job tracking, custom page sizes, and tray selection for Node.js and Electron",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",