Google Analytics

顯示具有 PhantomJS 標籤的文章。 顯示所有文章
顯示具有 PhantomJS 標籤的文章。 顯示所有文章

2015年8月5日 星期三

使用PhantomJS實作網頁截圖功能2

承上篇(使用PhantomJS實作網頁截圖功能)

是之前rasterize.js產生網頁截圖時會在指定的路徑下產生實體檔案,但有些情況下也許不希望有這些圖片檔案產生(而希望直接render到網頁中)。此時就需要改寫原本的script。

關鍵API

使用renderBase64()的方法會把原本要產生的圖片用base64的編碼方式轉成字串,可以列印至 console,伺服器端的程式再去讀此字串並以base64解碼。

page.renderBase64('PNG');

改寫網頁截圖script

改寫PhantomJS附的網頁截圖sample code,放在PhantomJS目錄下的examples/rasterize.js:
var page = require('webpage').create(),
    system = require('system'),
    address, output, size;

if (system.args.length < 3 || system.args.length > 5) {
    console.log('Usage: screenshot.js URL filename [imagewidth*imageheight|paperwidth*paperheight|paperformat] [zoom]');
    console.log('  image (png output) examples: "1024*768"');
    console.log('  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
    phantom.exit(1);
} else {
    address = system.args[1];
    output = system.args[2];
    var clipRect = null;
 
    page.viewportSize = { width: 600, height: 600 };
    if (system.args.length > 3) {
        size = system.args[3].split('*');
     
        clipRect = {top: 0, left:0, width: size[0], height: size[1]};
    }
    if (system.args.length > 4) {
        page.zoomFactor = system.args[4];
    }
 
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
            phantom.exit();
        } else {
            window.setTimeout(function () {              
                try {
                    if (clipRect) {
                        page.clipRect = clipRect;
                    }

                    var base64 = page.renderBase64(output);
                    console.log(base64);
                } catch (e) {
                    console.log('Failed to capture screenshot as ' + output + ': ' + e, "error");
                }
             
                phantom.exit();
            }, 200);
        }
    });
}

伺服機端(Java Servlet)程式

       try {
            long startTime = System.currentTimeMillis();
           
            String targetUrl = "http://www.google.com.tw/";
            String phantomExeFileName ="C:/application/phantomjs-1.9.7/phantomjs.exe";
            String phantomJsFileName ="C:/application/phantomjs-1.9.7/myJs/screenshotBase64.js";
            
            File phantomExeFile = new File(phantomExeFileName);
            File phantomJsFile = new File(phantomJsFileName);
            String imageSize = "1024*768";
           
                String execCommand = String.format("%s %s %s %s %s",
                            phantomExeFile.getAbsolutePath(),
                            phantomJsFile.getAbsolutePath(),
                            (targetUrl == null) || (targetUrl.length() ==0) ? "": targetUrl,
                            "png",
                             imageSize
                        ).trim();
               
                System.out.println(String.format("execCommand = [%s]", execCommand));
               
              Scanner scanner = new Scanner(Runtime.getRuntime().exec(execCommand).getInputStream()).useDelimiter("\\A");
              String cmdout =  scanner.hasNext() ? scanner.next() : "";
              System.out.println(String.format("cmdout = [%s]", cmdout));
             
                byte[] outImg = null;
               
                if ((cmdout != null) && (cmdout.length() > 0)) {
                    outImg = Base64.decodeBase64(outImg);
                }
                 
                OutputStream out = resp.getOutputStream();
               
                if(outImg != null) {
                    InputStream is = new ByteArrayInputStream(outImg);
                    resp.setContentType("image/png");
                    resp.setContentLength(outImg.length);
                    byte[] buf = new byte[4096];
                    while(true) {
                        int r = is.read(buf, 0, buf.length);
                        if(r == -1) break;
                        out.write(buf, 0, r);
                    }
                    is.close();
                    out.flush();
                }             

                long endTime = System.currentTimeMillis();
                System.out.println("total "new Double((endTime - startTime) / 1000) + " seconds");
               
        } catch (Exception e) {
            logger.error("main() - exception: ", e);
        }
       

        return resp;


