Nodejs + Phantomjs从变量渲染PDF

时间:2021-06-29 21:10:33

I would generate some PDF file in Node.js

我会在Node.js中生成一些PDF文件

I would generate it starting from an HTML variable. (i generate it with ejs) I'm installed the bridge and i did some tests but something doesn't go fine.

我会从HTML变量开始生成它。 (我用ejs生成它)我安装了桥,我做了一些测试,但有些事情并不顺利。

if i try to generate a PDF from an Http:// url it works.

如果我尝试从Http:// url生成PDF,它可以工作。

var phantom = require('phantom');
var pdffile =__dirname+'/test.pdf';

phantom.create(function(ph) {
  ph.createPage(function(page) {
    page.open('http://www.google.com',function(err,status){
      page.render(pdffile, function(err){
        ph.exit();
      });
    });
  });
});

if i try with a HTML into variable.... this example works:

如果我尝试使用HTML变量....这个例子有效:

var phantom = require('phantom');
var pdffile =__dirname+'/test.pdf';
var content = '<html><head></head><body><h1>Hello World</h1><p>my paragraph</p></body></html>';

phantom.create(function(ph) {
  ph.createPage(function(page) {
    page.set('content', content);
page.render(pdffile, function(err){
      console.log(err);
      ph.exit();
    });
  });
});

But... i don't understand the reason why this follow doesn't work

但是......我不明白为什么这个跟随不起作用的原因

var ejs   = require('ejs');
var fs    = require('fs');
var phantom = require('phantom');
var pdffile =__dirname+'/test.pdf';
var str = fs.readFileSync( __dirname + '/template.ejs', 'utf8');
var params = {id:2423432};
var html = ejs.render(str, params );
var content = '<html><head></head><body>'+html+'</body></html>';

phantom.create(function(ph) {
  ph.createPage(function(page) {
    page.set('content', content);
page.render(pdffile, function(err){
      console.log(err);
      ph.exit();
    });
  });
});

EJS render the content of template file into var, but nothing was render by phantomjs. I also didn't receive err message, the script stop and no PDF files was generated.

EJS将模板文件的内容呈现为var,但是没有任何内容由phantomjs呈现。我也没有收到错误消息,脚本停止并且没有生成PDF文件。

Nobody could help me?

没有人可以帮助我?

1 个解决方案

#1


1  

If what you have is mostly working, there may be a timing issue. Try putting the render inside of an onLoadFinished handler on page.

如果你的工作主要是工作,可能会有时间问题。尝试将渲染放在页面上的onLoadFinished处理程序中。

So, your sample would become:

那么,你的样本会变成:

  ph.createPage(function(page) {
    page.onLoadFinished = function (){
       page.render(pdffile);
       ph.exit();
    };
    page.set('content', content);
    });
  });

You can see a similar question and sample code here: PhantomJS - Rendering fails to show all images

您可以在此处看到类似的问题和示例代码:PhantomJS - 渲染无法显示所有图像

#1


1  

If what you have is mostly working, there may be a timing issue. Try putting the render inside of an onLoadFinished handler on page.

如果你的工作主要是工作,可能会有时间问题。尝试将渲染放在页面上的onLoadFinished处理程序中。

So, your sample would become:

那么,你的样本会变成:

  ph.createPage(function(page) {
    page.onLoadFinished = function (){
       page.render(pdffile);
       ph.exit();
    };
    page.set('content', content);
    });
  });

You can see a similar question and sample code here: PhantomJS - Rendering fails to show all images

您可以在此处看到类似的问题和示例代码:PhantomJS - 渲染无法显示所有图像