Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API create and download PDF

Is it possible to call the Google Drive API, create a document based on a template with dynamic data sent from an app that is not a Google Apps Scripts app, and download the document as a PDF.

like image 394
user1591178 Avatar asked Sep 05 '25 03:09

user1591178


2 Answers

Based on the Google Drive documentation, The Drive API allows you to download files that are stored in Google Drive. Also, you can download exported versions of Google Documents (Documents, Spreadsheets, Presentations, etc.) in formats that your app can handle. Drive also supports providing users direct access to a file via the URL in the webViewLink property.

Here is an example code that demonstrate on how to download a Google Document in PDF format using the client libraries:

String fileId = "1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().export(fileId, "application/pdf")
.executeMediaAndDownloadTo(outputStream);

Also check this page for more information.

like image 116
KENdi Avatar answered Sep 07 '25 17:09

KENdi


The documentation has instructions on doing this via an HTTP request. However, it is silent on doing it via the Drive V3 Python API.

I found the solution in this video Google Drive API: Uploading & Downloading Files, released by the official Google Developers YouTube channel.

This example assumes that you have a file created and that you have the authorization setup.

# setup authorization
DRIVE = discovery.build('drive', 'v3', credentials=creds)

file_id = 'your-file-id'

data = DRIVE.files().export(fileId=file_id, mimeType="application/pdf").execute()
if data:
    filename = 'your-file-name.pdf'
    with open(filename, 'wb') as pdf_file:
        pdf_file.write(data)

The file will be downloaded in the current working directory.

like image 31
GunnerFan Avatar answered Sep 07 '25 18:09

GunnerFan