I’m trying to find a way to change Google Docs files into PDFs using a script. The problem is I don’t want to use the Documents tab. Is there another method?
Here’s the code I’ve come up with so far:
function changeToPDF() {
let activeDoc = DocumentApp.getActive();
let docId = activeDoc.getId();
let targetDir = DriveApp.getFolderById('1ABC123XYZ789');
let docFile = DriveApp.getFileById(docId);
let pdfVersion = docFile.getAs('application/pdf');
targetDir.createFile(pdfVersion);
}
Does anyone know if this will work without the Documents tab? Or is there a better way to do this? Thanks for any help!
This script directly converts the specified Google Doc to PDF and saves it in the desired folder. It efficiently bypasses the Documents tab, and you simply need to replace ‘YOUR_DOC_ID’ and ‘TARGET_FOLDER_ID’ with your actual IDs. Make sure you’ve also enabled the Drive API in your project settings.
yo, i’ve got a trick for u. try using the google drive api instead. it’s way easier n doesn’t need the documents tab at all. here’s a quick example:
function toPDF() {
var file = DriveApp.getFileById('YOUR_DOC_ID');
var pdf = file.getAs('application/pdf');
DriveApp.getFolderById('TARGET_FOLDER_ID').createFile(pdf);
}
just replace the ids and ur good to go. hope this helps!
Hey there, I’ve actually tackled this issue before in one of my projects. Your approach is on the right track, but there’s a more efficient way to do this without relying on the Documents tab.
Instead of using DocumentApp, you can leverage the Drive API directly. Here’s a modified version of your script that should work:
function changeToPDF() {
var fileId = 'YOUR_GOOGLE_DOC_ID_HERE';
var destFolderId = '1ABC123XYZ789';
var blob = DriveApp.getFileById(fileId).getBlob();
var pdfFile = {
title: DriveApp.getFileById(fileId).getName() + '.pdf',
parents: [{ id: destFolderId }]
};
Drive.Files.insert(pdfFile, blob, { convert: true });
}
This method bypasses the need for the Documents tab entirely. It directly converts the Google Doc to PDF using the Drive API. Just make sure you’ve enabled the Drive API in your Google Cloud Console for the script to work. Hope this helps solve your problem!