2014年8月1日 星期五

使用PhantomJS實作網頁截圖功能

簡介

PhantomJS(http://phantomjs.org/)是一個讓使用者可以使用javascript 控制WebKit排版引擎(主要設計是用來讓網頁瀏覽器繪製網頁)的API套件。筆者使用過後讚嘆不已,只能說:簡單!強大!好用!

安裝方式

其實不需要安裝啦!去官網下載後,直接解壓縮放到某一個目錄即可,若是windows會在解壓縮目錄下找到phantomjs.exe,直接在command line執行寫好的PhantomJS javascript即可(如hello.js)。

phantom hello.js

撰寫網頁截圖script

其實PhantomJS已經有附一個簡易的網頁截圖sample code,放在PhantomJS目錄下的examples/rasterize.js(命名比較奇怪,所以容易找不到):
var page = require('webpage').create(),
    system = require('system'),
    address, output, size;

if (system.args.length < 3 || system.args.length > 5) {
    console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
    console.log('  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
    phantom.exit(1);
} else {
    address = system.args[1];
    output = system.args[2];
    page.viewportSize = { width: 600, height: 600 };
    if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
        size = system.args[3].split('*');
        page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
                                           : { format: system.args[3], orientation: 'portrait', margin: '1cm' };
    }
    if (system.args.length > 4) {
        page.zoomFactor = system.args[4];
    }
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
            phantom.exit();
        } else {
            window.setTimeout(function () {
                page.render(output);
                phantom.exit();
            }, 200);
        }
    });
}

rasterize.js支援4個參數,依序介紹如下:
  1. (必填)要被截圖的網址,如:http://www.google.com.tw
  2. (必填)截圖完成時儲存的檔案名稱,如:c:\pageCapture.png
  3. (選填)若截圖為pdf時(用截圖檔案結尾為"pdf"做判斷),pdf的紙張大小
  4. (選填)若截圖為pdf時(用截圖檔案結尾為"pdf"做判斷),pdf的zoom size

執行結果


但這個範例的最大問題在於截圖的大小固定為600*600 px,所以在實用上需要再做一點修改,以下為將rasterize.js改為可以接收以px為單位的截圖大小:
var page = require('webpage').create(),
    system = require('system'),
    address, output, size;

if (system.args.length < 3 || system.args.length > 5) {
    console.log('Usage: screenshot.js URL filename [imagewidth*imageheight|paperwidth*paperheight|paperformat] [zoom]');
    console.log('  image (png output) examples: "1024*768"');
    console.log('  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
    phantom.exit(1);
} else {
    address = system.args[1];
    output = system.args[2];
    var clipRect = null;
   
    page.viewportSize = { width: 600, height: 600 };
    if (system.args.length > 3) {
        size = system.args[3].split('*');
        console.log('size[0] = ' + size[0] + ', size[1] = ' + size[1]);
       
        if (system.args[2].substr(-4) === ".pdf") {
            page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
                                               : { format: system.args[3], orientation: 'portrait', margin: '1cm' };
        } else {
            clipRect = {top: 0, left:0, width: size[0], height: size[1]};
        }
    }
    if (system.args.length > 4) {
        page.zoomFactor = system.args[4];
    }
   
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
            phantom.exit();
        } else {
            window.setTimeout(function () {
               
                try {
                    if (clipRect) {
                        page.clipRect = clipRect;
                        console.log('Capturing page to ' + output + ' with clipRect' + JSON.stringify(clipRect));
                    } else {
                        console.log('Capturing page to ' + output);
                    }
                    page.render(output);
                } catch (e) {
                    console.log('Failed to capture screenshot as ' + output + ': ' + e, "error");
                }
               
                console.log('Capture screenshot success!');
                phantom.exit();
            }, 200);
        }
    });

}

使用Java呼叫PhantomJS

在java程式內,可以使用Runtime物件的exec()方法產生image。
String execCommand = "C:/phantomjs-1.9.7/phantomjs.exe C:/phantomjs-1.9.7/myJs/screenshot.js http://www.google.com.tw c:/pageCapture.png 1024*768";

Process process = Runtime.getRuntime().exec(execCommand);

process.waitFor();