[Translated]How to convert an HTML web page to PNG image on Linux.md

This commit is contained in:
geekpi 2014-02-24 14:58:51 +08:00
parent 9cb1cf1f48
commit 71343869d7
2 changed files with 85 additions and 87 deletions

View File

@ -1,87 +0,0 @@
Translating---------------geekpi
How to convert an HTML web page to PNG image on Linux
================================================================================
One of the easiest way to screen capture a particular web page as a PNG image is by using [CutyCapt][1], which is a convenient command line Linux tool for converting any HTML webpage to a variety of vector and bitmat image formats (e.g., SVG, PDF, PS, PNG, JPEG, TIFF, GIF). Internally, CutyCapt uses WebKit rendering engine to export webpage rendering output to an image file. Built with Qt, CutyCapt is actually a cross-platform application available for other platforms such as Windows as well.
In this tutorial, I will describe how to **convert an HTML web page to PNG image format using CutyCapt**.
### Install CutyCapt on Linux ###
Here are distro-specific instructions to install CutyCapt on Linux.
**Install CutyCapt on Debian, Ubuntu or Linux Mint**
$ sudo apt-get install cutycapt
**Install CutyCapt on Fedora**
$ sudo yum install subversion qt-devel qtwebkit-devel gcc-c++ make
$ svn co svn://svn.code.sf.net/p/cutycapt/code/ cutycapt
$ cd cutycapt/CutyCapt
Before compilation on Fedora, you need to [patch][2] source code.
Open CutyCapt.hpp with a text editor, and add the following two lines at the beginning of the file.
#include <QNetworkReply>
#include <QSslError>
Finally, compile and install CutyCapt as follows.
$ qmake-qt4
$ make
$ sudo cp CutyCapt /usr/local/bin/cutycapt
### Install CutyCapt on CentOS or RHEL ###
First [enable EPEL repository][3] on your Linux. Then follow the same procedure as in Fedora to build and install CutyCapt.
### Convert HTML to PNG with CutyCapt ###
To take a screenshot of an HTML page as a PNG image, simply run CutyCapt in the following format.
$ cutycapt --url=http://www.cnn.com --out=cnn.png
To save an HTML page to a different format (e.g., PDF), simply specify the output file appropriately.
$ cutycapt --url=http://www.cnn.com --out=cnn.pdf
The following shows command-line options of cutycapt.
![](http://farm3.staticflickr.com/2811/12542271814_3c2563d405_z.jpg)
### Convert HTML to PNG with CutyCapt on a Headless Server ###
While CutyCapt is a CLI tool, it requires an X server running. If you attempt to run CutyCapt on a headless server, you will get the error:
cutycapt: cannot connect to X server :0
If you want to run CutyCapt on a headless server without X windows, you can set up Xvfb (lightweight "fake" X11 server) on the server, so that CutyCapt does not complain.
To install Xvfb on Debian, Ubuntu or Linux Mint:
$ sudo apt-get install xvfb
To install Xvfb on Fedora, CentOS or RHEL:
$ sudo yum install xvfb
After installing Xvfb, proceed to run CutyCapt as follows.
$ xvfb-run --server-args="-screen 0, 1280x1200x24" cutycapt --url=http://www.cnn.com --out=cnn.png
It will launch Xvfb server first, and then use CutyCapt to screen capture the webpage. So it may take longer. If you want to make multiple screenshots, you may want to start Xvfb server as a background daemon beforehand.
--------------------------------------------------------------------------------
via: http://xmodulo.com/2014/02/convert-html-web-page-png-image-linux.html
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://cutycapt.sourceforge.net/
[2]:https://github.com/hoehrmann/CutyCapt/issues/3
[3]:http://xmodulo.com/2013/03/how-to-set-up-epel-repository-on-centos.html

View File

@ -0,0 +1,85 @@
如何在Linux上将HTML页面转化成png图片
================================================================================
将一个特定页面抓取为一张png图片的最简单的方法是使用[CutyCapt][1]这是一种在Linux下的方便地将HTML网页转化成不同向量和位图图像格式的命令行工具比如SVG, PDF, PS, PNG, JPEG, TIFF, GIF。CutyCapt内部使用WebKit渲染引擎来导出网页渲染输出到图片文件中。它使用Qt构建CutyCapt实际上是一个也可以在Windows上使用的跨平台应用。
在本篇教程中,我会描述如何**将一个HTML网页使用CutyCapt转化成png图片**。
### 在Linux上 CutyCapt###
这是在特定Linux发行版上的安装命令。
**在Debian, Ubuntu 或者 Linux Mint 安装 CutyCapt **
$ sudo apt-get install cutycapt
**在Fedora上安装 CutyCapt**
$ sudo yum install subversion qt-devel qtwebkit-devel gcc-c++ make
$ svn co svn://svn.code.sf.net/p/cutycapt/code/ cutycapt
$ cd cutycapt/CutyCapt
在Fedora上编译前你需要在源码上[打上补丁][2]。
使用文本编辑器打开CutyCapt.hpp,并且加入在文件的开头加上下面的两行。
#include <QNetworkReply>
#include <QSslError>
最后如下编译并安装CutyCapt。
$ qmake-qt4
$ make
$ sudo cp CutyCapt /usr/local/bin/cutycapt
### 在CentOS 或者 RHEL安装 CutyCapt ###
首先在你的Linux上[启用EPEL仓库][3]。接着和在Fedora上一样使用相同的步骤编译安装。
### 使用CutyCapt将 HTML 转化成 PNG ###
将一个HTML页面截图成一个png图片只要使用下面的格式运行CutyCapt。
$ cutycapt --url=http://www.cnn.com --out=cnn.png
要将HTML页面保存成不同的格式比如PDF只要适当地指定输出文件。
$ cutycapt --url=http://www.cnn.com --out=cnn.pdf
下图显示了CutyCapt命令选项。
![](http://farm3.staticflickr.com/2811/12542271814_3c2563d405_z.jpg)
### 在一台不含X的服务器上使用CutyCapt将HTML转换成PNG ###
虽然CutyCapt是一个命令行工具但是它需要X服务运行。如果你尝试在不含X服务的机器上运行你会得到下面这个错误
cutycapt: cannot connect to X server :0
如果你要不含X的服务器上运行CutyCapt你可以在服务器上安装Xvfb轻量级“假的”X11 服务。这样CutyCapt就不会报错了。
要在Debian, Ubuntu 或者 Linux Mint 上安装Xvfb
$ sudo apt-get install xvfb
要在Fedora, CentOS 或者 RHEL 上安装Xvfb:
$ sudo yum install xvfb
在安装Xvfb之后接下来像这样运行CutyCapt。
$ xvfb-run --server-args="-screen 0, 1280x1200x24" cutycapt --url=http://www.cnn.com --out=cnn.png
它首先会运行Xbfb服务接着使用CutyCapt来抓取网页。因此它可能会花费更长的时间。如果你想要截图多张截图你可能事先需要将Xvfb作为后台守护进程启动。
--------------------------------------------------------------------------------
via: http://xmodulo.com/2014/02/convert-html-web-page-png-image-linux.html
译者:[geekpi](https://github.com/geekpi) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://cutycapt.sourceforge.net/
[2]:https://github.com/hoehrmann/CutyCapt/issues/3
[3]:http://xmodulo.com/2013/03/how-to-set-up-epel-repository-on-centos.html