首页 > 解决方案 > 如何使用 electron-html-to node.js 解决超时问题

问题描述

我在尝试使用电子将 html 文件转换为 pdf 时遇到此超时。我正在通过节点运行这个 js 应用程序。

`{ Error: Worker Timeout, the worker process does not respond after 10000 ms
at Timeout._onTimeout (C:\Users\Owner\Desktop\code\PDF-Profile-Generator\node_modules\electron-workers\lib\ElectronManager.js:377:21)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
workerTimeout: true,
message:
Worker Timeout, the worker process does not respond after 10000 ms,
electronTimeout: true }`

我对电子不太了解。我无法尝试太多尝试调试它。js 代码旨在根据用户输入生成一个 html 文件,从 github 配置文件中提取。然后,需要将该 html 文件转换为 pdf 文件。

我的js代码如下:

const fs = require("fs")
const convertapi = require('convertapi')('tTi0uXTS08ennqBS');
const path = require("path");
const generate = require("./generateHTML");
const inquirer = require("inquirer");
const axios = require("axios");
const questions = ["What is your Github user name?", "Pick your favorite color?"];


function writeToFile(fileName, data) {
     return fs.writeFileSync(path.join(process.cwd(), fileName), data);
};

function promptUser() {
    return inquirer.prompt([
        {
            type: "input",
            name: "username",
            message: questions[0]
        },
        {
            type: "list",
            name: "colorchoice",
            choices: ["green", "blue", "pink", "red"],
            message: questions[1]
        }
     ])
};

function init() {
    promptUser()
        .then(function ({ username, colorchoice }) {
        const color = colorchoice;
        const queryUrl = `https://api.github.com/users/${username}`;
        let html;
        axios
            .get(queryUrl)
            .then(function (res) {
                res.data.color = color
                const starArray = res.data.starred_url.split(",")
                res.data.stars = starArray.length
                console.log(res)
                html = generate(res.data);
                console.log(html)
                writeToFile("profile.html", html)

            })

        var convertFactory = require('electron-html-to');

        var conversion = convertFactory({
            converterPath: convertFactory.converters.PDF
        });

        conversion({ file: './profile.html' }, function (err, result) {
            if (err) {
                return console.error(err);
            }

            console.log(result.numberOfPages);
            console.log(result.logs);
            result.stream.pipe(fs.createWriteStream(__dirname + '/profile.pdf'));
            conversion.kill(); // necessary if you use the electron-server strategy, see bellow for details
        });
        // convertapi.convert('pdf', { File: './profile.html' })
        //     .then(function (result) {
        //         // get converted file url
        //         console.log("Converted file url: " + result.file.url);

        //         // save to file
        //         return result.file.save(__dirname + "/profile.pdf");
        //     })
        //     .then(function (file) {
        //         console.log("File saved: " + file);
        //     });

    })
}
init();

标签: javascriptnode.jselectron

解决方案


我有一个类似的问题。我安装了多个版本的 Electron(electron、electron-html-to、electron-prebuilt),当我删除 package.json 中的旧版本时问题得到解决,因此只剩下一个。假设是他们互相干扰。

所以检查安装的电子版本,因为问题可能存在而不是你的代码。


推荐阅读