Can Google Docs be exported to PDF format without using the Documents tab?

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 to do this, but I’m not sure if it’s possible. Here’s what I’ve got so far:

function makePDFFromDoc() {
  const activeDocId = DocumentApp.getActiveDocument().getId();
  const targetFolder = DriveApp.getFolderById('1XYZ-abc123def456ghi789jkl');
  const sourceFile = DriveApp.getFileById(activeDocId);
  const pdfVersion = sourceFile.getAs('application/pdf');
  targetFolder.createFile(pdfVersion);
}

Does anyone know if this will work or if there’s a better way to do it? I’m really hoping to avoid using the Documents tab if possible. Thanks for any help!

I’ve actually been working on a similar problem recently and found a neat workaround. Instead of using scripts, you can leverage Google Apps Script’s built-in UrlFetch service to export docs to PDF. Here’s the gist:

function exportToPdf() {
  var docId = DocumentApp.getActiveDocument().getId();
  var url = 'https://docs.google.com/document/d/' + docId + '/export?format=pdf';
  var token = ScriptApp.getOAuthToken();
  var response = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' + token
    }
  });
  var pdfBlob = response.getBlob().setName(DocumentApp.getActiveDocument().getName() + '.pdf');
  DriveApp.getFolderById('your_folder_id_here').createFile(pdfBlob);
}

This method bypasses the Documents tab completely and is pretty fast. Just remember to replace ‘your_folder_id_here’ with your actual target folder ID. It’s been quite reliable for me across various doc sizes.

Your script looks promising, but there’s a more efficient way to achieve this without relying on the Documents tab. You can use the Drive API directly for PDF conversion. Here’s an adjusted version of your script that should work:

function makePdfFromDoc() {
  const activeDocId = DocumentApp.getActiveDocument().getId();
  const targetFolderId = '1XYZ-abc123def456ghi789jkl';
  const fileName = DriveApp.getFileById(activeDocId).getName() + '.pdf';

  const pdfBlob = DriveApp.getFileById(activeDocId).getAs('application/pdf');
  DriveApp.getFolderById(targetFolderId).createFile(pdfBlob.setName(fileName));
}

This method bypasses the Documents tab entirely and uses Drive API functions to handle the conversion and file creation. It’s more direct and should be faster. Remember to grant necessary permissions for your script to access Drive services.

hey, i’ve got a trick that might help. try using the google drive api directly with this url: https://www.googleapis.com/drive/v3/files/{fileId}/export?mimeType=application/pdf

you’ll need to authenticate, but it’s super quick and bypasses the docs interface completely. worked great for me when i was batch converting stuff!