Loading HTML from a Notes URL with LotusScript and Java

Sometimes it's needed to connect to a Notes URL and download the HTML to disk so DominoPDF can process it locally, manipulate the code, etc.

LotusScript has the web retriever method but it doesn't provide all the necessary support for connectivity, authentication, etc.

The following code shows a method of calling a Java class from LotusScript to open a URL and retrieve the HTML.

1. Open the script library section in the Notes designer and create a new Java library named GetHTML.

2. Paste the following code into the library and save accordingly.

import java.io.*;
import java.net.*;
import sun.misc.BASE64Encoder;

public class GetHTML {
   
   public String getHTML(String urlToRead, String username, String password) {
      URL url; // The URL to read
      HttpURLConnection conn; // The actual connection to the web page
      BufferedReader rd; // Used to read results from the web page
      String line; // An individual line of the web page HTML
      String result = ""; // A long string containing all the HTML
      try {
         url = new URL(urlToRead);
         conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("GET");
         
         String userpass = username + ":" + password;
         
         BASE64Encoder encoder = new BASE64Encoder();
         String encodedStr = encoder.encode(userpass.getBytes());
         
         String basicAuth = "Basic " + encodedStr;
         conn.setRequestProperty ("Authorization", basicAuth);
         
         rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         while ((line = rd.readLine()) != null) {
            result += line;
         }
         rd.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return result;
   }
}

3. Create your LotusScript agent which will handle the URL loading and paste the following code.

  1. Dim js As JAVASESSION
  2. Dim getHTMLClass As JAVACLASS
  3. Dim getHTMLObject As JavaObject
  4. Dim html As String
  5. Set js = New JAVASESSION
  6. Set getHTMLClass = js.GetClass("GetHTML")
  7. Set getHTMLObject = getHTMLClass.CreateObject
  8. html = getHTMLObject.getHTML("http://www.google.com", "username", "password")

4. On execution the variable html will contain the HTML output of the URL

5. If you wish to save the HTML to a local file for processing with DominoPDF, the following code can be used.

  1. Dim fileNum As Integer
  2. Dim fileName As String
  3. fileNum% = FreeFile()
  4. Open fileName$ For Output As fileNum%
  5. Print #fileNum%, html
  6. Close fileNum%

6. DominoPDF can be used to convert the local file to PDF as follows.

  1. Call DoPDF(fileName, "test.pdf", "")
JoomShaper