- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2021 11:20 PM
こんにちは。
現在、API連携で取得したPDFファイルのバイナリデータをPDFファイルとして、
特定のレコードに登録をしようと思っております。
ファイル自体は登録をできたのですが、
ファイルを開くと真っ白になってしまっております。
また、取得元のファイルはSJISですが、登録後のファイルはUTF-8になってしまっております。
このような事象の解決方法をご教示頂きたいです。
/*
*API連携でファイルデータ取得
*/
var content = <取得したバイナリデータ>;
var fileName = 'result.pdf';
var contentType = 'application/pdf';
var sys_id = "<格納したいレコードのsys_id>";
var attachment = new GlideSysAttachment();
var rec = new GlideRecord('<テーブル名>');
rec.get(sys_id);
var agr = attachment.write(rec, fileName, contentType, content);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 03:32 AM
何らかのRestAPI のGETでPDFが取得できるとしたときの、詳細仕様を確認したいところではありますが、一例として下記のページを紹介します。参考例は画像ですがPDFでも動作するはず。(もしうまく行かない場合はRestAPIの詳細仕様を教えてください)
- SAVING RESTMESSAGEV2 RESPONSES AS ATTACHMENTS
RESTMessageV2の応答を添付ファイルとして保存する
https://developer.servicenow.com/blog.do?p=/post/saving-restmessagev2-responses-as-attachments/
// This is where we'll save the attachment
// これが添付ファイルを保存する場所です
var tablename = 'incident';
var recordSysId = '8d6353eac0a8016400d8a125ca14fc1f';
var filename = 'snlogo.png';
// Let's download the ServiceNow Logo
// ServiceNow Logoをダウンロードしましょう
var logoUrl = 'https://instance.service-now.com/images/logos/logo_service-now.png';
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
request.setEndpoint(logoUrl);
// Configure the request to save the response as an attachment
// 添付ファイルとしての応答を保存する要求を設定する
request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);
// When we execute the request, the attachment will automatically be
// saved to the record we specified
//リクエストを実行すると、添付ファイルは自動的に指定されたレコードに保存されます。
var response = request.execute();
var httpResponseStatus = response.getStatusCode();
var httpResponseContentType = response.getHeader('Content-Type');
gs.debug("http response status_code: " + httpResponseStatus);
gs.debug("http response content-type: " + httpResponseContentType);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 01:28 AM
こんにちは。
取得元のファイルについて確認させてください。
取得元のファイルはSJISであると言うことは、それはテキストファイルで、ファイル名を仮に「test.txt」としてSJISファイルがあるとしたら、そのテキストファイルを何かのツールを使ってPDFに変換した。PDFファイル名を仮に「test.pdf」とします。
この時点で、存在するファイルは、
- テキストファイル「test.txt」(仮)、文字コード SJIS
- PDFファイル「test.pdf」(仮)、バイナリデータ
API連携でファイルデータを取得したのは、PDFファイルである。
ここで、わからないことがあります。
PDFファイルなのに、文字コードのSJISとかUTF8 と判断できたのでしょうか。
PDFファイルの文字コードが変化しているとは、PDFファイルの文字コードSJISであったとか、UTF8になっているとか、何を確認してその情報を判断しているのでしょうか。
PDFファイルがバイナリであるならば、バイナリのままAttachmentテーブルに記録することで表示できるはずです。
ほか考えられるのは、API連携時にバイナリーを Base64 エンコードのような文字列化をしていて、デコード時に文字列扱いになってしまっている。
違うとは思いますがテキストファイル「test.txt」をRestAPIで取得して、AttachmentテーブルにPDFとして登録している。この場合はファイルの形式は自動変換されないので当然PDFにはなりません。
確認したいこと、
- API連携で取得しているファイルは、事前にテキストファイルSJISをPDFに変換したバイナリーファイルであるか。
- PDFファイルの文字コードとは何を見て判断しているのか。
- 追加)RestAPIの方式やバイナリを取得しているスクリプトの処理概要も、可能なら教えてほしいです。
よろしくお願いいたします。
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 02:23 AM
iwaiさん、返信ありがとうございます。
確認事項に回答させていただきます。
- 想定されているものではないです。
- テキストエディタで開いた際の文字コードを見て判断しております。
- RESTMessageV2を使用しまして、GETリクエストを行っており、処理としましては正常終了しております。また、今回使用したAPIのリファレンスに戻り値の形式として、string($binary)と記載がありました。
以上、よろしくお願い致します。
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 03:32 AM
何らかのRestAPI のGETでPDFが取得できるとしたときの、詳細仕様を確認したいところではありますが、一例として下記のページを紹介します。参考例は画像ですがPDFでも動作するはず。(もしうまく行かない場合はRestAPIの詳細仕様を教えてください)
- SAVING RESTMESSAGEV2 RESPONSES AS ATTACHMENTS
RESTMessageV2の応答を添付ファイルとして保存する
https://developer.servicenow.com/blog.do?p=/post/saving-restmessagev2-responses-as-attachments/
// This is where we'll save the attachment
// これが添付ファイルを保存する場所です
var tablename = 'incident';
var recordSysId = '8d6353eac0a8016400d8a125ca14fc1f';
var filename = 'snlogo.png';
// Let's download the ServiceNow Logo
// ServiceNow Logoをダウンロードしましょう
var logoUrl = 'https://instance.service-now.com/images/logos/logo_service-now.png';
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
request.setEndpoint(logoUrl);
// Configure the request to save the response as an attachment
// 添付ファイルとしての応答を保存する要求を設定する
request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);
// When we execute the request, the attachment will automatically be
// saved to the record we specified
//リクエストを実行すると、添付ファイルは自動的に指定されたレコードに保存されます。
var response = request.execute();
var httpResponseStatus = response.getStatusCode();
var httpResponseContentType = response.getHeader('Content-Type');
gs.debug("http response status_code: " + httpResponseStatus);
gs.debug("http response content-type: " + httpResponseContentType);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 09:08 PM
iwaiさん、返信ありがとうございます。
ご共有頂いたものを試してみたところ、ファイルが真っ白になることなく登録できました。
ありがとうございました。