phantomjs pdf to stdout

前端 未结 2 1753
时光取名叫无心
时光取名叫无心 2021-02-14 17:35

I am desperately trying to output a PDF generated by phantomJS to stdout like here

What I am getting is an empty PDF file, although it is not 0 in size, it displays a bl

2条回答
  •  -上瘾入骨i
    2021-02-14 18:07

    Yes, that's right ISO-8859-1 is the default encoding for QT so you will need to add the required parameter to the command line --output-encoding=ISO-8859-1 so the pdf output won't be corrupted

    i.e.

    phantomjs.exe rasterize.js --output-encoding=ISO-8859-1 < input.html > output.pdf

    and rasterize.js looks like this (tested, works for both Unix and Windows)

    var page = require('webpage').create(),
    system = require('system');
    
    page.viewportSize = {width: 600, height: 600};
    page.paperSize = {format: 'A4', orientation: system.args[1], margin: '1cm'};
    
    page.content = system.stdin.read();
    
    window.setTimeout(function () {
        try {
            page.render('/dev/stdout', {format: 'pdf'});
        }
        catch (e) {
            console.log(e.message + ';;' + output_file);
        }
        phantom.exit();
    }, 1000);
    

    or alternatively you can set encoding using stdout and if you are reading from UTF-8 stream then you might have to set encoding for stdin as well;

    system.stdout.setEncoding('ISO-8859-1');
    system.stdin.setEncoding('UTF-8');
    page.content = system.stdin.read();
    

提交回复
热议问题