Giter Club home page Giter Club logo

java-phantomjs-wrapper's Introduction

phantomjs-wrapper

Build Status Maven Central Dependency Status

A Java wrapper around the PhantomJS binaries with additional support for rendering HTML from an InputStream

Install

Use maven to install this module and its dependencies.

<dependency>
  <groupId>com.moodysalem</groupId>
  <artifactId>phantomjs-wrapper</artifactId>
  <version>see pom.xml</version>
</dependency>

3.0 Optional binary dependencies

In 3.0, all the phantom JS binaries are included in separate modules declared as optional dependencies. If you are building a cross platform app, you will need to include all 3. Otherwise, you can include only the optional dependency for the target Operating System.

  <dependency>
    <groupId>com.moodysalem</groupId>
    <artifactId>phantomjs-wrapper-windows-binary</artifactId>
    <version>see pom.xml</version>
  </dependency>

  <dependency>
    <groupId>com.moodysalem</groupId>
    <artifactId>phantomjs-wrapper-linux-binary</artifactId>
    <version>see pom.xml</version>
  </dependency>

  <dependency>
    <groupId>com.moodysalem</groupId>
    <artifactId>phantomjs-wrapper-macosx-binary</artifactId>
    <version>see pom.xml</version>
  </dependency>

JS Execution

The phantomJS script bundled with this wrapper (render.js) to support the public render method described below includes logic for waiting for JS to complete execution before attempting to render the page. A page is considered rendered under any of these conditions:

  • PageRendered is undefined
  • PageRendered is a boolean and equal to true
  • PageRendered is a function and its return value is truthy

Public Interface

/**
 * Another way to call PhantomJS#render using the RenderOptions to specify all the common options
 *
 * @param html    to render
 * @param options for rendering
 * @return same as PhantomJS#render
 * @throws IOException
 * @throws RenderException
 */
public static InputStream render(InputStream html, RenderOptions options) throws IOException, RenderException;

/**
 * Render the html in the input stream with the following properties using a script included with the wrapper
 *
 * @param html         to render
 * @param paperSize    size of the paper (for printed output formats)
 * @param dimensions   dimensions of the viewport
 * @param margin       of the paper
 * @param headerInfo   how the header is generated
 * @param footerInfo   how the footer is generated
 * @param renderFormat the format to render
 * @param jsWait       the maximum amount of time to wait for JS to finish execution in milliseconds
 * @param jsInterval   the interval
 * @return a stream of the rendered output
 * @throws IOException     if any file operations fail
 * @throws RenderException if the render script fails for any reason
 */
public static InputStream render(InputStream html, PaperSize paperSize, ViewportDimensions dimensions,
                                 Margin margin, BannerInfo headerInfo, BannerInfo footerInfo,
                                 RenderFormat renderFormat, Long jsWait, Long jsInterval) throws IOException, RenderException;


/**
 * Execute a script with options and a list of arguments
 *
 * @param script    path of script to execute
 * @param options   options to execute
 * @param arguments list of arguments
 * @return the exit code of the script
 * @throws IOException if cmd execution fails
 */
public static PhantomJSExecutionResponse exec(InputStream script, PhantomJSOptions options, CommandLineArgument... arguments) throws IOException;

java-phantomjs-wrapper's People

Contributors

autumn-moon avatar guillaumecleme avatar jpraet avatar moodysalem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

java-phantomjs-wrapper's Issues

Java 7 Support?

I have a big project I'd like to use your PhantomJS wrapper for that is currently on Java version 7. The maven artifact you distributed seems to be compiled using Java 8 bytecode, hence it doesn't work on a Java 7 JRE.

I noticed the code uses a few Java 8 language features. I forked your repository and created a version that works with Java 7. Would you be willing to create a distributed version for Java 7?

Also, I forked off of the latest version 2 release. The logging change was causing a dependency conflict on my project (I didn't dig too deep into why), but we could potentially create two pull requests to support version 2 and 3 if that works. The effort is pretty minimal.

Thanks!

PhantomJSSetup shutdown hook fails when source dir does not exist

[WARNING] PhantomJSSetup was unable to clean up temporary directories under: [D:\babun\.babun\cygwin\tmp\java-phantomjs]. Caused by:
java.nio.file.NoSuchFileException: D:\babun\.babun\cygwin\tmp\java-phantomjs\source-c8ec28df-6b5e-403b-9244-b0cdad298dad
        at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
        at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
        at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
        at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269)
        at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
        at java.nio.file.Files.delete(Files.java:1126)
        at com.moodysalem.phantomjs.wrapper.PhantomJSSetup$1.run(PhantomJSSetup.java:202)

The shutdown hoop attempts to delete these dirs:

Files.delete(PhantomJSConstants.TEMP_SOURCE_DIR);
Files.delete(PhantomJSConstants.TEMP_SCRIPT_DIR);
Files.delete(PhantomJSConstants.TEMP_RENDER_DIR);

But I'm not using PhantomJS.render(), only PhantomJS.exec(), so the source and render dirs are never created in the first place. And because the deletion of the source dir fails, the scripts dir does not get cleaned up either.

Not render css

On invocation:

PhantomJS.render(null,
                new URL("http://www.myUrl.com").openStream(),
                PaperSize.A4, ViewportDimensions.VIEW_1280_1024,
                Margin.ZERO, BannerInfo.EMPTY, BannerInfo.EMPTY, RenderFormat.PDF, 10000L, 100L);

The html is rendered but without css.

What i'm doing wrong?

not render javascript files

hi ... javascript files are not loaded in java-phantomjs wrapper.please tell me how to include js files. it has taken <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script> like this.But it does not take <script src="jquery-3.3.3"></script>.How can i fix this .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.