HTML to PDF converter for Java and .NET

HOME   FEATURES   PRODUCTS   DOWNLOADS   BUY NOW!   SUPPORT
<< back

PD4ML: Launching of PDF Viewer

Very often in desktop application scenarios you need to open just-generated document in a PDF viewer application.

The classic appraoch - works with any JDK:

String params = "C:\\Program Files\\Adobe\\Reader 9.0\\Reader\\AcroRD32.exe " + pdfFileName;
Runtime.getRuntime().exec(params);
Exact version number of "C:\\Program Files\\Adobe\\Reader 9.0\\Reader\\AcroRD32.exe" can be determined by Adobe folder scan during the application startup.

A necessity to know the exact path to the Acroread binary appears as a disadvantage of the method, but it also has some benefits: it allows you to pass additional command line parameters with the document. Here you may see what the parameters are for.

On MacOS let the web browser determine which application is to run to show a particular document:

String params = "open /Applications/Safari.app " + resultPdf.getAbsolutePath();
Runtime.getRuntime().exec(params);
If you use SWT, probably the best approach is as follows:
org.eclipse.swt.program.Program.launch("file:" + resultPdf.getAbsolutePath());	
Also should work on Win32:
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + resultPdf.getAbsolutePath() );			
Java 6 simplifies the task on all platforms:
if (Desktop.isDesktopSupported()) {
	Desktop.getDesktop().open(resultPdf);
} else {
	System.out.println("Awt Desktop is not supported!");
}
The complete code looks like that:
package samples;

import java.awt.Desktop;
import java.awt.Insets;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.InvalidParameterException;

import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
import org.zefer.pd4ml.PD4PageMark;

public class PdfViewerStarter {
	protected int topValue = 10;
	protected int leftValue = 20;
	protected int rightValue = 10;
	protected int bottomValue = 10;
	protected int userSpaceWidth = 1300;

	public static void main(String[] args) {
		try {
			PdfViewerStarter jt = new PdfViewerStarter();
			jt.doConversion("http://old.pd4ml.com/sample.htm", "c:/pd4ml.pdf");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doConversion( String url, String outputPath ) 
					throws InvalidParameterException, MalformedURLException, IOException {
			File output = new File(outputPath);
			java.io.FileOutputStream fos = new java.io.FileOutputStream(output);

			PD4ML pd4ml = new PD4ML();
			pd4ml.setHtmlWidth(userSpaceWidth);
			pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
			pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));
			pd4ml.useTTF("c:/windows/fonts", true);

			pd4ml.render(new URL(url), fos);
			fos.close();

			if (Desktop.isDesktopSupported()) {
				Desktop.getDesktop().open(output);
			} else {
				System.out.println("Awt Desktop is not supported!");
			}			
			
			System.out.println( outputPath + "\ndone." );
	}
}
Copyright ©2004-24 zefer|org. All rights reserved. Bookmark and Share