Google Apis2
Google Apis2
Google Apis2
About
Products
For Teams
Search…
Home
PUBLIC
Questions
Tags
Users
Companies
COLLECTIVES
Explore Collectives
TEAMS
Stack Overflow for Teams – Start collaborating and sharing organizational
knowledge.
Google API to download a file from Google Docs as pdf
Asked 1 year ago
Modified 1 year ago
Viewed 325 times
Report this ad
0
I have a file with some text and tables in my Google Docs. The content of this file
is manually updated time to time.
I would need to send out this file as pdf many times a month. It is being done
manually with the Web UI (File - Download - pdf) and then sent via Email to my
users.
I would like to eliminate this human intervention and create a API which downloads
this pdf and provide the endpoint to my users.
Can this use case be done with Google API (preferably JavaScript)? If so, how?
javascriptnode.jsgoogle-apigoogle-docs
Share
Improve this question
Follow
asked Jun 27, 2022 at 17:04
Parthiva's user avatar
Parthiva
25433 silver badges2020 bronze badges
Add a comment
1 Answer
Sorted by:
To download the Google Doc to PDF, you can take as base the sample code in Google
Developers, which use the files.export method to download the file as a PDF. Here
is the sample:
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
/**
* Download a Document file in PDF format.
* @param realFileId file ID of any workspace document format file.
* @return byte array stream if successful, {@code null} otherwise.
* @throws IOException if service account credentials file not found.
*/
private static ByteArrayOutputStream exportPdf(String realFileId) throws
IOException{
// Load pre-authorized user credentials from the environment.
// TODO(developer) - See https://developers.google.com/identity for
// guides on implementing OAuth2 for your application.
GoogleCredentials credentials =
GoogleCredentials.getApplicationDefault().createScoped(Arrays.asList(DriveScopes.DR
IVE_FILE));
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
credentials);
All the guides have samples in Java. If you click on the option "View on GitHub,"
you will be redirected to a GitHub with the online repository of the code.
** Reference: **
Files: export.
Method: users.drafts.send.
Method: users.messages.send.
Share
Improve this answer
Follow
edited Jun 27, 2022 at 19:15
answered Jun 27, 2022 at 19:09
Giselle Valladares's user avatar
Giselle Valladares
1,90511 gold badge44 silver badges1313 bronze badges
Add a comment
Your Answer
Sign up or log in
Post as a guest
Name
Email
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge
that you have read and understand our privacy policy and code of conduct.
Not the answer you're looking for? Browse other questions tagged
javascriptnode.jsgoogle-apigoogle-docs or ask your own question.
The Overflow Blog
Why on earth are people paying for digital real estate? (Ep. 587)
The Overflow #185: The hardest part of software is requirements
Featured on Meta
Starting the Prompt Design Site: A New Home in our Stack Exchange Neighborhood
Colors update: A more detailed look
Temporary policy: Generative AI (e.g. ChatGPT) is banned
Testing native, sponsored banner ads on Stack Overflow (starting July 6)
Report this ad
4 people chatting
JavaScript
6 hours ago - Parking Master
JBis: 10 hours ago
JamesBot: yesterday
ThiefMaster: May 11 at 13:28
Tamil iOS and Web Developers
yesterday - Ramprasath Selvam
Related
7
Google Docs API for Node.js
1
How to send a downloadable pdf to javascript client via node.js?
1
How to download a pdf file through javascript?
1
Node.js - Download File from Google Drive via request
2
Programmatically download a Google Doc?
1
email Google Doc as PDF attachment
3
Google Drive API v3 - Convert doc to pdf
2
How can I fetch a Google Doc as a PDF file using the Drive API and send that blob
to my API consumer?
3
Google Drive Web API save and download pdf
7
Getting pdf file from api response
Hot Network Questions
Would it be possible for a civilization to create machines before wheels?
How can I remove a mystery pipe in basement wall and floor?
Extract data which is inside square brackets and seperated by comma
Relativistic time dilation and the biological process of aging
Landmine Number II
Were Patton's and/or other generals' vehicles prominently flagged with stars (and
if so, why)?
Is speaking the country's language fluently regarded favorably when applying for a
Schengen visa?
when to use the present participle (-nd) in German vs gerund (-ing) in English
What is the best understanding of Isaiah 53:10 in Hebrew
Book or a story about a group of people who had become immortal, and traced it back
to a wagon train they had all been on
Author contributions: Listed as "writing original draft" despite not writing
anything
Is religious confession legally privileged?
How to seal the top of a wood-burning cooking stove?
How to put left brace in nicematrix?
Where does the normal terminal output go to when a script is started by init.d?
Can I still have hopes for an offer as a software developer
Miniseries involving virtual reality, warring secret societies
In what circumstances should I use the Geometry to Instance node?
Keep a fixed distance between two bevelled surfaces
Is it legal to intentionally wait before filing a copyright lawsuit to maximize
profits?
Why did the Apple III have more heating problems than the Altair?
Are there nice walking/hiking trails around Shibu Onsen in November?
Was the Garden of Eden created on the third or sixth day of Creation?
more hot questions
Question feed
STACK OVERFLOW
Questions
Help
PRODUCTS
Teams
Advertising
Collectives
Talent
COMPANY
About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us
Cookie Settings
Cookie Policy
STACK EXCHANGE NETWORK
Technology
Culture & recreation
Life & arts
Science
Professional
Business
API
Data
Blog
Facebook
Twitter
LinkedIn
Instagram
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC
BY-SA. rev 2023.7.7.43526
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on
your device and disclose information in accordance with our Cookie Policy.