Title: Issue with Saving PDF Files in Electron App After Build
Body of the Question: Hey everyone, hope you're doing well!
I'm working on an Electron app and everything was great while I was running it through npm run start. I didn't encounter any issues until I began building the app, in both dev and non-dev modes. I'm unable to save anything to the path chosen by the user. I managed to save a file some time ago (can't recall what changes I made), but now, the saved PDF is corrupted and completely empty. Considering this, I moved the PDF saving code from my function into the main process as a buffer. Unfortunately, I encountered the same error again.
Running with npm run start without compiling = Everything works fine, no errors.
I've been trying for hours to find a solution. Thanks for your help!
const { createInvoice } = require(path.join(__dirname, 'createInvoice.js'));
ipcMain.on('submitInvoiceForm', async (event, formData) => {
const desktopPath = path.join(os.homedir(), 'Desktop'); // Path to the user's Desktop
const defaultFilePath = path.join(desktopPath,`Quote_${formData.invoiceNumber}-${formData.clientName}.pdf`);
const { filePath } = await dialog.showSaveDialog({
title: 'Save Quote',
defaultPath: defaultFilePath,
buttonLabel: 'Save',
filters: [{ name: 'PDF', extensions: ['pdf'] }]
});
if (filePath) {
try {
const pdfBuffer = await createInvoice(formData);
fs.writeFileSync(filePath, pdfBuffer);
} catch (error) {
console.log(error);
}
}
});
[{
"name": "@kosprodutora/gerador-de-nota-kos",
"version": "1.0.0",
"description": "Gerador de orçamento para KOS Produtora",
"main": "main.js",
"scripts": {
"start": "electron .",
"pack": "electron-builder --dir",
"dist": "electron-builder",
"debug-build": "electron-builder --dir --config.extraMetadata.main=main.js"
},
"pluginOptions": {
"electronBuilder": {
"nodeIntegration": true,
"externals": [
"pdfkit"
]
}
},
"author": {
"name": "G",
"email": "[email protected]"
},
"build": {
"appId": "com.kosprodutora.geradordenotakos",
"productName": "Gerador de Nota KOS",
"win": {
"target": "nsis",
"icon": "icon.ico"
},
"nsis": {
"oneClick": false,
"allowToChangeInstallationDirectory": true
}
},
"devDependencies": {
"electron": "^20.3.12",
"electron-builder": "^23.6.0"
},
"dependencies": {
"fs": "^0.0.1-security",
"get-stream": "^6.0.1",
"path": "^0.12.7",
"pdfkit": "^0.13.0"
}
}]
my createinvoice
const PDFDocument = require('pdfkit');
const getStream = require('get-stream');
async function createInvoice(invoice) {
const doc = new PDFDocument();
generateHeader(doc);
generateCustomerInformation(doc, invoice);
generateInvoiceTable(doc, invoice);
generateFooter(doc);
doc.end();
return await getStream.buffer(doc);
}
function generateHeader(doc) {
doc.text("Header Text");
}
function generateCustomerInformation(doc, invoice) {
doc.text("Customer Information");
}
function generateInvoiceTable(doc, invoice) {
doc.text("Invoice Table");
}
function generateFooter(doc) {
doc.text("Footer Text");
}
module.exports = {
createInvoice
};
A solutution for my problem.