Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
729 views
in Technique[技术] by (71.8m points)

linux - PhantomJS: pipe input

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.

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

Then in another terminal:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...