0

I am trying to convert a webpage into pdf but web page is getting load and some jquery function needs to executed on page to bring dynamic data which takes time hence page loading is not a problem here but dynamic addition of data via jquery is troubling me alot. below is code which i am using to generate pdf

String pathToPhantomJS = "/usr/bin/phantomjs" //path to your phantom js
String pathToRasterizeJS = "/home/tothenew/Desktop/rasterize.js" //path to your rasterize.js
String paperSize = "A4"
String url = "https://www.google.co.in/" //url of your web page to which you want to convert into pdf
File outputFile = File.createTempFile("sample", ".pdf") //file in which you want to save your pdf

//TODO: also do exception handling stuff . i am not doing this for simplicity

Process process = Runtime.getRuntime().exec(pathToPhantomJS + " " + pathToRasterizeJS + " " + url + " " + outputFile.absolutePath + " " + paperSize);
int exitStatus = process.waitFor(); //do a wait here to prevent it running for ever
if (exitStatus != 0) {
log.error("EXIT-STATUS - " + process.toString());
}

is there is way in java i can tell wait for page to be load or what should i do so that phantom js will capture the webpage only after its get loaded completely.

  • It seems like the loading of the page is handled by phantomjs. Other similar SO posts say that this is a problem with phantomjs. – Steampunkery Sep 29 '17 at 14:18
  • none has given me a solution that should work i have seen this post but nothing help me here https://stackoverflow.com/questions/11340038/phantomjs-not-waiting-for-full-page-load –  Sep 29 '17 at 14:25
  • i think i figure out the problem updated same in question pls check let me know if what should i do –  Sep 29 '17 at 14:29

1 Answers1

0

According to this Stack Overflow post, this is a problem with phantomjs, not Java. However, If you are just trying to get a webpage as PDF, there are other options. After a bit of google-fu, I found this website which supplies a flexible command-line tool with ample documentation, as well as a C library. Seeing as you are already using a command-line tool to get your page, implementing this one should be no problem in your code. Here is a short example:

    String wkhtmltopdf_path = "/usr/local/bin/wkhtmltopdf";
    String paperSize = "A4";
    String url = "https://www.google.com/";

    // This is where your output file is/was defined, now with error handling
    File outputFile = null; 
    try {
        //file in which you want to save your pdf
        outputFile = File.createTempFile("sample", ".pdf");
        System.out.println(outputFile.getAbsolutePath()); // Show output file path, remove this in production
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    // This is where your process runs, with error handling
    Process process = null;
    try {
        process = Runtime.getRuntime().exec(String.format("%s -s %s %s %s", wkhtmltopdf_path, paperSize, url, outputFile.getAbsolutePath()));
    } catch (IOException e) {
        e.printStackTrace(); // Do your error handling here
    }

    // This is where your exitStatus and waitFor() was/is, with error handling
    int exitStatus = 0;
    try {
        //do a wait here to prevent it running for ever
        exitStatus = process.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace(); // Do your error handling here
    }
    if (exitStatus != 0) {
        // Do error handling here
    }
Steampunkery
  • 3,839
  • 2
  • 19
  • 28
  • what does String.format("%s -s %s %s %s") will do. i didnt understand the logic of how it should wait for all page operation to be completed ? can u pls explain –  Sep 29 '17 at 15:55
  • @FerozSiddiqui that is a format string. The %s means string and the variables are substituted in sequence. https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.lang.String-java.lang.Object...- – Steampunkery Sep 29 '17 at 18:04