|
@@ -1,11 +1,24 @@
|
|
|
+import { app } from 'electron'
|
|
|
+import { writeFileSync, unlinkSync } from 'fs'
|
|
|
+import { spawnSync } from 'child_process'
|
|
|
+import Path from 'path'
|
|
|
+import { dirSync } from 'tmp'
|
|
|
import UUID from 'uuid/v4'
|
|
|
-import { printDirect, getPrinters, getDefaultPrinterName, getJob, getSupportedPrintFormats } from 'printer'
|
|
|
+import {
|
|
|
+ printDirect,
|
|
|
+ getPrinters,
|
|
|
+ getDefaultPrinterName,
|
|
|
+ getJob,
|
|
|
+ getSupportedPrintFormats
|
|
|
+} from 'printer'
|
|
|
+import { readSettings } from './settings'
|
|
|
|
|
|
const REQUEST_PRINTER_NAME = 'request_printer_name'
|
|
|
const SHOW_PRINT_STATUS = 'show_print_status'
|
|
|
const DOWNLOAD_DATA = 'download_data'
|
|
|
|
|
|
const printQueue = []
|
|
|
+const settings = readSettings()
|
|
|
|
|
|
/**
|
|
|
*
|
|
@@ -56,6 +69,29 @@ const getFormat = mimeType => {
|
|
|
return 'RAW'
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ */
|
|
|
+const redirectToSpawn = (buffer, printer) => {
|
|
|
+ const fileId = UUID()
|
|
|
+ const tmpDir = dirSync()
|
|
|
+ const tmpPath = Path.join(tmpDir.name, `${fileId}.pdf`)
|
|
|
+
|
|
|
+ writeFileSync(tmpPath, buffer)
|
|
|
+
|
|
|
+ const execBin = Path.join(app.getAppPath(), 'bin', 'SumatraPDF.exe');
|
|
|
+
|
|
|
+ spawnSync(execBin, [
|
|
|
+ '-print-to',
|
|
|
+ printer,
|
|
|
+ '-silent',
|
|
|
+ tmpPath
|
|
|
+ ])
|
|
|
+
|
|
|
+ unlinkSync(tmpPath)
|
|
|
+ tmpDir.removeCallback()
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
* @param {*} socket
|
|
@@ -104,8 +140,12 @@ const doPrint = (socket, request) => {
|
|
|
}
|
|
|
|
|
|
let format = getFormat(mimeTypeRegex[0])
|
|
|
+ let supportedFormats = getSupportedPrintFormats(request.printer)
|
|
|
|
|
|
- const supportedFormats = getSupportedPrintFormats(request.printer)
|
|
|
+ if (settings.print.alwaysPdf) {
|
|
|
+ format = 'PDF'
|
|
|
+ supportedFormats.push('PDF')
|
|
|
+ }
|
|
|
|
|
|
if (!supportedFormats.includes(format)) {
|
|
|
socket.emit(SHOW_PRINT_STATUS, {
|
|
@@ -122,6 +162,17 @@ const doPrint = (socket, request) => {
|
|
|
|
|
|
let buffer = Buffer.from(data, 'base64')
|
|
|
|
|
|
+ if (process.platform == 'win32') {
|
|
|
+ redirectToSpawn(buffer, request.printer)
|
|
|
+
|
|
|
+ socket.emit(SHOW_PRINT_STATUS, {
|
|
|
+ status: 'printing',
|
|
|
+ printer: request.printer
|
|
|
+ })
|
|
|
+
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
printDirect({
|
|
|
data: buffer,
|
|
|
printer: request.printer,
|