How to read the content of the PDF file which is uploaded into Google Drive?

Viewed 1079

I want to read the content of the PDF file which is uploaded into google drive. The content should be in a human readable format, not in binary format. Right now I'm using google drive API to read the content but unable to do that.

 $fileId = $request['thesis_google_id'];
    $response = $service->files->get($fileId,   array(
    'alt' => 'media'));
    $content = $response->getBody()->read(2024);
2 Answers

The core functionality of the Google Drive API is to download and upload files in Google Drive. This API does not give you the ability to read anything it just stores the files for you and allows you to download them.

The Google Docs api does give you access to read and write files but i suspect it only supports google docs format files not PDF.

You will need to download the file locally then use some form of PDF reader for PHP to read it.

You can use the Files: export method provided by the Google Drive REST API to get the contents of a PDF file. The export method for .pdf files won’t return images, but all text in the file can be exported into a human readable format. The supported MIME Types that you can export to are listed here.

In PHP, you can achieve this with the following code:

$fileId = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$response = $service->files->export($fileId, 'MIMEType', array(
    'alt' => 'media'));
$content = $response->getBody()->getContents();

Your application will need at least read permissions for Google Docs in order to do this, and should include the query parameter alt=media.

Related