背景

    因为某些原因,某些人喜欢看pdf文件而不是网页。所以需要一个工具来把HTML文件装换为PDF格式的文件。

    chrome 里面装一个插件也能做到:Scren Capture。把它转换为图片然后想怎么弄就怎么弄了。

    正好 nixCraft 上面有 一篇文章 介绍,我就尝试了下 wkhtmltopdf ,效果不错。

    wkhtmltopdf

    功能

    • Cross platform.
    • Open source.
    • Convert any web pages into PDF documents using webkit.
    • You can add headers and footers.
    • TOC generation.
    • Batch mode conversions.
    • Can run on Linux server with an XServer (the X11 client libs must be installed).
    • Can be directly used by PHP or Python via bindings to libwkhtmltox.

    安装

    archlinux 感谢 Yaourt 的方便:

    yaourt -S wkhtmltopdf
    ln -s $(which wkhtmltopdf) ~/bin/html2pdf
    

    使用

    语法:

    html2pdf http://www.cyberciti.biz/path/to/url.html output.pdf
    html2pdf http://www.cyberciti.biz/blog/print/url-slut.html output.pdf
    html2pdf -option1  -option2 http://www.cyberciti.biz/blog/print/url-slut.html output.pdf
    

    安装 pdfinfo 可以查看 PDF 文件的相关信息。

    yaourt -S poppler
    pdfinfo foo.pdf
    

    举个例子:

    $ html2pdf http://sydi.org /tmp/sydi.org.pdf
    Fontconfig warning: "/etc/fonts/conf.d/50-user.conf", line 9: reading configurations from ~/.fonts.conf is deprecated.
    Loading page (1/2)
    content-type missing in HTTP POST, defaulting to application/x-www-form-urlencoded. Use QNetworkRequest::setHeader() to fix this problem.
    QFont::setPixelSize: Pixel size <= 0 (0)
    Printing pages (2/2)                                               
    QFont::setPixelSize: Pixel size <= 0 (0)
    Done
    $ pdfinfo /tmp/sydi.org.pdf 
    Title:          
    Creator:        
    Producer:       Qt 4.8.3 (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
    CreationDate:   Thu Oct 18 06:01:37 2012
    Tagged:         no
    Form:           none
    Pages:          3
    Encrypted:      no
    Page size:      595 x 842 pts (A4)
    Page rot:       0
    File size:      203003 bytes
    Optimized:      no
    PDF version:    1.4
    

    灰度pdf

    $ html2pdf -g http://sydi.org /tmp/sydi.org.pdf
    

    所有选项

    $ html2pdf --help
    $ html2pdf -H | less