12.14 PDFを生成しAmazon S3に格納後、ダイレクト印刷するサンプルプログラム(WCF)
概要
PDFを生成しAmazon S3に格納後、ダイレクト印刷するサンプルプログラムです。
サンプル構成
| 項目 | ファイルパス |
|---|---|
| ソースサンプル |
<bswss-client_home>/sample/dotNET/wcf/WSS_WCF_Sample14.aspx.cs
|
ソースサンプル
using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web;
using BizStreamWebApplication.BizStreamServiceReference;
using com.brainsellers.bizstream.directprint;
namespace BizStreamWebApplication
{
public partial class WSS_WCF_Sample14 : System.Web.UI.Page
{
// Webサービスサーバのホスト名
const string WSS_HOST_NAME = "wsssrv";
// Webサービスサーバ上のサンプルディレクトリの場所
const string WSS_BIZSTREAM_SAMPLE_DIR = "\\bs\\sample";
// Webサービスのエンドポイントプリフィックス
const string WSS_SERVICE_LOCATION = "http://" + WSS_HOST_NAME + ":8080/axis2/services";
// BASIC認証のユーザ名
const string USER_NAME = @"bizuser1";
// BASIC認証のパスワード
const string PASSWORD = @"bizuser1";
// 監査ロギング用のユーザ定義
const string USER_DEF = "AAA";
// 入力レイアウト定義ファイル名
const string LAYOUT_FILE = WSS_BIZSTREAM_SAMPLE_DIR + "\\xml\\Sample1.xml";
// 格納するファイルのバケット内のパス (先頭に/は不要)
const string TARGET_FILE_PATH = "Sample/WSS_WCF_Sample14.pdf";
// ユーザアプリケーションサーバのホスト名:ポート名
const string AP_HOST_NAME = "localhost:50000";
// プリンタ名
const string PRINTER_NAME = "FinePrint";
// 印刷結果応答URL
const string RESPONSE_URL = "http://" + AP_HOST_NAME + "/WSS_WCF_Sample9_1.aspx";
protected void Page_Load(object sender, EventArgs e)
{
// Amazon S3にPDFファイルを格納
generateOutputDataResponse response = generate();
// レスポンスより格納したファイルの情報を取得
amazonS3 s3 = response.amazonS3;
String region = s3.region; // リージョン
String bucketName = s3.bucketName; // バケット名
String filePath = s3.filePath; // ファイルパス
// ファイルデータの取得
byte[] data = getContentData(response);
// 取得したファイルをダイレクト印刷のストリームに出力
PDFDirectPrintStream direct = makePDFDirectPrintStream(Response);
direct.Write(data);
direct.Close();
}
private generateOutputDataResponse generate()
{
// クライアントを生成
bizstreamPortTypeClient client =
new bizstreamPortTypeClient("BizstreamSOAP11port",
WSS_SERVICE_LOCATION + "/bizstream?UserDef=" + USER_DEF);
// 認証の設定
BindingElementCollection elements = client.Endpoint.Binding.CreateBindingElements();
elements.Find<HttpTransportBindingElement>().AuthenticationScheme =
System.Net.AuthenticationSchemes.Basic;
client.ClientCredentials.UserName.UserName = USER_NAME;
client.ClientCredentials.UserName.Password = PASSWORD;
// MTOMを有効化
elements.Remove<TextMessageEncodingBindingElement>();
MtomMessageEncodingBindingElement mtom = new MtomMessageEncodingBindingElement();
mtom.ReaderQuotas.MaxStringContentLength = 100000;
elements.Insert(0, mtom);
elements.Find<HttpTransportBindingElement>().KeepAliveEnabled = false;
client.Endpoint.Binding = new CustomBinding(elements);
// リクエストメッセージの作成
generateOutputDataRequest request = new generateOutputDataRequest();
// Amazon S3への格納設定
// ・下記のようなAPI指定の他にcloud_storage.propertiesファイルでの指定が可能
// ・特にアクセスキーとシークレットキーはセキュリティ上cloud_storage.propertiesファイルにて設定すること
// ・APIとpropertiesファイルと両方指定している場合はAPI指定が優先される
// ・ファイルパスはAPIでの指定が必須
amazonS3 s3 = new amazonS3();
// s3.accessKey = "xxx"; // アクセスキー (API指定は非推奨)
// s3.secretKey = "xxx"; // シークレットキー (API指定は非推奨)
// s3.serviceEndpoint = "s3.amazonaws.com"; // サービスエンドポイント
// s3.region = "ap-northeast-1"; // リージョン
// s3.bucketName = "BucketName"; // バケット名 (バケットは事前に作成しておくこと)
// s3.proxyHost = "proxy"; // プロキシホスト名
// s3.proxyPort = 8080; // プロキシポート番号
s3.filePath = TARGET_FILE_PATH; // バケット内のファイルパス (必須)
s3.tags = new tag[10];
for (int i = 1; i <= 10; i++)
{
// タグは10個まで設定可能
s3.tags[i - 1] = new tag();
s3.tags[i - 1].key = "key" + i; // タグのキー
s3.tags[i - 1].value = "value" + i; // タグの値
}
// 出力ファイルの種類を設定
pdf pdfData = new pdf();
pdfData.Item = s3;
output outputData = new output();
outputData.Item = pdfData;
request.output = outputData;
// レイアウト定義を設定
request.layoutData = new layoutDefinition[1];
request.layoutData[0] = new layoutDefinition();
request.layoutData[0].uri = LAYOUT_FILE;
// リクエストを送信
return client.generateOutputData(request);
}
private byte[] getContentData(generateOutputDataResponse response)
{
// コンテンツデータの配列を取得
base64Binary[] list = response.contentDataGroup.contentData;
// PDF, Excelの場合は要素が1つのみ
base64Binary base64 = list[0];
// ファイルデータを取得
return base64.Value;
}
private PDFDirectPrintStream makePDFDirectPrintStream(HttpResponse response)
{
//インスタンス生成
PDFDirectPrintStream direct = new PDFDirectPrintStream(response);
// 印刷応答URL
direct.setResponseUrl(RESPONSE_URL);
// 出力PDFファイル
// direct.setSaveFileName("C:/Temp/WSS_WCF_Sample14.pdf");
// プリンタ名
direct.setPrinterName(PRINTER_NAME);
// 印刷部数
direct.setNumberOfCopy(1);
// 出力トレイ
direct.setSelectedTray(PDFDirectPrintStream.TRAY_AUTO);
// 印刷ジョブ名
direct.setJobName("WSS_WCF_Sample14");
// 印刷ダイアログ表示
direct.setPrintDialog(false);
// ブラウザのターゲットフレーム名(IEでのみ有効)
direct.setTarget("bizPrint");
// sppファイル暗号化パスワード
// direct.setPassword("password");
// エンコード済み暗号化パスワード
// direct.setPasswordWithEncoded("cGFzc3dvcmQ=");
// 用紙に合わせて印刷
direct.setDoFit(true);
// 印刷開始ページ
direct.setFromPage(1);
// 印刷終了ページ
direct.setToPage(-1);
return direct;
}
}
}
