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?
Any help would be appreciated
CodePudding user response:
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;
/* Class to demonstrate use-case of drive's export pdf. */
public class ExportPdf {
/**
* 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.DRIVE_FILE));
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
credentials);
// Build a new authorized API client service.
Drive service = new Drive.Builder(new NetHttpTransport(),
GsonFactory.getDefaultInstance(),
requestInitializer)
.setApplicationName("Drive samples")
.build();
OutputStream outputStream = new ByteArrayOutputStream();
try {
service.files().export(realFileId, "application/pdf")
.executeMediaAndDownloadTo(outputStream);
return (ByteArrayOutputStream) outputStream;
} catch (GoogleJsonResponseException e) {
// TODO(developer) - handle error appropriately
System.err.println("Unable to export file: " e.getDetails());
throw e;
}
}
}
If you want to send the email with the attachment automatically, you can use as base the Google Developer Guide to create messages with attachments, and once the message is created, you can adapt the sample code for sending messages.
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: **
CodePudding user response:
Maybe try this tool: https://sites.google.com/site/gdocs2direct/
The used function in this site is:
function go(linkId) {
var idExtractor = /\/d\/(. ?)(?:\/|#|\?|$)/;
var result = idExtractor.exec(linkId);
var outputBox = document.getElementById("output");
if (!result) {
outputBox.value = "";
setStatus("Error: Invalid URL", true);
outputBox.disabled = true;
return;
}
var finalLink = "https://drive.google.com/uc?export=download&id=" result[1];
outputBox.disabled = false;
outputBox.value = finalLink;
setStatus("Success! Click the output link to copy it to your clipboard");
}