Created
January 30, 2025 19:09
App Script to generate a PDF version from a google doc, preserving the PDF file link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createPDF() { | |
// Get the current Google Doc | |
const doc = DocumentApp.getActiveDocument(); | |
// Get the document ID and name | |
const docId = doc.getId(); | |
const docName = doc.getName(); | |
// Get the folder where the document is located | |
const file = DriveApp.getFileById(docId); | |
const parents = file.getParents(); | |
// If the document is in at least one folder | |
if (parents.hasNext()) { | |
const folder = parents.next(); | |
// Create PDF blob | |
const pdfBlob = DriveApp.getFileById(docId).getAs('application/pdf'); | |
// Determine the PDF filename | |
const pdfName = docName.toLowerCase().endsWith('.pdf') | |
? docName | |
: `${docName}.pdf`; | |
// Search for existing PDF with the same name in the folder | |
const files = folder.getFilesByName(pdfName); | |
if (files.hasNext()) { | |
// If PDF exists, update its content using Advanced Drive API | |
const existingPdf = files.next(); | |
const existingId = existingPdf.getId(); | |
// Store the sharing settings | |
const sharingAccess = existingPdf.getSharingAccess(); | |
const sharingPermission = existingPdf.getSharingPermission(); | |
const isPublic = sharingAccess === DriveApp.Access.ANYONE || | |
sharingAccess === DriveApp.Access.ANYONE_WITH_LINK; | |
const resource = { | |
title: pdfName, | |
mimeType: 'application/pdf' | |
}; | |
// Update the file content | |
Drive.Files.update(resource, existingId, pdfBlob); | |
// Re-apply sharing settings | |
const updatedFile = DriveApp.getFileById(existingId); | |
if (isPublic) { | |
updatedFile.setSharing(sharingAccess, sharingPermission); | |
} | |
DocumentApp.getUi().alert('Existing PDF was updated successfully with preserved sharing settings!'); | |
} else { | |
// If no existing PDF, create a new one | |
const pdfFile = folder.createFile(pdfBlob); | |
pdfFile.setName(pdfName); | |
// Set default sharing to "Anyone with link can view" | |
pdfFile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW); | |
DocumentApp.getUi().alert('New PDF was created successfully with "Anyone with link can view" permissions!'); | |
} | |
} else { | |
// If the document is not in any folder (i.e., in root of Drive) | |
DocumentApp.getUi().alert('Document must be in a folder to create PDF'); | |
} | |
} | |
// Create a menu item when the document opens | |
function onOpen() { | |
const ui = DocumentApp.getUi(); | |
ui.createMenu('PDF Tools') | |
.addItem('Create PDF', 'createPDF') | |
.addToUi(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you have to enable 'advanced drive api' to make this work. See
https://developers.google.com/apps-script/advanced/drive
Specifically follow the link here that says:
This is an advanced service that must be enabled before use.