GAS AI BuilderApps Script Assistant Sign in

Fill a Doc template and save it as PDF

Apps Script recipes › Email, Docs, Forms and PDF

Copies a Google Doc with {{placeholders}}, fills them in, saves a PDF in a folder and removes the copy.

File Code.gs

const TEMPLATE_DOC_ID = '';
const PDF_FOLDER_ID = '';

/** fields: { name: 'Rina', date: '3 November 2026' } → the PDF file. */
function makePdf_(fields, fileName) {
  const folder = DriveApp.getFolderById(PDF_FOLDER_ID);
  const copy = DriveApp.getFileById(TEMPLATE_DOC_ID).makeCopy(fileName, folder);
  const doc = DocumentApp.openById(copy.getId());
  const body = doc.getBody();
  Object.entries(fields).forEach(([key, value]) => {
    body.replaceText('\\{\\{' + key + '\\}\\}', String(value));
  });
  doc.saveAndClose();
  const pdf = folder.createFile(copy.getAs(MimeType.PDF)).setName(fileName + '.pdf');
  copy.setTrashed(true);
  return pdf;
}

Good to know

Let the AI put it in your appGAS AI Builder knows every recipe here. Describe your app and it uses this code where it fits, in a Google Apps Script project in your own Drive.

Use it in a project

More in Email, Docs, Forms and PDF

Make a real Google Form with codeCreates a Google Form once, keeps its ID, sends its responses to the project's Sheet and returns its links.Send an HTML email from a templateThe email body is an HTML file with <?= name ?> fields; checks the daily email quota first.