尝试使用 Electron 和 node-html-pdf 创建 PDF 时出错
Error when trying to create a PDF with Electron and node-html-pdf
我有这个小应用程序,它使用 html-pdf 从电子中的简单形式创建 PDF,当我 运行 npm start(electron .) 时它工作正常,但在我尝试构建之后生成 PDF 我收到此错误:
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)
这是我的主文件“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();
}
});
这是我的网页,格式为“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>
我的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"
}
}
我的文件夹项目
ELECTRON_CREATE_PDF
|- node_modules
|- release_builds
|- views
| |-PDF
| |-result.pdf
|-index.js
|-package.json
感谢所有抽出时间的人。
所以我在这个 post 中发现了一个与 electron-pdf 类似的错误,解决了将“electron-pdf”更改为 node_module 外部的父文件夹,与不允许任何写操作的“asar”相关的东西。在我的例子中,我需要做的是改变我的编译器方法,所以我首先使用样板文件创建了一个新项目,
"npm create-electron-app"
已经有一个构建应用程序的方法是“npm 运行 make”,它显然解决了使用 [=22 写入的权限问题=]-pdf 来自 node_modules,构建后工作正常。
我有这个小应用程序,它使用 html-pdf 从电子中的简单形式创建 PDF,当我 运行 npm start(electron .) 时它工作正常,但在我尝试构建之后生成 PDF 我收到此错误:
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)
这是我的主文件“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();
}
});
这是我的网页,格式为“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>
我的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"
}
}
我的文件夹项目
ELECTRON_CREATE_PDF
|- node_modules
|- release_builds
|- views
| |-PDF
| |-result.pdf
|-index.js
|-package.json
感谢所有抽出时间的人。
所以我在这个 post 中发现了一个与 electron-pdf 类似的错误,解决了将“electron-pdf”更改为 node_module 外部的父文件夹,与不允许任何写操作的“asar”相关的东西。在我的例子中,我需要做的是改变我的编译器方法,所以我首先使用样板文件创建了一个新项目,
"npm create-electron-app"
已经有一个构建应用程序的方法是“npm 运行 make”,它显然解决了使用 [=22 写入的权限问题=]-pdf 来自 node_modules,构建后工作正常。