Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to complete the function of html picture output by PhantomJs in Java

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

The knowledge of this article "PhantomJs in Java how to complete the html picture output function" is not understood by most people, so the editor summarizes the following contents to you, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to complete the html picture output function in PhantomJs in Java" article.

i. Background

How to generate a picture in Mini Program and share it in moments? At present, the front end does not seem to have a good solution, so it can only be supported by the back end, so how can we play?

It is relatively easy to generate pictures.

Simple scenarios can be directly supported by jdk, and generally speaking, there is no too complicated logic.

I have written a logic of picture synthesis before, which is realized by awt: picture synthesis.

Universal and complex templates

Simple ones can be supported directly, but more complex ones, it is undoubtedly disgusting to let the backend support. I also searched some open source libraries for rendering html on github, and I don't know if it was the wrong posture or something, and I didn't get a very satisfactory result.

Now, how to support complex templates?

This is the guide of this article, use phantomjs to achieve html rendering, support to generate pdf, generate pictures, parse dom ok, and then demonstrate how to build a web page rendering service combined with phantomjs.

ii. Prerequisite preparation

1. Phantom.js installation

# 1. Download # # mac system wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-macosx.zip## linux system wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2## windows system # # Don't play, it's no fun # 2. If unzipping sudo su tar-jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2# reports an error, install the following # yum-y install bzip2# 3. To install # # simply, move to cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs / usr/local/bin# 4 under the bin directory. Verify whether okphantomjs-- version# outputs the version number, which means ok

2. Java dependency configuration

Maven configuration add dependency

Org.seleniumhq.selenium selenium-java 2.53.1 com.github.detro ghostdriver 2.1.0 jitpack.io https://jitpack.io

Start

The main logic to call phantomjs to render a picture with html is as follows

Public class Html2ImageByJsWrapper {private static PhantomJSDriver webDriver = getPhantomJs (); private static PhantomJSDriver getPhantomJs () {/ / set the necessary parameters DesiredCapabilities dcaps = new DesiredCapabilities (); / / ssl certificate supports dcaps.setCapability ("acceptSslCerts", true); / / screenshot supports dcaps.setCapability ("takesScreenshot", true); / / css search supports dcaps.setCapability ("cssSelectorsEnabled", true); / / js supports dcaps.setJavascriptEnabled (true) / / driver support (the second parameter indicates the path where your phantomjs engine is located, which can be viewed by which/whereis phantomjs) / / fixme. If execution is written here, you can consider whether the system is installed, and obtain the corresponding path or open to specify the path dcaps.setCapability (PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/ usr/local/bin/phantomjs"). / / create interface-less browser object return new PhantomJSDriver (dcaps);} public static BufferedImage renderHtml2Image (String url) throws IOException {webDriver.get (url); File file = webDriver.getScreenshotAs (OutputType.FILE); return ImageIO.read (file);}}

Test case

Public class Base64Util {public static String encode (BufferedImage bufferedImage, String imgType) throws IOException {ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); ImageIO.write (bufferedImage, imgType, outputStream); return encode (outputStream);} public static String encode (ByteArrayOutputStream outputStream) {return Base64.getEncoder (). EncodeToString (outputStream.toByteArray ());} @ Testpublic void testRender () throws IOException {BufferedImage img = null; for (int I = 0; I)

< 20; ++i) { String url = "https://my.oschina.net/u/566591/blog/1580020"; long start = System.currentTimeMillis(); img = Html2ImageByJsWrapper.renderHtml2Image(url); long end = System.currentTimeMillis(); System.out.println("cost: " + (end - start)); } System.out.println(Base64Util.encode(img, "png"));} III. 网络实测 操作演示:

The above is about "PhantomJs in Java how to complete html picture output function" of this article, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge content, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report