Ask AI

Saving a PDF from Text to Google Drive with Preserved Formatting

Google Drive API does not support direct creation of a formatted PDF file from plain text.

However, you can achieve the desired result through a simple workaround: first create a Google Docs file from the text, then export it as a PDF, preserving formatting such as line breaks, lists, and headings.


๐Ÿ”ง Step-by-step guide

Step 1 โ€” Create a Google Docs file from text

To begin, use one of the following nodes to create a document in Google Docs format

  • Google Drive โ†’ Create new file from text
  • Google Docs โ†’ Create new document from text
    • Notion image
โœ… On successful creation, these nodes will return the fileId of the new document โ€” this is what youโ€™ll use in the next step to export it as a PDF.

Step 2 โ€” Export the document as PDF and save it to Drive

Paste the code below into a JavaScript node to export the document and save it as a PDF. This step also deletes the original .gdoc file after export.

/** @CustomParams
{
  "accessToken": {
    "title": "Google Drive Access Token",
    "key": "accessToken",
    "type": "string"
  },
  "fileId": {
    "title": "Google Docs File ID",
    "key": "fileId",
    "type": "string"
  },
  "pdfFileName": {
    "title": "PDF File Name",
    "key": "pdfFileName",
    "type": "string"
  }
}
*/
import { google } from 'googleapis';
import stream from 'stream';

export default async function run({ data }) {
  const auth = new google.auth.OAuth2();
  auth.setCredentials({ access_token: data.accessToken });

  const drive = google.drive({ version: 'v3', auth });

  try {
    const exportResponse = await drive.files.export(
      {
        fileId: data.fileId,
        mimeType: 'application/pdf'
      },
      {
        responseType: 'stream'
      }
    );

    const bufferStream = new stream.PassThrough();
    exportResponse.data.pipe(bufferStream);

    const uploadResponse = await drive.files.create({
      resource: {
        name: data.pdfFileName || 'exported.pdf',
        mimeType: 'application/pdf'
      },
      media: {
        mimeType: 'application/pdf',
        body: bufferStream
      },
      fields: 'id, name, webViewLink'
    });

    const pdfFile = uploadResponse.data;

    // โ— Optional: Delete the original Google Docs file
    await drive.files.delete({ fileId: data.fileId });

    return {
      message: 'โœ… PDF saved to Google Drive and source document deleted',
      pdfFileId: pdfFile.id,
      fileName: pdfFile.name,
      viewLink: pdfFile.webViewLink
    };

  } catch (err) {
    return {
      error: 'โŒ Failed during export/upload/delete',
      details: err.message
    };
  }
}
โœ… Paste this into a JavaScript node, click โ€œGenerate Paramsโ€, and fill in:

๐Ÿ” Workflow summary

  1. Create a Google Docs file using a built-in Drive/Docs node
  1. Export it as PDF using a JavaScript node
  1. Upload the PDF to Drive
  1. Delete the temporary .gdoc file after export

Notion image
Did this answer your question?
๐Ÿ˜ž
๐Ÿ˜
๐Ÿคฉ