Skip to content

Commit 885c26e

Browse files
committed
fix print client on windows
1 parent ec37ea1 commit 885c26e

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

packages/server/client/printer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ let timer = null;
1616
export async function ConvertCodeToPDF(code, lang, filename, team, location) {
1717
compiler ||= await createTypstCompiler();
1818
const typst = generateTypst(team, location, filename, lang);
19-
compiler.addSource(path.resolve(process.cwd(), 'main.typst'), typst);
20-
compiler.addSource(path.resolve(process.cwd(), filename), code);
19+
compiler.addSource('/main.typst', typst);
20+
compiler.addSource(`/${filename}`, code);
2121
const docs = await compiler.compile({
2222
format: 'pdf',
23-
mainFilePath: path.resolve(process.cwd(), 'main.typst'),
23+
mainFilePath: '/main.typst',
2424
});
2525
logger.info(`Convert ${filename} to PDF`);
2626
return docs;
@@ -32,7 +32,7 @@ export async function printFile(doc) {
3232
} = doc;
3333
try {
3434
const docs = await ConvertCodeToPDF(code || 'empty file', lang, filename, team, location);
35-
fs.writeFileSync(path.resolve(process.cwd(), `data/${tid}#${_id}.pdf`), docs);
35+
fs.writeFileSync(path.resolve(process.cwd(), `data${path.sep}${tid}#${_id}.pdf`), docs);
3636
if (config.printers.length) {
3737
// eslint-disable-next-line no-constant-condition
3838
while (true) {

packages/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"dependencies": {
1818
"@freedom_sky/esc-pos-encoder": "^5.8.0",
1919
"@hydrooj/framework": "0.0.3",
20-
"@myriaddreamin/typst-ts-web-compiler": "^0.4.1",
20+
"@myriaddreamin/typst-ts-web-compiler": "^0.5.0-rc4",
2121
"@myteril/node-win-printer": "^1.1.5",
2222
"base16384": "^1.0.0",
2323
"cordis": "3.16.0",

packages/server/utils/printers.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import winPrint from '@myteril/node-win-printer';
3+
import { getPrinters as wingetPrinters, PDFPrinter as WinPDFPrint } from '@myteril/node-win-printer';
44
import { getPrinters as unixgetPrinters, print as unixPrint } from 'unix-print';
5+
import { Logger } from '.';
56

6-
let winPrinter: winPrint.PDFPrinter;
7+
const logger = new Logger('printer');
8+
9+
let winPrinter: WinPDFPrint;
710

811
export interface Printer {
912
printer: string;
@@ -14,6 +17,7 @@ export interface Printer {
1417
}
1518

1619
export function initWinPrinter() {
20+
if (winPrinter) return;
1721
const execPath = [
1822
'./SumatraPDF.exe',
1923
path.resolve(__dirname, 'SumatraPDF.exe'),
@@ -23,10 +27,11 @@ export function initWinPrinter() {
2327
];
2428
const sumatraPdfPath = execPath.find((p) => fs.existsSync(p));
2529
if (!sumatraPdfPath) {
26-
throw new Error(`SumatraPDF not found, please install it on https://www.sumatrapdfreader.org/download-free-pdf-viewer,
27-
or direct download from https://www.sumatrapdfreader.org/dl/rel/3.1.2/SumatraPDF-3.1.2.zip`);
30+
// eslint-disable-next-line max-len
31+
throw new Error('SumatraPDF not found, please install it on https://www.sumatrapdfreader.org/download-free-pdf-viewer, or direct download from https://www.sumatrapdfreader.org/dl/rel/3.1.2/SumatraPDF-3.1.2.zip');
2832
}
29-
winPrinter = new winPrint.PDFPrinter({
33+
logger.info(`SumatraPDF found at ${sumatraPdfPath}`);
34+
winPrinter = new WinPDFPrint({
3035
sumatraPdfPath,
3136
});
3237
}
@@ -38,23 +43,23 @@ const windowsPrinterStatus = {
3843

3944
export async function getPrinters(): Promise<Printer[]> {
4045
if (process.platform === 'win32') {
41-
const winprinters = await winPrint.getPrinters();
42-
return winprinters.map((p: any) => ({
43-
printer: p.DriverName,
46+
const winprinters = await wingetPrinters();
47+
return winprinters.filter((p: any) => p.DeviceID).map((p: any) => ({
48+
printer: p.DeviceID,
4449
description: p.Caption,
4550
status: windowsPrinterStatus[p.PrinterStatus] ? windowsPrinterStatus[p.PrinterStatus] : 'unknown',
4651
}));
4752
}
4853
return await unixgetPrinters();
4954
}
5055

51-
export async function print(printer: string, file: string, startPage?: number, endPage?: number) {
56+
export async function print(file: string, printer: string, startPage?: number, endPage?: number) {
5257
if (process.platform === 'win32') {
5358
return winPrinter.print({
5459
file,
5560
printer,
5661
pages: startPage && endPage ? [{ start: startPage, end: endPage }] : undefined,
5762
});
5863
}
59-
return unixPrint(printer, file, startPage && endPage ? ['-P', `${startPage}-${endPage}`] : []);
64+
return unixPrint(file, printer, startPage && endPage ? ['-P', `${startPage}-${endPage}`] : []);
6065
}

0 commit comments

Comments
 (0)