简体   繁体   English

在 PhantomJS 中打印页面

[英]Print page in PhantomJS

Is there any way to print a page in PhantomJS .有没有办法在PhantomJS中打印页面。 I want to pass my page to PDF Printer ( PDFCreator ) in order to get a good-looking PDF page ( See my question here )我想将我的页面传递给 PDF 打印机 ( PDFCreator ) 以获得好看的 PDF 页面(在此处查看我的问题

I'm using the following code, but it doesn't work我正在使用以下代码,但它不起作用

var page = require('webpage').create(),
address, output, size;

var input = phantom.args[0];
page.open(input, function (status) {
    page.evaluate(function () {
        window.print();
    });
    phantom.exit();
});

Maybe this helps: You need to set a larget timeout in rasterize.js in order to allow for the entire document to render before you do page.render(output)也许这有帮助:您需要在rasterize.js中设置一个较大的超时时间,以便在执行page.render(output)之前允许呈现整个文档

Also, the script you provided is very different from what rasterize.js does and it won't work.此外,您提供的脚本与rasterize.js所做的非常不同,它不会起作用。

Looks like PhantomJS has examples included on its website .看起来PhantomJS 在其网站上包含示例 One of them, rasterize.js , says it shows how to output the HTML as a PDF.其中之一rasterize.js表示它展示了如何将 HTML 输出为 PDF。 I haven't tried it, but perhaps that could lead to a solution.我还没有尝试过,但也许这会导致解决方案。

I found this gist: https://gist.github.com/boriscy/a0ac26c54f30296949df which does what looks like a crucial thing:我找到了这个要点: https ://gist.github.com/boriscy/a0ac26c54f30296949df,它看起来很重要:

page.onLoadFinished = function() {
  page.render(pdf);
  phantom.exit();
}

The other examples I have seen put in a big wait and just hope by the time the wait is over the document is done.我看到的其他示例需要等待很长时间,希望等待结束时文档已经完成。 To me that doesn't seem great as bigger documents will fail, and smaller documents will be slower than needed.对我来说,这似乎不太好,因为较大的文档会失败,而较小的文档会比需要的慢。

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

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