简体   繁体   English

html-pdf和Javascript

[英]html-pdf and Javascript

I have been trying to dynamically generate a PDF file from my web app based on Firebase . 我一直在尝试从基于Firebase Web应用程序动态生成PDF文件。

I recently tried to use html-pdf node module to generate a pdf. 我最近尝试使用html-pdf节点模块生成pdf。 But the implementation I wanted to do was to be able to generate the PDF file using a combination of Javascript and html-pdf. 但是我要实现的实现是能够结合使用Javascript和html-pdf来生成PDF文件。

So what I wanted to understand is , how can I call html-pdf 's create functionality from my apps javascript file . 所以我想了解的是,如何从我的应用程序javascript file调用html-pdf的创建功能。

I have used browserify to create a bundle.js file for html-pdf node module . 我已经使用bundle.jshtml-pdf node module创建bundle.js文件。

If there is any other info that you need. 如果您需要任何其他信息。 Please let me know. 请告诉我。

You can do this by importing the html-pdf package into your file 您可以通过将html-pdf包导入文件中来实现

const pdf = require('html-pdf');

In my case, I use EJS as my template engine so first, I have to render the file before sending it to the PDF creator. 就我而言,我使用EJS作为模板引擎,因此,首先,必须渲染文件,然后再将其发送给PDF创建者。 Then, through the pdf.create function, I send my rendered HTML with options and convert into a PDF file through the .toFile() function. 然后,通过pdf.create函数,发送带有选项的渲染HTML,并通过.toFile()函数将其转换为PDF文件。

ejs.renderFile('example.ejs', {...}, (err, result) => {
        // render on success
        if (result) {
            pdf.create(result, options).toFile('./example.pdf', function(err, res) {
                if (err) return console.log(err);
            });
        }
        // render or error
        else {
            res.end('An error occurred');
        }
    });

Cheers! 干杯!

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

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