How can I convert a HTML table to an image?

蓝咒 提交于 2019-12-10 11:45:43

问题


I want to to convert a HTML table to a PNG or GIF image. I'm using Perl under Linux operating system.

I use the TinyMCE editor. The user can edit a HTML table (using CSS classes and other CSS formatters), and I want to save this table as an image.

What is the simplest way to do this?


回答1:


Can you use ImageMagick/PerlMagick and Webkit?

https://metacpan.org/module/PDF::WebKit

http://code.google.com/p/wkhtmltopdf/

https://metacpan.org/module/Image::Magick

http://www.imagemagick.org/script/perl-magick.php

use PDF::WebKit;
use Image::Magick;

# PDF::WebKit->new takes the HTML and any options for wkhtmltopdf
# run `wkhtmltopdf --extended-help` for a full list of options
my $kit = PDF::WebKit->new( \$html, page_size => 'Letter' );
push @{ $kit->stylesheets }, "/path/to/css/file";

# save the PDF to a file
my $file = $kit->to_file('/path/to/save/pdf');

#Perl interface
my $p = new Image::Magick;
$p->Read('/path/to/save/pdf');
$p->Write("file.png");

#Or command line
my @args = ( 'convert', '/path/to/save/pdf', "file.png" );
system(@args) == 0
  or die "system @args failed: $?";



回答2:


You can accomplish this with a few command line tools.

$: html2ps table.html > table.ps
$: ps2pdf table.ps 
$: convert table.pdf table.png

On my Ubuntu system I installed these with:

apt-get install html2ps
apt-get install ps2pdf
apt-get install imagemagick

The conversion isn't likely to be identical to what you see in your web browser.



来源:https://stackoverflow.com/questions/13903130/how-can-i-convert-a-html-table-to-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!