PhantomJS生成PDF

当需要将页面原样输出为图片或PDF时,可以使用PhantomJS进行抓取
(官宣PhantomJS开发暂停,最新稳定版本phantomjs-2.1.1)

下载文件

官网 Download phantomjs-2.1.1-linux-x86_64.tar.bz2(22.3 MB) and extract the content.
上传至Linux服务器

修改screenshot.js

"use strict";
var page = require('webpage').create();
var system = require('system');

var address = system.args[1];
var output = system.args[2];

page.viewportSize = {
  width: 2400,
  height: 20000
};

page.zoomFactor = 1;
page.settings.loadImages = true;
page.settings.resourceTimeout = 30000;
page.settings.userAgent = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36';

page.open(address, function(status) {

    function checkReadyState() {
        setTimeout(function () {
            var readyState = page.evaluate(function () {
                return document.readyState;
            });
            if ("complete" === readyState) {               
                page.paperSize = { 
                    format: 'A4',
                    orientation: 'portrait', 
                    margin: '0.8cm' 
                };
                page.render(output);
                console.log("生成成功");
                console.log("$"+output+"$");
                phantom.exit(); 

            } else {
                checkReadyState();
            }
        },1000);
    }
    checkReadyState();
});

修改文件权限

执行Linux指令
    /** chmod能改变权限,-R是目录下所有文件,777就是高权限(读、写、执行) */
    chmod -R 777 /usr/local/phantomjs-2.1.1-forpdf

Java代码

public String phantomjsPdf(String orderNo,HttpServletRequest request){
    /*phantomjs在服务器的路径*/
    String phantomjs = "/usr/local/phantomjs-2.1.1-forpdf/bin/phantomjs";
    /*截图js在服务器的路径*/
    String screenshot = "/usr/local/phantomjs-2.1.1-forpdf/bin/screenshot.js";
    /*要生成图片的链接*/
    String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+"/product/getPdfPage.do?orderNo="+orderNo;
    /*生成的图片路径*/
    String time = new Date().getTime()+"";
    String imgPath = request.getSession().getServletContext().getRealPath("/")+"/images/upload/image/"+"product_agreement/"+time+".pdf";
    /*linux命令*/
    String shell = phantomjs+" "+screenshot+" "+url+" "+imgPath;
    MyUtil.shell(shell);
    return "/images/upload/image/product_agreement/"+time+".pdf";
}

注:1. url中参数传多个时,shell中的url必须使用''包裹,否则会造成参数丢失
   2. url参数中包含中文时,需将参数进行转码,否则会发生乱码

/*MyUtil.shell() 执行shell命令*/
public static void shell(String cmd) {
    String[] cmds = { "/bin/sh", "-c", cmd };
    Process process;
    try {
        process = Runtime.getRuntime().exec(cmds);
        StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "Error");
        StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "Output");
        errorGobbler.start();
        outputGobbler.start();
        try {
            process.waitFor();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

  转载请注明: 迷失の信徒 PhantomJS生成PDF

 本篇
PhantomJS生成PDF PhantomJS生成PDF
当需要将页面原样输出为图片或PDF时,可以使用PhantomJS进行抓取(官宣PhantomJS开发暂停,最新稳定版本phantomjs-2.1.1) 下载文件官网 Download phantomjs-2.1.1-linux-x86_64
2019-03-20 linhai
下一篇 
PDF添加图片印章-JAVA PDF添加图片印章-JAVA
使用Free Spire.PDF for Java 产品,为PDF添加印章① 下载JAR包或使用Maven仓库② Java代码示例 //创建PdfDocument对象,加载PDF文档 PdfDocument doc = new PdfDoc
2019-03-17 linhai
  目录