Can I transform Google Docs into PDF format without using the Documents tab?

Hey everyone, I’m trying to figure out if there’s a way to change Google Docs files into PDFs without having to use the Documents tab. I’ve been working on a script, but I’m not sure if it’s possible to do this automatically. Here’s what I’ve got so far:

function makePDFFromDoc() {
  const documentId = DocumentApp.getActiveDocument().getId();
  const targetFolder = DriveApp.getFolderById('abc123xyz789');
  const sourceFile = DriveApp.getFileById(documentId);
  const pdfVersion = sourceFile.getAs('application/pdf');
  targetFolder.createFile(pdfVersion);
}

Has anyone done something like this before? I’m wondering if there’s a way to bypass the Documents tab entirely. Any tips or tricks would be super helpful. Thanks!

I’ve actually implemented something similar in my work recently. Your approach is on the right track, but you can indeed bypass the Documents tab entirely. Here’s a refined version that should work:

function convertToPDF() {
  const fileId = 'YOUR_GOOGLE_DOC_ID';
  const destFolderId = 'YOUR_DESTINATION_FOLDER_ID';
  
  const file = DriveApp.getFileById(fileId);
  const blob = file.getAs(MimeType.PDF);
  const pdfFile = DriveApp.getFolderById(destFolderId).createFile(blob);
  
  pdfFile.setName(file.getName() + '.pdf');
}

This script directly accesses the file via the Drive API, converts it to PDF, and saves it in your specified folder. There’s no need to open the document or use the Documents tab. Just ensure you replace the placeholder IDs with your actual document and folder IDs. Hope this helps!

yep, u can totally skip the Documents tab! try using the Drive API directly like this:

function makePDF() {
  var doc = DriveApp.getFileById('ur_doc_id');
  var pdf = doc.getAs('application/pdf');
  DriveApp.getFolderById('ur_folder_id').createFile(pdf);
}

just swap in ur actual IDs and it should work. no need to mess with DocumentApp at all!

I’ve been doing this for a while now, and I can confirm it’s definitely possible to convert Google Docs to PDF without using the Documents tab. Here’s what I’ve found works best:

Use the DriveApp class to interact with the file directly. You can get the file by ID, then use the getAs() method to convert it to PDF format. Here’s a quick example:

function convertToPDF(fileId, folderId) {
var file = DriveApp.getFileById(fileId);
var pdfBlob = file.getAs(MimeType.PDF);
var folder = DriveApp.getFolderById(folderId);
folder.createFile(pdfBlob);
}

This approach is more efficient and doesn’t require opening the document. Just make sure you have the correct file and folder IDs. Also, remember to set up the necessary permissions in your script to access Drive files.

Hope this helps with your automation efforts!