node.jsstreamelectronpipenode-html-pdf

Error when trying to create a PDF with Electron and node-html-pdf


I have this little aplication that creates a PDF from a simple form in electron with html-pdf, it works fine when I run npm start(electron .), but after build when I try to generate the PDF I get this error:

A JavaScript error occurred in the mais process
Error: write EPIPE
at afterWriteDispatched(internal/stream_base_commons.js:156:25)
at writeGeneric (internal/stream_base_commons.js:147:3)
enter image description here

This is my main file "index.js"

   const { app, BrowserWindow, ipcMain } = require("electron");
   const pdf = require("html-pdf");
   const path = require("path");
   function createWindow() {
     const win = new BrowserWindow({
       width: 1000,
       height: 500,
       webPreferences: {
         nodeIntegration: true,
         contextIsolation: false,
       },
     });
   
     win.loadFile("views/index.html");
   }
   
   app.whenReady().then(() => {
     createWindow();
     app.on("activate", () => {
       if (BrowserWindow.getAllWindows().length === 0) {
         createWindow();
       }
     });
   });
   
   //PDF Template
   function createTemplatePDF(formData) {
     return `<!DOCTYPE>
     <html>
     <head></head>
     <body>
       <div>
       <h1>${formData.name}</h1>
       <p>${formData.text}</p>
       </div>  
     </body>
     </html>
       `;
   }
   
   //create PDF
   function createPDF(data) {
     const templatePDF = createTemplatePDF(data); // create template from the form inputs
     return new Promise((resolve, reject) => {
       pdf
         .create(templatePDF)
         .toFile(path.join(__dirname, "views/PDF/result.pdf"), (err, res) => {
           if (err) reject();
           else resolve(res);
         });
     });
   }
   
   //IPC Catch
   ipcMain.on("item:submit", (e, item) => {
     //item is the object with the infos of the form
     const novoPDF = createPDF(item); // call the createPDF function
     novoPDF
       .then(() => {
         console.log("PDF created!");
       })
       .catch((err) => {
         console.log(err);
       });
   });
   
   app.on("window-all-closed", () => {
     if (process.platform !== "darwin") {
       app.quit();
     }
   });

This is my webpage with the form "index.html"

    <!DOCTYPE html>
    <html lang="en">
      <head></head> 
      <body>
    
        <form id="form">
          <div><label>Name: </label> <input id="name" type="text" /></div>
          <div><label>text: </label> <textarea id="text"></textarea></div>
          <button type="submit">Generate PDF</button>
        </form>    
        <script>
          const { ipcRenderer } = require("electron");
          const form = document.querySelector("#form");
          form.addEventListener("submit", sendForm);
          function sendForm(e) {
            e.preventDefault();
            let input_name = e.target.name.value;
            let input_text = e.target.text.value;
     
            let data = {
              name: input_name,
              text: input_text
            };
            ipcRenderer.send("item:submit", data);
          }
        </script>
      </body>
    </html>

My package.json

{
  "name": "electron_create_pdf",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "electron .",
    "package-win": "electron-packager . electron-tutorial-app --overwrite --asar=true --platform=win32 --arch=ia32 --icon=assets/icons/win/icon.ico --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Tutorial App\""
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "electron": "^12.0.8",
    "electron-packager": "^15.2.0",
    "express": "^4.17.1",
    "html-pdf": "^3.0.1"
  }
}

My folder project

ELECTRON_CREATE_PDF
|- node_modules
|- release_builds
|- views
|  |-PDF
|     |-result.pdf 
|-index.js
|-package.json

Thanks to anyone who take the time.


Solution

  • So I found a similar error in this post with the electron-pdf, that was solved changing the "electron-pdf" to a parent folder outer of node_module, something related with the "asar" that wasn't allowing any write operation. In my case what I needed to do was change my compiler method, so I first created a new project using the boilerplate,

    "npm create-electron-app"

    Which already have a method to build application that is "npm run make", it apparently solved the problem of permission issue to write with the html-pdf from the node_modules, working just fine after build.