@@ -25,6 +25,8 @@ process.env.PUBLIC = process.env.VITE_DEV_SERVER_URL
2525 ? path . join ( process . env . DIST_ELECTRON , '../public' )
2626 : process . env . DIST ;
2727
28+ const isDev = ! app . isPackaged || process . env . NODE_ENV === 'development' ;
29+
2830// Disable GPU Acceleration for Windows 7
2931if ( release ( ) . startsWith ( '6.1' ) ) app . disableHardwareAcceleration ( ) ;
3032
@@ -63,6 +65,12 @@ app.setLoginItemSettings({
6365 args : [ '--hidden' ]
6466} ) ;
6567
68+ // Global settings for dev and production
69+ if ( ! process . env . VITE_DEV_SERVER_URL ) {
70+ // For production
71+ Menu . setApplicationMenu ( null )
72+ }
73+
6674
6775async function createWindow ( ) {
6876
@@ -104,15 +112,11 @@ async function createWindow() {
104112 const contextMenu = Menu . buildFromTemplate ( [
105113 {
106114 label : 'Show App' ,
107- click : ( ) => {
108- win ?. show ( ) ;
109- } ,
115+ click : ( ) => win ?. show ( ) ,
110116 } ,
111117 {
112- label : 'Quit' ,
113- click : ( ) => {
114- app . quit ( ) ;
115- } ,
118+ label : 'Close App' ,
119+ click : ( ) => app . quit ( ) ,
116120 } ,
117121 ] ) ;
118122 tray . setToolTip ( 'MotionPrinter' ) ;
@@ -528,12 +532,10 @@ ipcMain.on("printFile", async (event, file: any) => {
528532} )
529533
530534
531- ipcMain . on ( 'generate-pdf' , async ( event , obj : { htmlContent : string , isPrint ?: boolean , printPDF ?: boolean , filename ?: string , } ) => {
532-
533- console . log ( 'generate-pdf' )
535+ ipcMain . on ( 'generate-pdf' , async ( event , obj : { htmlContent : string , head : string , isPrint ?: boolean , printPDF ?: boolean , filename ?: string } ) => {
534536
535537 const win = new BrowserWindow ( {
536- show : false ,
538+ show : isDev ,
537539 webPreferences : {
538540 nodeIntegration : true ,
539541 contextIsolation : false ,
@@ -542,11 +544,42 @@ ipcMain.on('generate-pdf', async (event, obj: { htmlContent: string, isPrint?: b
542544
543545 try {
544546
545- await win . loadURL ( `data:text/html;charset=UTF-8,${ encodeURIComponent ( obj . htmlContent ) } ` ) ;
547+ let cssFiles : string [ ] = [ ]
548+
549+ if ( ! isDev ) {
550+ console . log ( "Running in production mode" ) ;
551+ const cssDir = path . join ( process . resourcesPath , 'app.asar.unpacked/dist/assets' ) ;
552+ const files = fs . readdirSync ( cssDir ) ;
553+
554+ // Read all .css files
555+ cssFiles = files
556+ . filter ( file => file . endsWith ( '.css' ) )
557+ . map ( file => fs . readFileSync ( path . join ( cssDir , file ) , 'utf8' ) ) ;
558+
559+ }
560+
561+ const htmlContent = `
562+ <!DOCTYPE html>
563+ <html>
564+ <head>
565+ ${ cssFiles . map ( ( el , i ) => `<style type="text/css" id="inserted-${ i } ">${ el } </style>` ) . join ( '\n' ) }
566+ ${ obj . head }
567+ </head>
568+ <body>
569+ ${ obj . htmlContent }
570+ </body>
571+ </html>
572+ ` ;
573+
574+ await win . loadURL ( `data:text/html;charset=UTF-8,${ encodeURIComponent ( htmlContent ) } ` ) ;
575+
576+ await sleep ( 500 ) ;
577+
546578
547579 if ( obj ?. printPDF ) {
548580 console . log ( 'Printing to PDF...' )
549581 const pdfOptions = {
582+ color : true ,
550583 margins : {
551584 marginType : 'printableArea' as "printableArea" , // 'none', 'printableArea', or custom
552585 top : 0 ,
@@ -601,4 +634,17 @@ ipcMain.on('generate-pdf', async (event, obj: { htmlContent: string, isPrint?: b
601634 console . error ( error )
602635 }
603636
637+ } ) ;
638+
639+ const fsPromise = require ( 'fs' ) . promises ;
640+ ipcMain . handle ( 'read-css-file' , async ( event , fileUrl ) => {
641+ try {
642+ // Convert file:// URL to filesystem path
643+ const filePath = fileUrl . replace ( 'file://' , '' ) ;
644+ // Read the CSS file from app.asar
645+ const content = await fsPromise . readFile ( filePath , 'utf-8' ) ;
646+ return content ;
647+ } catch ( error : any ) {
648+ throw new Error ( `Failed to read CSS file: ${ error . message } ` ) ;
649+ }
604650} ) ;
0 commit comments