レポートを印刷する方法およびプリンタを指定して印刷する方法
対象製品
Elixir Report 8.7J
詳細
レポートの印刷にはいくつかの方法があります。出力先プリンタや利用できる機能が異なりますので用途に応じて使い分けてください。
通常印刷
■サンプルコード
// 通常印刷のサンプルコード
import java.util.Properties;
// レポートサーバーAPIクラスのインポート
import com.elixirtech.ers2.client.ERSClient;
import com.elixirtech.report2.runtime.IJobInfo;
public class BasicReportSample {
public static void main(String args[]){
try{
// レポートパラメータ用プロパティオブジェクト
Properties prop = new Properties();
prop.put( "MyParam" , "valuename" );
// レポートサーバー接続管理オブジェクトの作成
ERSClient client = new ERSClient(
"localhost" , 7001 , "User1" , "password" );
// レポートサーバーへレポート印刷リクエストを発行
IJobInfo job = client.renderReport(
"/samples/demo/member/member.rml", "application/x-print",
null , prop);
}catch (Exception e){
e.printStackTrace();
}
}
}
Java Print Service(JPS)を利用した印刷
■サンプルコード
// JPSを利用した印刷のサンプルコード
import java.util.Properties;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
// レポートサーバーAPIクラスのインポート
import com.elixirtech.ers2.client.ERSClient;
import com.elixirtech.glint.print.GlintStreamPrinter;
import com.elixirtech.glint.print.PrinterUtil;
import com.elixirtech.report2.runtime.IJobInfo;
public class BasicReportSample {
public static void main(String args[]){
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// レポートパラメータ用プロパティオブジェクト
Properties prop = new Properties();
prop.put( "MyParam" , "valuename" );
// レポートサーバー接続管理オブジェクトの作成
ERSClient client = new ERSClient(
"localhost" , 7001 , "User1" , "password" );
// レポートサーバーへGlint形式のレポート生成リクエストを発行
IJobInfo job = client.renderReport(
"/samples/demo/member/member.rml", "application/x-glint",
baos , prop);
GlintStreamPrinter gsp = new GlintStreamPrinter();
HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
// 印刷属性の設定
// aset.add(new MediaPrintableArea(10, 10, 190, 277, MediaPrintableArea.MM));
// プリンタ名一覧の取得(プリンタ名の確認)
// String[] printer = PrinterUtil.getPrinters();
// 取得したプリンタ名をコンソールに出力
// for(int i=0; i<printer.length; i++){
// System.out.println("プリンタ"+ i + ": " + printer[i]);
// }
// プリンタ名の指定
PrintService ps =
PrinterUtil.getSelectedPrintService("<アプリケーション側プリンタ>");
// 印刷(ダイアログ出力なし)
gsp.printNonInteractive(ps,
new ByteArrayInputStream(baos.toByteArray()), aset, null);
}catch (Exception e){
e.printStackTrace();
}
}
}
印刷ターゲットを利用した印刷
■サンプルコード
// 印刷ターゲットを利用した印刷のサンプルコード
import java.io.ByteArrayOutputStream;
import java.util.Properties;
import com.elixirtech.ers2.client.ERSClient;
import com.elixirtech.report2.runtime.IJobInfo;
public class PrinterTargetSample {
public static void main(String args[]){
try{
// レポートパラメータ用プロパティオブジェクト
Properties prop = new Properties();
prop.put( "MyParam" , "valuename" );
// 出力ターゲット用プロパティオブジェクト
// printer-nameとしてプリンタ名を設定
Properties targetProp = new Properties();
targetProp.put("printer-name", "<レポートサーバー側のプリンタ>");
// レポートサーバー接続管理オブジェクトの作成
ERSClient client = new ERSClient(
"localhost" , 7001 , "User1" , "password" );
// レポートサーバーへGlint形式のレポート生成リクエストを発行
// 印刷ターゲット名、ターゲット用プロパティを同時に渡す
IJobInfo job = client.renderReport(
"/samples/demo/member/member.rml", "application/x-glint",
prop, "printertarget", targetProp);
}catch (Exception e){
e.printStackTrace();
}
}
}
通常印刷
- 出力形式にapplication/x-printを指定
- レポートサーバーで生成したレポートをそのまま印刷
- 出力先プリンタはレポートサーバーが動作するOSに登録されたデフォルトプリンタ。
プリンタ指定不可。
■サンプルコード
// 通常印刷のサンプルコード
import java.util.Properties;
// レポートサーバーAPIクラスのインポート
import com.elixirtech.ers2.client.ERSClient;
import com.elixirtech.report2.runtime.IJobInfo;
public class BasicReportSample {
public static void main(String args[]){
try{
// レポートパラメータ用プロパティオブジェクト
Properties prop = new Properties();
prop.put( "MyParam" , "valuename" );
// レポートサーバー接続管理オブジェクトの作成
ERSClient client = new ERSClient(
"localhost" , 7001 , "User1" , "password" );
// レポートサーバーへレポート印刷リクエストを発行
IJobInfo job = client.renderReport(
"/samples/demo/member/member.rml", "application/x-print",
null , prop);
}catch (Exception e){
e.printStackTrace();
}
}
}
Java Print Service(JPS)を利用した印刷
- 出力形式にapplication/x-glintを指定
- レポートサーバーで生成したGlint形式のレポートを呼び出し側アプリケーション
(サーブレットやアプレット)で受け取り、レポートサーバーAPIとJava Print Service(JPS)使用して印刷 - 出力先プリンタは呼び出し側アプリケーションが動作するOSに登録されたプリンタ。
プリンタ指定可能。
出力先プリンタ例)
- アプリケーションサーバーが動作するOSに接続
- アプレットが動作するクライアントOSに接続 - Java Print Serviceを使用するため、
javax.print.attribute.HashPrintRequestAttributeSetクラスを使用した印刷属性の指定が可能
※実際の動作は環境に依存します。 - <ELIXIR_ROOT>/server/clients/libディレクトリのglint.jarにクラスパスの設定が必要
■サンプルコード
// JPSを利用した印刷のサンプルコード
import java.util.Properties;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
// レポートサーバーAPIクラスのインポート
import com.elixirtech.ers2.client.ERSClient;
import com.elixirtech.glint.print.GlintStreamPrinter;
import com.elixirtech.glint.print.PrinterUtil;
import com.elixirtech.report2.runtime.IJobInfo;
public class BasicReportSample {
public static void main(String args[]){
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// レポートパラメータ用プロパティオブジェクト
Properties prop = new Properties();
prop.put( "MyParam" , "valuename" );
// レポートサーバー接続管理オブジェクトの作成
ERSClient client = new ERSClient(
"localhost" , 7001 , "User1" , "password" );
// レポートサーバーへGlint形式のレポート生成リクエストを発行
IJobInfo job = client.renderReport(
"/samples/demo/member/member.rml", "application/x-glint",
baos , prop);
GlintStreamPrinter gsp = new GlintStreamPrinter();
HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
// 印刷属性の設定
// aset.add(new MediaPrintableArea(10, 10, 190, 277, MediaPrintableArea.MM));
// プリンタ名一覧の取得(プリンタ名の確認)
// String[] printer = PrinterUtil.getPrinters();
// 取得したプリンタ名をコンソールに出力
// for(int i=0; i<printer.length; i++){
// System.out.println("プリンタ"+ i + ": " + printer[i]);
// }
// プリンタ名の指定
PrintService ps =
PrinterUtil.getSelectedPrintService("<アプリケーション側プリンタ>");
// 印刷(ダイアログ出力なし)
gsp.printNonInteractive(ps,
new ByteArrayInputStream(baos.toByteArray()), aset, null);
}catch (Exception e){
e.printStackTrace();
}
}
}
印刷ターゲットを利用した印刷
- 出力形式にapplication/x-glintを指定
- レポートサーバーで生成したGlint形式のレポートをレポートサーバーに設定した印刷ターゲットに出力
- 出力先プリンタはレポートサーバーが動作するOSに登録されたプリンタ。
印刷ターゲットでプリンタ指定可能。
■サンプルコード
// 印刷ターゲットを利用した印刷のサンプルコード
import java.io.ByteArrayOutputStream;
import java.util.Properties;
import com.elixirtech.ers2.client.ERSClient;
import com.elixirtech.report2.runtime.IJobInfo;
public class PrinterTargetSample {
public static void main(String args[]){
try{
// レポートパラメータ用プロパティオブジェクト
Properties prop = new Properties();
prop.put( "MyParam" , "valuename" );
// 出力ターゲット用プロパティオブジェクト
// printer-nameとしてプリンタ名を設定
Properties targetProp = new Properties();
targetProp.put("printer-name", "<レポートサーバー側のプリンタ>");
// レポートサーバー接続管理オブジェクトの作成
ERSClient client = new ERSClient(
"localhost" , 7001 , "User1" , "password" );
// レポートサーバーへGlint形式のレポート生成リクエストを発行
// 印刷ターゲット名、ターゲット用プロパティを同時に渡す
IJobInfo job = client.renderReport(
"/samples/demo/member/member.rml", "application/x-glint",
prop, "printertarget", targetProp);
}catch (Exception e){
e.printStackTrace();
}
}
}