I’m trying to find a way to save Google Docs as PDF files without using the Documents tab. Is this possible? I’ve been working on a script to do this, but I’m not sure if it’s the right approach. Here’s what I’ve got so far:
hey lucask, ur script looks pretty good! i’ve used something similar before. one thing to consider is error handling - maybe add a try/catch block? also, don’t forget to set the pdf filename. somthing like:
Your script approach is on the right track, but there’s a more straightforward method to export Google Docs as PDFs without using the Documents tab. You can utilize the Drive API directly, which is more efficient for bulk operations. Here’s an improved version of your script:
This method is more direct and handles the PDF conversion more efficiently. It also preserves the original document name for the PDF file. Remember to replace ‘abc123xyz456’ with your actual target folder ID. This approach should work well for your needs and is scalable for multiple documents if needed.
I’ve been in your shoes, lucask, and I can tell you there’s definitely a way to export Google Docs as PDFs without using the Documents tab. Your script is a good start, but let me share what worked for me.
I found that using the advanced Drive service can make this process much smoother. Here’s a snippet that I’ve used successfully:
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').createFile(pdfBlob);
}
This approach has been more reliable for me, especially when dealing with larger documents or batch processing. Just remember to enable the Advanced Drive Service in your script project settings. Good luck with your project!