简体   繁体   English

NodeJS HTML-pdf:fs.readfilesync如何异步/等待

[英]NodeJS Html-pdf: fs.readfilesync how to async/await

I have a problem with my html-pdf document creation. 我的html-pdf文档创建有问题。 The problem is that often the code runs to fast to complete the process of pdf-docutment creation. 问题在于,通常代码会快速运行以完成pdf文件创建过程。 The Processes consists out of building an HTML-String by replacing placeholders in an Html file. 该过程包括通过替换HTML文件中的占位符来构建HTML字符串。 Below you see the code what happens afterwards. 在下面的代码中,您会看到随后的情况。

                            Object.keys(setter).forEach(function(element, key, _array) {
                              var regex = new RegExp(element, "g");

                              data = data.replace(regex, setter[element])
                            })

                            var result = data;

                            fs.writeFile(mergeFileRes, result, 'utf8', function (err) {
                                if(err) { 
                                    console.log(err); 
                                    return;
                                } else {

                                    let html2 = fs.readFileSync(mergeFileRes, 'utf8');
                                    let options = { 
                                        format: 'a4' , 
                                        "directory" : "/tmp",
                                    };                            

                                    if(html2){
                                        pdf.create(html2, options).toStream(function(err, stream2){
                                            if(err)  console.log(err);
                                            stream2.pipe(res);

                                            stream2.on('end', function () {
                                                try{
                                                    fs.unlink(mergeFileRes)
                                                    console.log(3090, "deleted file");
                                                }
                                                catch (err){
                                                    console.log(3090, "Did not delete file");
                                                }
                                            });
                                        });
                                    } else {

                                    }
                                }
                            });

My problem is that in many cases the html2 variable is not yet created before the pdf.create process starts. 我的问题是,在许多情况下,在pdf.create进程开始之前尚未创建html2变量。 This is probably because the readFileSync takes too long to finish. 这可能是因为readFileSync需要太长时间才能完成。

I was wondering, how can I fix this. 我想知道如何解决这个问题。 How can I make the pdf.create wait for the readFileSync to finish and the html2 variable to be filled. 如何使pdf.create等待readFileSync完成并填充html2变量。

You can use fs.readFile to read the file asynchronously and html2 will be available within the callback function. 您可以使用fs.readFile异步读取文件,并且html2将在回调函数中可用。

Object.keys(setter).forEach(function(element, key, _array) {
    var regex = new RegExp(element, "g");

    data = data.replace(regex, setter[element])
})

var result = data;

fs.writeFile(mergeFileRes, result, 'utf8', function (err) {
    if(err) { 
        console.log(err); 
        return;
    } else {

        fs.readFile(mergeFileRes, 'utf8', function(err, html2){
            if (err) throw err;

            let options = { 
                format: 'a4' , 
                "directory" : "/tmp",
            };                            

            pdf.create(html2, options).toStream(function(err, stream2){
                if(err)  console.log(err);
                stream2.pipe(res);

                stream2.on('end', function () {
                    try{
                        fs.unlink(mergeFileRes)
                        console.log(3090, "deleted file");
                    }
                    catch (err){
                        console.log(3090, "Did not delete file");
                    }
                });
            });
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM