fbpx
< 戻る
印刷

NodeJSを使ってPDFからテキストを抽出する

目次

はじめに

以下のチュートリアルでは、ホスティングされたJPedalクラウドAPIを使ってPDFからテキストを抽出する方法を紹介します。セルフホスト型のJPedalマイクロサービスをセットアップすることもできます。

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

NodeJSを使用してPDFからテキストを抽出するサンプルコード

  1. npmを使用して、次のコマンドでidrcloudclientパッケージをインストールします:
				
					npm install --save @idrsolutions/idrcloudclient  
				
			
  1. idrcloudclient オブジェクトを作成します
				
					var idrcloudclient = require('@idrsolutions/idrcloudclient');
				
			
  1. エンドポイント変数の作成
				
					var endpoint = 'https://my-self-hosted-service.com/' + idrcloudclient.JPEDAL;
				
			
  1. ファイルをアップロードするためのパラメータマップの作成
				
					var parameters =  {
    input: idrcloudclient.UPLOAD,
    file: 'path/to/exampleFile.pdf',
    settings: '{"mode":"extractText","type":"plainText"}'
}
				
			
  1. [オプション] 進捗、成功、失敗をトリガーするリスナーを作成します。
				
					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);
}
				
			
  1. 先に作成した変数を使用して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に返信

JPedal マイクロサービスは、抽出の完了時にステータスを送信するコールバック URL を受け付けます。コールバックURLを使用すると、抽出がいつ完了したかを判断するためにサービスをポーリングする必要がなくなります。
コールバックURLは以下のように指定します。

				
					var parameters =  {
    input: idrcloudclient.UPLOAD,
    callbackUrl: 'http://listener.url',
    file: 'path/to/exampleFile.pdf',
    settings: '{"mode":"extractText","type":"plainText"}'
}
				
			

完全なコード例

以下は、上記のセクションのステップに基づいてPDFからテキストを抽出する完全なコードサンプルです。設定オプションと高度な機能は次のセクションにあります。

				
					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://my-self-hosted-service.com/' + idrcloudclient.JPEDAL;
var parameters =  { 
    input: idrcloudclient.UPLOAD,
    file: 'path/to/exampleFile.pdf',
    settings: '{"mode":"extractText","type":"plainText"}'
 }

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

設定オプション

JPedal APIは、抽出をカスタマイズするためのキーと値のペアの設定オプションを含む文字列化されたJSONオブジェクトを受け付けます。設定はパラメータ配列に追加する必要があります。PDFからテキストを抽出するための設定オプションの完全なリストはここにあります。

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

URLによるアップロード

ローカルファイルをアップロードするだけでなく、JPedalマイクロサービスがダウンロードして抽出を行うURLを指定することもできます。そのためには、inputとfileの値を以下のように置き換えてください。

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

認証の使用

PDFからテキストを抽出するためにユーザ名とパスワードを必要とする独自のJPedalマイクロサービスをデプロイする場合、変換のたびにユーザ名とパスワードを提供する必要があります。これらは、URLの前にユーザ名とパスワードを持つユーザフラグを追加することで提供されます。

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