将.html文件转换为图片

10
我正在寻找一个能够忠实地将.html文件转换为图像格式(最好是png或jpeg)的工具。我已经谷歌了几个小时并下载了十几个免费试用版,但似乎没有一个能胜任这项工作。Adobe Acrobat Pro 差点就可以胜任,但它会决定将表格单元格缩小以适应页面,而不是缩小整个表格。如有建议,请告知。祝好!

假设您尝试做的页面太多,无法简单地将它们全部一起截屏打印出来? - Austin
这个东西 http://awesomium.com/ 可以用吗? - Mike Christensen
我使用Firefox的Screengrab扩展。Chrome有一些类似的扩展程序。 - j08691
也许这个帖子可以帮助。 - user377628
有许多适用于Firefox的插件可以完成这个任务。您的具体使用情况是什么? - steveax
谢谢大家。把问题写出来让我找到了解决办法。我只需要在表格单元格中添加nowrap,Acrobat就可以正常工作了。 - Kevin
3个回答

20

我正在使用wkhtmltoimage。

它是一个快速的命令行工具,使用webkit库渲染任何网站。有两个版本,一个用于生成pdf,另一个用于生成图像。wkhtmltoimage适用于Windows、Linux和Mac。

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

/wkhtmltoimage-i386 -H
Name:
  wkhtmltoimage 0.10.0 rc2

Synopsis:
  wkhtmltoimage [OPTIONS]... <input file> <output file>

Description:
  Converts an HTML page into an image, 

General Options:
      --allow <path>                  Allow the file or files from the specified
                                      folder to be loaded (repeatable)
      --checkbox-checked-svg <path>   Use this SVG file when rendering checked
                                      checkboxes
      --checkbox-svg <path>           Use this SVG file when rendering unchecked
                                      checkboxes
      --cookie <name> <value>         Set an additional cookie (repeatable)
      --cookie-jar <path>             Read and write cookies from and to the
                                      supplied cookie jar file
      --crop-h <int>                  Set height for croping
      --crop-w <int>                  Set width for croping
      --crop-x <int>                  Set x coordinate for croping
      --crop-y <int>                  Set y coordinate for croping
      --custom-header <name> <value>  Set an additional HTTP header (repeatable)
      --custom-header-propagation     Add HTTP headers specified by
                                      --custom-header for each resource request.
      --no-custom-header-propagation  Do not add HTTP headers specified by
                                      --custom-header for each resource request.
      --debug-javascript              Show javascript debugging output
      --no-debug-javascript           Do not show javascript debugging output
                                      (default)
      --encoding <encoding>           Set the default text encoding, for input
  -H, --extended-help                 Display more extensive help, detailing
                                      less common command switches
  -f, --format <format>               Output file format (default is jpg)
      --height <int>                  Set screen height (default is calculated
                                      from page content) (default 0)
  -h, --help                          Display help
      --htmldoc                       Output program html help
      --images                        Do load or print images (default)
      --no-images                     Do not load or print images
  -n, --disable-javascript            Do not allow web pages to run javascript
      --enable-javascript             Do allow web pages to run javascript
                                      (default)
      --javascript-delay <msec>       Wait some milliseconds for javascript
                                      finish (default 200)
      --load-error-handling <handler> Specify how to handle pages that fail to
                                      load: abort, ignore or skip (default
                                      abort)
      --disable-local-file-access     Do not allowed conversion of a local file
                                      to read in other local files, unless
                                      explecitily allowed with --allow
      --enable-local-file-access      Allowed conversion of a local file to read
                                      in other local files. (default)
      --manpage                       Output program man page
      --minimum-font-size <int>       Minimum font size
      --password <password>           HTTP Authentication password
      --disable-plugins               Disable installed plugins (default)
      --enable-plugins                Enable installed plugins (plugins will
                                      likely not work)
      --post <name> <value>           Add an additional post field (repeatable)
      --post-file <name> <path>       Post an additional file (repeatable)
  -p, --proxy <proxy>                 Use a proxy
      --quality <int>                 Output image quality (between 0 and 100)
                                      (default 94)
      --radiobutton-checked-svg <path> Use this SVG file when rendering checked
                                      radiobuttons
      --radiobutton-svg <path>        Use this SVG file when rendering unchecked
                                      radiobuttons
      --readme                        Output program readme
      --run-script <js>               Run this additional javascript after the
                                      page is done loading (repeatable)
  -0, --disable-smart-width           Use the specified width even if it is not
                                      large enough for the content
      --stop-slow-scripts             Stop slow running javascripts (default)
      --no-stop-slow-scripts          Do not Stop slow running javascripts
                                      (default)
      --transparent                   Make the background transparent in pngs
      --use-xserver                   Use the X server (some plugins and other
                                      stuff might not work without X11)
      --user-style-sheet <url>        Specify a user style sheet, to load with
                                      every page
      --username <username>           HTTP Authentication username
  -V, --version                       Output version information an exit
      --width <int>                   Set screen width (default is 1024)
                                      (default 1024)
      --window-status <windowStatus>  Wait until window.status is equal to this
                                      string before rendering page
      --zoom <float>                  Use this zoom factor (default 1)

Specifying A Proxy:
  By default proxy information will be read from the environment variables:
  proxy, all_proxy and http_proxy, proxy options can also by specified with the
  -p switch

  <type> := "http://" | "socks5://"
  <serif> := <username> (":" <password>)? "@"
  <proxy> := "None" | <type>? <sering>? <host> (":" <port>)?

  Here are some examples (In case you are unfamiliar with the BNF):

  http://user:password@myproxyserver:8080
  socks5://myproxyserver
  None

Contact:
  If you experience bugs or want to request new features please visit 
  <http://code.google.com/p/wkhtmltopdf/issues/list>, if you have any problems
  or comments please feel free to contact me: <uuf6429@gmail.com>

演示:

./wkhtmltoimage-i386 "https://dev59.com/wWfWa4cB1Zd3GeqPeCbm#11908055" demo.png

Demo


2
这个链接说明了如何实现wkhtmlimage:https://dev59.com/questions/nm3Xa4cB1Zd3GeqPjuK_#22797539 - KarlosFontana
你是如何生成这些图片的?在我的使用中,它们都没有出现,CSS也是。 - nmrlqa4

1
如果您需要一个命令行程序,请尝试IECapt。它使用Internet Explorer渲染引擎。还有一个名为CutyCapt的跨平台实用程序,我没有尝试过哪个更好。关于IECapt的文件参数,需要指定完整路径才能正常工作。似乎不识别相对路径。路径中允许使用引号,例如:--file:"C:\....."

1

还有PhantomJS(参见http://phantomjs.org)。 我在一个用PHP编写的搜索引擎中使用它来生成网站缩略图。 像wkhtmltoimage一样,它是基于WebKit的命令行程序,可以生成图像和PDF。但由于它是一个完整的浏览器,它使用JavaScript进行处理。

编辑:请参阅https://github.com/ariya/phantomjs/wiki/Screen-Capture以获取图像渲染的具体示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接