3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Google Apps Script で PDF をテキスト化する

Posted at

はじめに

GASでPDFの内容を読み取りたい!と思った時に書いたコードの記録

コード

export class PDFService {

	toStringByFileId(fileId: string): string {
		const blob: GoogleAppsScript.Base.Blob = DriveApp.getFileById(fileId).getBlob();
		const resource: any = {
			title: blob.getName(),
			mimeType: blob.getContentType(),
		};
		const doc: GoogleAppsScript.Document.Document = this.toDoc(resource, blob, 'ja');
		return doc.getBody().getText();
	}

	private toDoc(resource: any, blob: GoogleAppsScript.Base.Blob, language: string): GoogleAppsScript.Document.Document {
		const options: any = {ocr: true, ocrLanguage: language};
		try {
			const file: GoogleAppsScript.Drive.Schema.File = Drive.Files.insert(resource, blob, options);
			return DocumentApp.openById(file.id);
		} catch(e: any) {
			console.error(e);
		}
	}
}
3
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?