Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs上的wkhtmltopdf生成损坏的PDF_Node.js_Pdf_Wkhtmltopdf - Fatal编程技术网

Node.js nodejs上的wkhtmltopdf生成损坏的PDF

Node.js nodejs上的wkhtmltopdf生成损坏的PDF,node.js,pdf,wkhtmltopdf,Node.js,Pdf,Wkhtmltopdf,我正在使用wkhtmltopdf在nodejs中生成PDF 下面是我生成pdf的示例代码 var wkhtmltopdf = require('wkhtmltopdf') , createWriteStream = require('fs').createWriteStream; var r = wkhtmltopdf('http://www.google.com', { pageSize: 'letter' }) .pipe(createWriteStr

我正在使用wkhtmltopdf在nodejs中生成PDF

下面是我生成pdf的示例代码

var  wkhtmltopdf = require('wkhtmltopdf')
    , createWriteStream = require('fs').createWriteStream;

 var r =  wkhtmltopdf('http://www.google.com', { pageSize: 'letter' })
            .pipe(createWriteStream('C:/MYUSERNAME/demo.pdf'));

        r.on('close', function(){
            mycallback();
        });
上述代码正在生成损坏的PDF。我想不出这个问题。 虽然当我使用命令提示符生成PDF时,它是正确生成的 就像我在windows命令提示符中使用下面的代码一样

wkhtmltopdf http://www.google.com demo.pdf 
我得到了正确的pdf生成,遗憾的是,当我试图在节点环境中生成pdf时,它生成了损坏的pdf

如果有帮助,我正在使用wkhtmltopdf 0.11.0 rc2


提前感谢。

wkhtmltopdf for node在windows上有一个bug,因此您可以编写一个新的bug

像这样:

function wkhtmltopdf(input, pageSize) {
    var spawn = require('child_process').spawn;

    var html;

    var isUrl = /^(https?|file):\/\//.test(input);
    if (!isUrl) {
        html = input;
        input = '-';
    }

    var args = ['wkhtmltopdf', '--quiet', '--page-size', pageSize, input, '-']
    if (process.platform === 'win32') {
        var child = spawn(args[0], args.slice(1));
    } else {
        var child = spawn('/bin/sh', ['-c', args.join(' ') + ' | cat']);
    }

    if (!isUrl) {
        child.stdin.end(html);
    }

    return child.stdout;
}

// usage:

createWriteStream = require('fs').createWriteStream;

wkhtmltopdf('http://google.com/', 'letter')
    .pipe(createWriteStream('demo1.pdf'));

wkhtmltopdf('<body>hello world!</body>', 'letter')
    .pipe(createWriteStream('demo2.pdf'));
函数wkhtmltopdf(输入,页面大小){ var spawn=require('child_process')。spawn; var-html; var isUrl=/^(https?|文件):\/\/.test(输入); 如果(!isUrl){ html=输入; 输入='-'; } 变量args=['wkhtmltopdf','-quiet','-page size',pageSize,input'-'] 如果(process.platform==='win32'){ var child=spawn(args[0],args.slice(1)); }否则{ var child=spawn('/bin/sh',['-c',args.join('')+'| cat']); } 如果(!isUrl){ child.stdin.end(html); } 返回child.stdout; } //用法: createWriteStream=require('fs')。createWriteStream; wkhtmltopdf('http://google.com/","字母") .pipe(createWriteStream('demo1.pdf'); wkhtmltopdf(“你好,世界!”,“字母”) .pipe(createWriteStream('demo2.pdf');
注意:参数现在是'letter'而不是{pageSize:'letter'}

wkhtmltopdf for node在windows中有一个bug,因此您可以编写一个新的bug

像这样:

function wkhtmltopdf(input, pageSize) {
    var spawn = require('child_process').spawn;

    var html;

    var isUrl = /^(https?|file):\/\//.test(input);
    if (!isUrl) {
        html = input;
        input = '-';
    }

    var args = ['wkhtmltopdf', '--quiet', '--page-size', pageSize, input, '-']
    if (process.platform === 'win32') {
        var child = spawn(args[0], args.slice(1));
    } else {
        var child = spawn('/bin/sh', ['-c', args.join(' ') + ' | cat']);
    }

    if (!isUrl) {
        child.stdin.end(html);
    }

    return child.stdout;
}

// usage:

createWriteStream = require('fs').createWriteStream;

wkhtmltopdf('http://google.com/', 'letter')
    .pipe(createWriteStream('demo1.pdf'));

wkhtmltopdf('<body>hello world!</body>', 'letter')
    .pipe(createWriteStream('demo2.pdf'));
函数wkhtmltopdf(输入,页面大小){ var spawn=require('child_process')。spawn; var-html; var isUrl=/^(https?|文件):\/\/.test(输入); 如果(!isUrl){ html=输入; 输入='-'; } 变量args=['wkhtmltopdf','-quiet','-page size',pageSize,input'-'] 如果(process.platform==='win32'){ var child=spawn(args[0],args.slice(1)); }否则{ var child=spawn('/bin/sh',['-c',args.join('')+'| cat']); } 如果(!isUrl){ child.stdin.end(html); } 返回child.stdout; } //用法: createWriteStream=require('fs')。createWriteStream; wkhtmltopdf('http://google.com/","字母") .pipe(createWriteStream('demo1.pdf'); wkhtmltopdf(“你好,世界!”,“字母”) .pipe(createWriteStream('demo2.pdf');
注意:参数现在是'letter'而不是{pageSize:'letter'}

wkhtmltopdf for node在windows中有一个bug,因此您可以编写一个新的bug

像这样:

function wkhtmltopdf(input, pageSize) {
    var spawn = require('child_process').spawn;

    var html;

    var isUrl = /^(https?|file):\/\//.test(input);
    if (!isUrl) {
        html = input;
        input = '-';
    }

    var args = ['wkhtmltopdf', '--quiet', '--page-size', pageSize, input, '-']
    if (process.platform === 'win32') {
        var child = spawn(args[0], args.slice(1));
    } else {
        var child = spawn('/bin/sh', ['-c', args.join(' ') + ' | cat']);
    }

    if (!isUrl) {
        child.stdin.end(html);
    }

    return child.stdout;
}

// usage:

createWriteStream = require('fs').createWriteStream;

wkhtmltopdf('http://google.com/', 'letter')
    .pipe(createWriteStream('demo1.pdf'));

wkhtmltopdf('<body>hello world!</body>', 'letter')
    .pipe(createWriteStream('demo2.pdf'));
函数wkhtmltopdf(输入,页面大小){ var spawn=require('child_process')。spawn; var-html; var isUrl=/^(https?|文件):\/\/.test(输入); 如果(!isUrl){ html=输入; 输入='-'; } 变量args=['wkhtmltopdf','-quiet','-page size',pageSize,input'-'] 如果(process.platform==='win32'){ var child=spawn(args[0],args.slice(1)); }否则{ var child=spawn('/bin/sh',['-c',args.join('')+'| cat']); } 如果(!isUrl){ child.stdin.end(html); } 返回child.stdout; } //用法: createWriteStream=require('fs')。createWriteStream; wkhtmltopdf('http://google.com/","字母") .pipe(createWriteStream('demo1.pdf'); wkhtmltopdf(“你好,世界!”,“字母”) .pipe(createWriteStream('demo2.pdf');
注意:参数现在是'letter'而不是{pageSize:'letter'}

wkhtmltopdf for node在windows中有一个bug,因此您可以编写一个新的bug

像这样:

function wkhtmltopdf(input, pageSize) {
    var spawn = require('child_process').spawn;

    var html;

    var isUrl = /^(https?|file):\/\//.test(input);
    if (!isUrl) {
        html = input;
        input = '-';
    }

    var args = ['wkhtmltopdf', '--quiet', '--page-size', pageSize, input, '-']
    if (process.platform === 'win32') {
        var child = spawn(args[0], args.slice(1));
    } else {
        var child = spawn('/bin/sh', ['-c', args.join(' ') + ' | cat']);
    }

    if (!isUrl) {
        child.stdin.end(html);
    }

    return child.stdout;
}

// usage:

createWriteStream = require('fs').createWriteStream;

wkhtmltopdf('http://google.com/', 'letter')
    .pipe(createWriteStream('demo1.pdf'));

wkhtmltopdf('<body>hello world!</body>', 'letter')
    .pipe(createWriteStream('demo2.pdf'));
函数wkhtmltopdf(输入,页面大小){ var spawn=require('child_process')。spawn; var-html; var isUrl=/^(https?|文件):\/\/.test(输入); 如果(!isUrl){ html=输入; 输入='-'; } 变量args=['wkhtmltopdf','-quiet','-page size',pageSize,input'-'] 如果(process.platform==='win32'){ var child=spawn(args[0],args.slice(1)); }否则{ var child=spawn('/bin/sh',['-c',args.join('')+'| cat']); } 如果(!isUrl){ child.stdin.end(html); } 返回child.stdout; } //用法: createWriteStream=require('fs')。createWriteStream; wkhtmltopdf('http://google.com/","字母") .pipe(createWriteStream('demo1.pdf'); wkhtmltopdf(“你好,世界!”,“字母”) .pipe(createWriteStream('demo2.pdf');

注意:参数现在是'letter'而不是{pageSize:'letter'}

我正在尝试你的代码,我会尽快回复你。似乎只有args被改变了…?现在工作得很好。但是当我传递一个包含html字符串的参数时,它没有生成输出,而不是google.com。有什么建议吗?我用了同样的方法,但是我的html是一个巨大的字符串,不知道它为什么会生成损坏的pdf。我将检查新代码。谢谢每当出现分页符时,我的表行都会被切成两半。有什么建议吗…?媒体打印css似乎不起作用,如果你有时间,你可以建议一些事吗?我正在尝试你的代码,我会尽快回复你。似乎只有args被改变了…?现在工作得很好。但是当我传递一个包含html字符串的参数时,它没有生成输出,而不是google.com。有什么建议吗?我用了同样的方法,但是我的html是一个巨大的字符串,不知道它为什么会生成损坏的pdf。我将检查新代码。谢谢每当出现分页符时,我的表行都会被切成两半。有什么建议吗…?媒体打印css似乎不起作用,如果你有时间,你可以建议一些事吗?我正在尝试你的代码,我会尽快回复你。似乎只有args被改变了…?现在工作得很好。但是,当我传递一个包含html字符串的参数时,它没有生成输出,而不是google.com。有什么建议吗…?我用了同样的方法,但是我的html是一个巨大的字符串,不知道为什么它会生成