java利用phantomjs进行截图实例教程

2025-05-29 0 97

前言

最近工作中遇到一个需求,需要实现截图功能,断断续续查找资料、验证不同的实现方法终于算基本搞定了页面截图,因为中间过程曲折花费较多时间,分享出来帮助大家快速实现截图

为什么选用phantomjs进行截图

截图可以实现的方式有很多,比如:

  • selenium
  • htmlunit
  • html2image、、、and so on但是这些实现的截图效果都不好。selenium只能实现截屏,不能截取整个页面,而htmlunit、html2image对js的支持效果并不好,截下来的图会有很多空白。phantomjs就是万精油了,既能截取整个页面,对js支持的效果又好

plantomjs提供了如 css 选择器、dom操作、json、html5、canvas、svg 等。phantomjs 的用处很广泛,如网络监控、网页截屏、页面访问自动化、无需浏览器的 web 测试等,这里只用到网页截屏。

前期准备

安装phantomjs。mac os

?

1
brew install phantomjs

命令行的方式进行截图

安装以后我们就可以小试牛刀了

打开终端,输入以下命令:

?

1

2

3

4
/users/hetiantian/softwares/phantomjs/bin/phantomjs

/users/hetiantian/softwares/phantomjs/examples/rasterize.js

https://juejin.im/post/5bb24bafe51d450e4437fd96

/users/hetiantian/desktop/juejin-command.png

查看效果

java利用phantomjs进行截图实例教程

发现图片没有加载好

来看以下刚刚的命令行:

/users/hetiantian/softwares/phantomjs/bin/phantomjs:phantomjs可执行文件保存地址
/users/hetiantian/softwares/phantomjs/examples/rasterize.js:rasterize.js文件地址

这段命令可以理解为用phantomjs去运行rasterize.js文件,所以要想解决图片空白的问题我们需要去看一下rasterize.js文件。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49
"use strict";

var page = require('webpage').create(),

system = require('system'),

address, output, size, pagewidth, pageheight;

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"');

console.log(' image (png/jpg output) examples: "1920px" entire page, window width 1920px');

console.log(' "800px*600px" window, clipped to 800x600');

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' };

} else if (system.args.length > 3 && system.args[3].substr(-2) === "px") {

size = system.args[3].split('*');

if (size.length === 2) {

pagewidth = parseint(size[0], 10);

pageheight = parseint(size[1], 10);

page.viewportsize = { width: pagewidth, height: pageheight };

page.cliprect = { top: 0, left: 0, width: pagewidth, height: pageheight };

} else {

console.log("size:", system.args[3]);

pagewidth = parseint(system.args[3], 10);

pageheight = parseint(pagewidth * 3/4, 10); // it's as good an assumption as any

console.log ("pageheight:",pageheight);

page.viewportsize = { width: pagewidth, height: pageheight };

}

}

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(1);

} else {

window.settimeout(function () {

page.render(output);

phantom.exit();

}, 200);

}

});

}

尝试一:

对page.viewportsize = { width: 600, height: 600 };产生了疑问

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java利用phantomjs进行截图实例教程 https://www.kuaiidc.com/111065.html

相关文章

发表评论
暂无评论