How to Generate a PDF from a Google Doc?

This topic was automatically generated from Slack. You can find the original thread here.

I need help generating a pdf from a google doc

To generate a PDF from a Google Doc using Pipedream, you’ll need to use the Google Drive API to export the document as a PDF. Below is a Pipedream component that demonstrates how to do this: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { google: { type: "app", app: "google", }, }, async run({ steps, $ }) { // Replace 'DOCUMENT_ID' with the actual ID of your Google Doc const documentId = 'DOCUMENT_ID'; const response = await axios($, { method: "GET", url: `https://www.googleapis.com/drive/v3/files/${documentId}/export`, params: { // Specify the MIME type to export as PDF mimeType: 'application/pdf', }, headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, }, // Set responseType to 'arraybuffer' to handle the PDF binary responseType: 'arraybuffer', }); // The PDF content will be in the response body as an ArrayBuffer // You can now save this to a file, send it in an email, etc. const pdfContent = response.data; // For example, to save the PDF to a file in the /tmp directory: const fs = require('fs'); const path = require('path'); const filePath = path.join('/tmp', 'exported-document.pdf'); fs.writeFileSync(filePath, Buffer.from(pdfContent)); // Return the path to the saved PDF file return filePath; }, }); Make sure to replace 'DOCUMENT_ID' with the actual ID of your Google Doc. This code will export the document as a PDF and save it to a file in the /tmp directory. You can then use this file as needed, for example, to send it via email or store it elsewhere. Please note that you need to have the appropriate OAuth scopes authorized for your Google account to perform this operation. The scopes https://www.googleapis.com/auth/drive or https://www.googleapis.com/auth/drive.readonly should be sufficient for exporting documents. If you encounter any permission issues, please ensure that your Google app in Pipedream has these scopes enabled.