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

I’m trying to find a way to change Google Docs files into PDFs using a script, but without having to open the Documents tab. Is this even possible?

Here’s the code I’ve come up with so far:

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

Does anyone know if there’s a way to make this work without using the Documents tab? I’d really appreciate any help or advice on this!

hey sky24, yea u can do it w/o the docs tab. try using DriveApp.getFileById() to fetch your file, then .getAs(‘application/pdf’) to convert it.

hope that helps!

I’ve actually tackled this issue before in a project. You’re on the right track with your code, but here’s a tweak that should work:

function convertToPDF() {
  const fileId = 'YOUR_FILE_ID_HERE';
  const file = DriveApp.getFileById(fileId);
  const pdfBlob = file.getAs(MimeType.PDF);
  const pdfFile = DriveApp.createFile(pdfBlob);
  
  // Optional: Move to a specific folder
  const targetFolderId = 'YOUR_FOLDER_ID_HERE';
  DriveApp.getFolderById(targetFolderId).addFile(pdfFile);
  DriveApp.getRootFolder().removeFile(pdfFile);
}

This approach bypasses the Documents service entirely, working directly with Drive. Just replace the placeholder IDs with your actual file and folder IDs. It’s been reliable for me in automating PDF conversions across multiple docs.

I’ve used a similar approach in my work, and it’s quite effective. One thing to keep in mind is that while this method works well for basic documents, it might not perfectly preserve complex formatting or embedded elements. If you’re dealing with a large number of files, you might want to add some error handling and logging to your script. Something like:

function convertToPDF() {
  try {
    // Your conversion code here
    Logger.log('Conversion successful for file: ' + fileId);
  } catch (error) {
    Logger.log('Error converting file ' + fileId + ': ' + error.toString());
  }
}

This way, you can track which files converted successfully and which ones encountered issues. It’s been a lifesaver for me when batch processing documents.