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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With