mac配置wkhtmltopdf

安装依赖:git, wget, gcc, xcode, X11.


wkhtmltopdf是一个将html页面转成pdf的工具。

这段时间公司涉及到将html转成pdf/jpg的相关功能,我查了很多资料,网上大能找到的方法主要有以下两种:

a)利用swt的panel.setPage(url) 和Graphics2D结合

b)HtmlImageGenerator

 

1)下载wkhtmltopdf-qt 

$ git clone git://gitorious.org/+wkhtml2pdf/qt/wkhtmltopdf-qt.git
$ cd wkhtmltopdf-qt

2)编译wkhtmltopdf-qt(这阶段时间可能会比较久)

$ ./configure -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -arch x86_64 -release -static -fast -exceptions -no-accessibility -no-stl -no-sql-ibase -no-sql-mysql -no-sql-odbc -no-sql-psql -no-sql-sqlite -no-sql-sqlite2 -no-qt3support -xmlpatterns -no-phonon -no-phonon-backend -webkit -no-scripttools -no-mmx -no-3dnow -no-sse -no-sse2 -no-ssse3 -qt-zlib -qt-libtiff -qt-libpng -qt-libmng -qt-libjpeg -openssl -graphicssystem raster -opensource -nomake "tools examples demos docs translations" -no-opengl -no-dbus -no-framework -no-dwarf2 -no-multimedia -no-declarative -largefile -rpath -no-nis -no-cups -no-iconv -no-pch -no-gtkstyle -no-nas-sound -no-sm -no-xshape -no-xinerama -no-xfixes -no-xrandr -no-xrender -no-mitshm -no-xkb -no-glib -no-openvg -no-xsync -no-javascript-jit -no-egl -carbon --prefix=../wkqt/
$ make -j3
$ make install

3)退出wkhtmltopdf-qt目录,此时会生成一个wkqt目录。下载并解压wkhtmltopdf-0.11.0_rc1.tar.bz2

$ wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1.tar.bz2
$ tar xvjf wkhtmltopdf-0.11.0_rc1.tar.bz2
$ rm wkhtmltopdf-0.11.0_rc1.tar.bz2
$ cd wkhtmltopdf-0.11.0_rc1

4)修改src/image/image.pro和src/pdf/pdf.pro文件 

macx {
#    CONFIG -= app_bundle
    CONFIG += x86_64
}

 5)保持在目录wkhtmltopdf-0.11.0_rc1,编译wkhtmltopdf和wkhtmltoimage

$ ../wkqt/bin/qmake
$ make
$ sudo make install

6)绑定生成/bin/wkhtmltopdf.app和/bin/wkhtmltoimage.app(这里需要用到qt里的资源文件)。

$ cd wkhtmltopdf-qt
$ sudo cp -pr src/gui/mac/qt_menu.nib /bin/wkhtmltopdf.app/Contents/Resources
$ sudo cp -pr src/gui/mac/qt_menu.nib /bin/wkhtmltoimage.app/Contents/Resources
$ sudo ln -s /bin/wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf /usr/local/bin
$ sudo ln -s /bin/wkhtmltoimage.app/Contents/MacOS/wkhtmltoimage /usr/local/bin

7)确定编译成功wkhtmltopdf --version 

timelyxyz-MacBook-Pro:bin apple$ wkhtmltopdf --version
Name:
  wkhtmltopdf 0.10.0 rc2

License:
  Copyright (C) 2010 wkhtmltopdf/wkhtmltoimage Authors.



  License LGPLv3+: GNU Lesser General Public License version 3 or later
  <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to
  change and redistribute it. There is NO WARRANTY, to the extent permitted by
  law.

Authors:
  Written by Jan Habermann, Christian Sciberras and Jakob Truelsen. Patches by
  Mehdi Abbad, Lyes Amazouz, Pascal Bach, Emmanuel Bouthenot, Benoit Garret and
  Mário Silva.

上面提到的swt的方法具体代码

/**
  * 根据构造函数内容将url抓屏后的页面生成图片放在file路径对应位置
  * 
  * @throws Exception
  */
  public String producePic() throws Exception {
      JEditorPane editorPane = new JEditorPane();
      editorPane.setEditable(false);
      editorPane.setPage(url);
      JScrollPane jsp = new JScrollPane(editorPane);
      getContentPane().add(jsp);
      this.setLocation(0, 0);
      this.setVisible(true);

      Thread.sleep(5 * 1000);

      setSize(10000, 10000);
      pack();
      BufferedImage image = new BufferedImage(editorPane.getWidth(),editorPane.getHeight(), BufferedImage.TYPE_INT_RGB);
      Graphics2D graphics2D = image.createGraphics();
      editorPane.paint(graphics2D);

      BufferedImage image1 = resize(image, 600, 400);

      ImageIO.write(image1, "jpg", file);
      dispose();
      return file.getName();
}

 

 

 

posted on 2012-12-24 20:33  timelyxyz  阅读(3699)  评论(0编辑  收藏  举报

导航