fbpx
印刷

NodeJSを使用してBuildVuマイクロサービスにアクセスする

はじめに

以下のチュートリアルでは、ホストされたBuildVuクラウドAPIを使ってPDFファイルをHTMLやSVGに変換する方法を紹介します:

上記のサービスはすべて、従来のHTTPリクエストでアクセスできますが、このチュートリアルでは、REST APIにシンプルなNodeJSラッパーを提供するオープンソースのNodeJS IDRCloudClientを使用します。

NodeJSを使用してPDFファイルをHTMLまたはSVGに変換する方法

1. npmを使用して、以下のコマンドでidrcloudclientパッケージをインストールします:

				
					npm install --save @idrsolutions/idrcloudclient
				
			

2. idrcloudclientオブジェクトを作成します:

				
					var idrcloudclient = require('@idrsolutions/idrcloudclient');
				
			

3. エンドポイント変数の作成

				
					var endpoint = 'https://cloud.idrsolutions.com/cloud/' + idrcloudclient.BUILDVU;
				
			

4. ファイルをアップロードするためのパラメータマップを作成する

				
					var parameters =  {
    //token: 'Token', //Required only when connecting to the IDRsolutions trial and cloud subscription service
    input: idrcloudclient.UPLOAD,
    file: 'path/to/exampleFile.pdf'
}
				
			

5. [オプション] 進捗、成功、失敗をトリガーするリスナーを作成する。

 
				
					function progressListener(e) {
    console.log(JSON.stringify(e));
}
				
			
				
					function failureListener(e) {
    console.log(e);
    console.log('Failed!');
}
				
			
				
					function successListener(e) {
    console.log(JSON.stringify(e));
    console.log('Download URL: ' + e.downloadUrl);
}
				
			

6. 先に作成した変数を使用してconvertメソッドを呼び出す

				
					idrcloudclient.convert({
    endpoint: endpoint,
    parameters: parameters,

    // The below are the optional listeners, ignore any you haven't defined
    progress: progressListener,
    success: successListener,
    failure: failureListener
});
				
			

コールバックURLへ結果を返す

BuildVu Microserviceは、完了時に変換ステータスを送信するコールバックURLを受け付けます。コールバックURLを使用すると、変換がいつ完了したかを判断するためにサービスをポーリングする必要がなくなります。
convert methodでは、以下のようにコールバックURLを指定することができます。

				
					var parameters =  {
    //token: 'Token', //Required only when connecting to the IDRsolutions trial and cloud subscription service
    input: idrcloudclient.UPLOAD,
    callbackUrl: 'http://listener.url',
    file: 'path/to/exampleFile.pdf'
}
				
			

完全なコードサンプル

ここでは、上記のセクションのステップに基づいてPDFファイルをHTMLまたはSVGに変換する完全なコード例を示します。設定オプションと高度な機能は次のセクションにあります。

				
					var idrcloudclient = require('@idrsolutions/idrcloudclient');

function progressListener(e) {
    console.log(JSON.stringify(e));
}

function failureListener(e) {
    console.log(e);
    console.log('Failed!');
}

function successListener(e) {
    console.log(JSON.stringify(e));
    console.log('Download URL: ' + e.downloadUrl);
}

var endpoint = 'https://cloud.idrsolutions.com/cloud/' + idrcloudclient.BUILDVU;
var parameters =  {
    //token: 'Token', //Required only when connecting to the IDRsolutions trial and cloud subscription service
    input: idrcloudclient.UPLOAD,
    file: 'path/to/exampleFile.pdf'
 }

idrcloudclient.convert({
    endpoint: endpoint,
    parameters: parameters,
    
    // The below are the available listeners
    progress: progressListener,
    success: successListener,
    failure: failureListener
});
				
			

設定オプション

BuildVu APIは、変換をカスタマイズするためのキーバリューペア設定オプションを含む文字列化されたJSONオブジェクトを受け付けます。この設定はconvertメソッドに与える必要があります。PDFファイルをHTMLまたはSVGに変換するための設定オプションの完全なリストはこちらをご覧ください。

				
					settings: '{"key":"value","key":"value"}'
				
			

URLによるアップロード

ローカルファイルをアップロードするだけでなく、BuildVu Microserviceがダウンロードして変換を実行するURLを指定することもできます。これを行うには、convertメソッドのinputとfileの値を以下のように置き換えます。

				
					input: IDRCloudClient.DOWNLOAD
url: 'http://exampleURL/exampleFile.pdf'

				
			

認証を使用する

PDFファイルをHTMLまたはSVGに変換するためにユーザー名とパスワードを必要とする独自のBuildVu Microserviceをデプロイした場合、変換のたびにユーザー名とパスワードを提供する必要があります。これらは、以下のようにconvertメソッドにauthという変数を渡すことで提供されます。

				
					username: 'username',
password: 'password',
				
			
    MENU
    PAGE TOP