Skip to content

Instantly share code, notes, and snippets.

@501A-Designs
Created November 10, 2021 00:04
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save 501A-Designs/0c95b43ea459ac230e7611b8b077179f to your computer and use it in GitHub Desktop.
Google Docs from a folder and converts it into a PDF
function doGet(){return HtmlService.createHtmlOutputFromFile("index")}
function myFunction() {
var folderId = " "; // folder with docs
var moveToFolderId = " "; // folder to move PDF
var doneFolderId = " "; // folder with docs that have already been converted to PDF (to reduce duplication)
var filesN = DriveApp.getFolderById(folderId).getFiles();
var downloadedFolder = DriveApp.getFolderById(moveToFolderId);
var doneFolder = DriveApp.getFolderById(doneFolderId);
while(filesN.hasNext()){
var doc = filesN.next();
doc.moveTo(doneFolder);
var docName = doc.getName();
docBlob = doc.getAs('application/pdf');
docBlob.setName(docName + ".pdf");
var file = downloadedFolder.createFile(docBlob);
Logger.log('Your PDF file is available at ' + file.getUrl());
}
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<style>
body{
padding:5%;
font-family:arial;
}
header{
border-radius:10px;
padding:1em;
box-shadow: 0px 5px 10px #e6e6e6;
}
#status{
display:none;
margin: 0.5em 0;
padding: 1em;
border-radius:5px;
}
#status div{
padding:0;
margin:0;
display:flex;
gap:1em;
flex-direction:row;
align-items:center;
}
#doneId{
color:#4c8bf5;
margin: 0.5em 0 1.5em 0;
padding: 0.8em 1em;
border-radius:5px;
}
button{
cursor:pointer;
width: auto;
border-radius: 5px;
background: #4c8bf5;
font-weight:bold;
color: #fff;
outline: none;
border: none;
padding: 15px;
transition: .5s;
}
button:hover{
box-shadow: 0 1px 5px lightblue;
}
</style>
<script>
function startConversion(){
google.script.run.withSuccessHandler(createUpdate).myFunction();
document.getElementById("status").style.display = 'block';
}
function createUpdate(){
document.getElementById("status").style.display = 'none';
var done = document.createElement('div');
done.setAttribute('id', 'doneId');
done.innerHTML = "<h3>✓ Successfully Converted!</h3>";
document.getElementById("updates").appendChild(done);
}
</script>
<body>
<header>
<h1>Doc to PDF Converter</h1>
</header>
<ol>
<li>Instructions go here</li>
</ol>
<div id="updates">
<div id="status">
<div>
<img width="40" height="40" src="https://c.tenor.com/I6kN-6X7nhAAAAAj/loading-buffering.gif">
<p>Loading, please wait.</p>
</div>
</div>
</div>
<button onclick="startConversion();">Convert Doc to PDF</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment