wkhtmltoimage将html转换为图像Perl和Centos

时间:2015-01-19 20:11:11

标签: linux perl centos wkhtmltoimage

我正在尝试使用wkhtmltoimage将html和网页转换为使用Perl模块WKHTMLTOPDF的图像。下面的脚本和代码可以从命令行运行,但如果我从浏览器调用脚本则不起作用。

Update:

If I run the script from the shell as a root user, it runs without
error, if I switch to the domain user where the script is located,
I get that error, seems it is executable permissions for domain owner.

错误是:

error running '/usr/local/bin/wkhtmltoimage': '/usr/local/bin/wkhtmltoimage http://yahoo.com 
/home/xxxx/public_html/pdfwebkit/output.png' died with signal 11, with coredump at 
/usr/local/perl-5.18.1/lib/site_perl/5.18.1/MooseX/Role/Cmd.pm line 128.
MooseX::Role::Cmd::run(WKHTMLTOPDF=HASH(0x2714260), "http://yahoo.com", 
"/home/xxxx/public_html/pdfwebkit/output.png") called at /usr/local/
perl-5.18.1/lib/site_perl/5.18.1/WKHTMLTOPDF.pm line 645 WKHTMLTOPDF::generate(WKHTMLT
OPDF=HASH(0x2714260)) called at htmltoimage.cgi line xxx main::convert_using_WKHTMLTOPDF_image("http://yahoo.com", 
"/home/xxxx/public_html/pdfwebkit/output.png") called at htmltoimage.cgi line xx

我使用的代码是:

#!/usr/bin/perl
#!C:\perl\bin\perl.exe
print "Content-type: text/html;charset=utf-8\n\n";
use File::Spec::Functions;
use File::Basename;
BEGIN {
    $|=1;
    use CGI::Carp qw(fatalsToBrowser set_message);
    sub handle_errors {
        #print "Content-type: text/html;charset=utf-8\n\n";
        my $msg = shift;
        print qq!<h1><font color="red">Software Error</font></h1>!;
        print qq!<p>$msg</p>!;
    }
    set_message(\&handle_errors);
}

$|=1;
my ($Script, $Bin);
if ($ENV{SCRIPT_FILENAME}) {
    ($Script, $Bin) = fileparse($ENV{SCRIPT_FILENAME});
}
else {
    ($Script, $Bin) = fileparse(__FILE__);
}

use WKHTMLTOPDF;

    my $outfile = catfile ($Bin, 'output.jpg');
    print "Converting url to image file $outfile...<br>\n";
    convert_using_WKHTMLTOPDF_image('http://yahoo.com', $outfile);
    print "Finished...<br>\n";
    exit;

sub convert_using_WKHTMLTOPDF_image {
    my ($page, $output) = @_;
    my $pdf = new WKHTMLTOPDF;
    my $bin = '/usr/local/bin/wkhtmltoimage';
    #my $bin = 'C:/Program Files/wkhtmltopdf/bin/wkhtmltoimage.exe';
    $pdf->bin_name($bin);
    $pdf->_input_file($page);
    $pdf->_output_file($output);
    #$pdf->grayscale(1);
    $pdf->generate;
}

sub convert_html_to_image_direct {
    my ($page, $output) = @_;
    my $bin = '/usr/local/bin/wkhtmltoimage --quiet ';
    my $out = `$bin $page $output`;
    print "out: $out<br>\n";
    return $out;
}

代码在Windows上从浏览器正常工作。 如果我尝试使用wkhtmltopdf将html转换为pdf,我会遇到同样的问题。

我安装二进制文件的方式是从这里开始的:

https://gist.github.com/DaRamirezSoto/5489861

# wget http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.11.0_rc1-static-amd64.tar.bz2
# wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2
# tar xvjf wkhtmltoimage-0.11.0_rc1-static-amd64.tar.bz2
# tar xvjf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2
# chown root:root wkhtmltopdf-amd64
# chown root:root wkhtmltoimage-amd64
# mv wkhtmltopdf-amd64 /usr/bin/wkhtmltopdf
# mv wkhtmltoimage-amd64 /usr/bin/wkhtmltoimage

// dependencies
# yum install -y libXrender libXext openssl openssl-devel fontconfig 

1 个答案:

答案 0 :(得分:0)

我使用Centos 6服务器进行了测试(在IE 11 / Win 8.1中),只需对我想要保存的文件夹的路径稍作修改就可以正常工作。

我的输出是 将url转换为图像文件/ home / user /../ bittertruth.jpg ... 完了...

就像你预期的那样......