Skip to content

Instantly share code, notes, and snippets.

@pkorpine
Last active November 17, 2023 20:05
Show Gist options
  • Select an option

  • Save pkorpine/c7efed52086147916c951ce2211465e5 to your computer and use it in GitHub Desktop.

Select an option

Save pkorpine/c7efed52086147916c951ce2211465e5 to your computer and use it in GitHub Desktop.

Revisions

  1. pkorpine revised this gist May 31, 2021. No changes.
  2. pkorpine revised this gist May 31, 2021. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion save_as_pdf_gs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    // Saves the current document in Google Docs as PDF to the same folder.
    // Code from https://stackoverflow.com/questions/28312992/convert-google-doc-to-pdf-using-google-script-editior
    // Just added onOpen() to add the script to the menubar.

    function onOpen() {
    var ui = DocumentApp.getUi();
    // Or DocumentApp or FormApp.
    @@ -6,7 +10,7 @@ function onOpen() {
    .addToUi();
    }

    // https://stackoverflow.com/questions/28312992/convert-google-doc-to-pdf-using-google-script-editior

    function convertPDF() {
    doc = DocumentApp.getActiveDocument();
    // ADDED
  3. pkorpine created this gist May 31, 2021.
    42 changes: 42 additions & 0 deletions save_as_pdf_gs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    function onOpen() {
    var ui = DocumentApp.getUi();
    // Or DocumentApp or FormApp.
    ui.createMenu('My scripts')
    .addItem('Convert to PDF', 'convertPDF')
    .addToUi();
    }

    // https://stackoverflow.com/questions/28312992/convert-google-doc-to-pdf-using-google-script-editior
    function convertPDF() {
    doc = DocumentApp.getActiveDocument();
    // ADDED
    var docId = doc.getId();
    var docFolder = DriveApp.getFileById(docId).getParents().next().getId();
    // ADDED
    var ui = DocumentApp.getUi();
    var result = ui.alert(
    'Save As PDF?',
    'Save current document (Name:'+doc.getName()+'.pdf) as PDF',
    ui.ButtonSet.YES_NO);
    if (result == ui.Button.YES) {
    docblob = DocumentApp.getActiveDocument().getAs('application/pdf');
    /* Add the PDF extension */
    docblob.setName(doc.getName() + ".pdf");
    var file = DriveApp.createFile(docblob);
    // ADDED
    var fileId = file.getId();
    moveFileId(fileId, docFolder);
    // ADDED
    ui.alert('Your PDF file is available at ' + file.getUrl());
    } else {
    ui.alert('Request has been cancelled.');
    }
    }

    function moveFileId(fileId, toFolderId) {
    var file = DriveApp.getFileById(fileId);
    var source_folder = DriveApp.getFileById(fileId).getParents().next();
    var folder = DriveApp.getFolderById(toFolderId)
    folder.addFile(file);
    source_folder.removeFile(file);
    }