As of February 2023 'Convert Drive Files' is now available as a Google Workspace Marketplace Add-on.
The following Google Apps Script is designed to create a PDF file of a Google Doc in a Drive folder that you specify, with the option to delete the original Doc. This snippet of code is from larger solutions developed on this blog and allows you to understand and replicate the process.
The Code
Firstly we get our Google Doc (that we want to convert to a PDF) and the Google Drive folder where we want our PDF to be stored:
// get Google Drive folder
var folder = DriveApp.getFolderById('ENTER ID HERE');
// get Google Doc file
var file = DriveApp.getFileById('ENTER ID HERE');
Next we get the content of our Google Doc as a PDF (blob) and create our new file in the Google Drive folder:
// get file content as PDF blob
var pdfBlob = file.getAs('application/pdf');
// create new PDF file in Google Drive folder
folder.createFile(pdfBlob);
Finally, and optionally if you want to comment this part out, we delete the original Google Doc that we no longer need:
// delete original Google Doc file
file.setTrashed(true);
Download
Convert Google Doc to PDF in a given folder download here (please use 'Overview' > 'Make a copy' for your own version).
function docToPDF() { | |
// ID of Google Doc | |
var docID = 'ENTER ID HERE'; | |
// ID of destination folder | |
var folderID = 'ENTER ID HERE'; | |
// get Google Drive folder | |
var folder = DriveApp.getFolderById(folderID); | |
// get Google Doc file | |
var file = DriveApp.getFileById(docID); | |
// get file content as PDF blob | |
var pdfBlob = file.getAs('application/pdf'); | |
// create new PDF file in Google Drive folder | |
folder.createFile(pdfBlob); | |
// delete original Google Doc file | |
file.setTrashed(true); | |
} |
Amazing blog
ReplyDeleteThank you Rashel
Delete