Linux PHP automatically generated a preview of the article, HTML into a variety of format pictures, PDF-----to phpboy from the article

Source: Internet
Author: User
Tags wkhtmltopdf support microsoft

Original address:

Use WordPress to build a blog site, choose a set of templates that can show the thumbnail of the article, after tossing the original template changed beyond recognition, the final is directly on the line, do not want to toss.

Site online Not a few days, in the company to do the project, the picture has done a magnified JS, his blog plus click Thumbnail view of the original function, and then quickly added.

After a few days, whim, want to fuss about the preview, that is, click on the thumbnail view of the article preview, that is, you are now on the homepage and the article List page to see that feature.

Do not crap, do not know when again to toss ...

Linux PHP automatically generated a preview of the article, the initial general idea is very simple, is that I can somehow invoke the browser kernel to render the Web page is HTML, and then save the HTML as a picture. Then began to plunge into Google, Baidu, search for a long time, concluded that the data is too little, want to write a C extension, call WebKit kernel. On the verge of giving up, in the forum to see Wkhtmltox, and then all the problems are Wkhtmltox, it is fortunate that in Google code inside saw Phpwkhtmltox, and then I laughed.

Install software Inventory:

A.libwkhtmltox Linux WebKit kernel (not dependent on graphical interface)
B.phpwkhtmltox PHP extension, can call the WebKit kernel to convert the Web page into various formats of pictures or PDF
C.font-chinese Chinese Font
D.msyh,consolas fonts

System environment: Linux CentOS 6.2 64-bit

1.Linux under Installation Libwkhtmltox

Libwkhtmltox is extracted from the WebKit kernel that can be copied directly to the corresponding directory of Linux, it may be thought that to render a Web page requires a graphics browser, but our VPS is generally no graphical interface, In fact, we only need to install Libwkhtmltox to Linux, do not need a graphical interface.

1.1. Download Libwkhtmltox

: Releasedate+size+downloadcount

It is necessary to note that the current version of Libwkhtmltox is already 0.11, but I installed libwkhtmltox-0.11 under CentOS 6.2, in the conversion of the Web page into a picture error, the final choice of 0.10 version, this version is still quite stable.

1.2. Installing Libwkhtmltox

Download and Unzip

1 #mkdir libwkhtmltox
2 #cd libwkhtmltox
3 #wget
4 #tar jxvf libwkhtmltox-0.10.0_rc2-amd64.tar.bz2
5 #ll
6 drwxr-xr-x 2 root root       59 Jan  3 20:58 examples
7 drwxr-xr-x 3 1000 1000       22 Nov  7  2010 include
8 drwxr-xr-x 2 1000 1000       28 Jan 31  2011 lib


1 #cd lib
2 #cp /usr/local/lib/
3 #cd ../include
4 #cp -R wkhtmltox /usr/local/include/

1.3. Installing Phpwkhtmltox

Download and Unzip

1 #wget
2 #unzip

Install as a PHP extension

1 #cd php-wkhtmltox-master
2 #phpize
3 #./configure
4 #make install

Modify the PHP configuration file, append


Reload system dynamic link library (important, otherwise PHP cannot complete extension Phpwkhtmltox)

1 #ldconfig

1.4. Check if PHP has supported Phpwkhtmltox

1 #php -m

If the output includes the Phpwkhtmltox description, the extension succeeds.

1.5. Try "Sledgehammer" to save Google home as a picture

1.5.1. Editing the test's PHP script test_html_to_image.php

1 <?php
2 wkhtmltox_convert(
3     ‘image‘,
4     array(
5         ‘out‘=> ‘/tmp/test.jpg‘,
6         ‘in‘=> ‘‘
7     )
8 );
9 ?>

1.5.2.wkhtmltox_convert ($type, $globalsetting, $objectsetting) description

HTML Conversion to PDF example:

1 <?php
2 wkhtmltox_convert(‘pdf‘,
3     array(
4         ‘out‘=> ‘/tmp/test.pdf‘,
5         ‘imageQuality‘=> ‘95‘
6     ), // global settings
7     array(
8         array(
9             ‘page‘=> ‘‘
10         ),
11         array(
12             ‘page‘=> ‘‘
13         )
14     )// object settings
15 );
16 ?>

Please refer to the specific other parameters: Http://

1.5.2. Running PHP

1 #php test_html_to_image.php

To the TMP directory can see we have to save Google's homepage as a test.jpg image, is not a very fulfilling feeling, the picture to download to the local, to view the picture, you may find that the Chinese display as garbled, do not worry, please see below.

1.6. Let CentOS support Chinese

1 #yum groupinstall chinese-support

After installation, run PHP test_html_to_image.php again, Chinese will not appear garbled, but you may feel that the Chinese display is not good to see, do not worry, please see below.

1.7. Let Linux CentOS support Microsoft Ya-Black font

1.7.1. Download or copy Microsoft Jache from a local Windows (note that there should be 2 files copied from the Windows system) and upload to the Linux server

1.7.2. Copy the font file to/usr/share/fonts/xxx, where xxx is the new font folder, such as Msyh

1 #cd /usr/share/fonts/
2 #mkdir msyh
3 #cd msyh

1.7.3. Creating a Font Cache

1 #mkfontscale
2 #mkfontdir
3 #fc-cache -fv

1.8. Let Linux CentOS support Consolas fonts (technical blogs may publish a sample code, most WordPress technology blogs will install the Syntaxhighlighter plugin, and the plugin code shows that the font preferred is the Consolas font , so the sample code looks good in order to convert HTML into pictures, and we need to have Linux support Consolas fonts as well.

1.8.1 download or copy consolas from local Windows (note that there should be 4 files copied from the Windows system) and upload to the Linux server

1.8.2. Copy the font file to/usr/share/fonts/xxx, where xxx is the new font folder, such as Consolas

1 #cd /usr/share/fonts/
2 #mkdir Consolas
3 #cd Consolas

1.8.3. Creating a Font Cache

1 #mkfontscale
2 #mkfontdir
3 #fc-cache -fv

After installation, you can run PHP test_html_to_image.php again, if you fill in the URL corresponding to the Web page using Microsoft Ya Black font, the page converted into a picture of the display effect is more beautiful.

Finally, wrote a WordPress plug-in, to achieve in saving the article automatically generated thumbnail images, the need for colleagues can leave a message left contact.

Reference Documentation:
1.libwkhtmltox Supported parameter Description Http://
2. Auto-Generate Web page (screenshot) method
3.CentOS Install Chinese Language Pack and input method

Linux PHP automatically generated a preview of the article, HTML into a variety of format pictures, PDF-----to phpboy from the article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.