PhantomJS:管道输入 [英] PhantomJS: pipe input

查看:109
本文介绍了PhantomJS:管道输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PhantomJS将html页面呈现为pdf. 我不想将文件写入磁盘,内存中有html,而内存中则需要pdf.

I am trying to use PhantomJS to render an html page to pdf. I do not want to write the files to disk, I have the html in memory, and I want the pdf in memory.

使用Pooria Azimi的出色答案,网址为此问题,我可以获取pdf从命名管道.当在另一端尝试相同的操作时(用命名管道替换输入文件),我最终得到一个空白的pdf.

Using the excellent answer from Pooria Azimi at this question, i am able to get the pdf from a named pipe. When trying the same on the other end (replacing the input file with a named pipe), I end up with a blank pdf.

这就是我现在正在做的事情(简体):

This is what I am doing now (simplified):

mkfifo in_pipe.html out_pipe.pdf
./phantomjs rasterize.js in_pipe.html out_pipe.pdf

然后在另一个终端:

echo '<center>hey!</center>' > in_pipe.html
cat out_pipe.pdf > out.pdf

文件out.pdf已创建,但为空白.我想念什么吗?

The file out.pdf is created, but is blank. Am I missing something?

推荐答案

您可以直接在PhantomJS中进行非常简单的查找(只是没有真正记录在文档中).

You can do what you're looking for very simply (it's just not really documented) directly in PhantomJS.

var page = require('webpage').create(),
    fs = require('fs');

page.viewportSize = { width: 600, height: 600 };
page.paperSize = { format: 'Letter', orientation: 'portrait', margin: '1cm' };

page.content = fs.read('/dev/stdin');

window.setTimeout(function() {
    page.render('/dev/stdout', { format: 'pdf' });
    phantom.exit();
}, 1);

(如果您的图像需要加载等,则可能需要增加超时时间.)

(May need to increase the timeout if you have images that need loading, etc.)

HTML输入标准输入,PDF二进制输出标准输出.您可以像这样测试它:

HTML comes in stdin, PDF binary goes out stdout. You can test it like:

echo "<b>test</b>" | phantomjs makepdf.js > test.pdf && open test.pdf

这篇关于PhantomJS:管道输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