【问题标题】:How can I convert a HTML table to an image?如何将 HTML 表格转换为图像?
【发布时间】:2021-09-21 20:49:14
【问题描述】:

我想将 HTML 表格转换为 PNG 或 GIF 图像。我在 Linux 操作系统下使用 Perl。

我使用 TinyMCE 编辑器。用户可以编辑 HTML 表格(使用 CSS 类和其他 CSS 格式化程序),我想将此表格保存为图像。

最简单的方法是什么?

【问题讨论】:

标签: html css image perl converter


【解决方案1】:

您可以使用一些命令行工具来完成此操作。

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

在我的 Ubuntu 系统上,我安装了这些:

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

转化可能与您在网络浏览器中看到的不同。

【讨论】:

  • 感谢您的回答。但是这个解决方案没有处理 CSS 格式。
【解决方案2】:

你可以使用 ImageMagick/PerlMagick 和 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: $?";

【讨论】:

  • 此解决方案将整个 HTML 文档保存为 pdf,在 png 之后...?
  • 它会先将 html 保存为 pdf。然后 ImageMagick 会将其转换为 png。
  • 听起来不错。转换 PNG 后,我需要从图像中剪切我的表格并保存为另一个图像。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 2012-05-31
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
相关资源
最近更新 更多