Export a specific page of a Google Doc to PDF via Google Apps Script
Asked Answered
S

0

7

The original code basically converts the Google Doc to PDF based on 2 different templates (2 different Google Doc with 2 different keys);

From the Google Form responses, if a standard part is selected, it will send the user an email with a quotation attached (PDF format). If not, then a different template will be sent out to the user instead:

1st template --> A quotation (standard parts)
2nd template --> Request has been received template (non standard parts)

So what I'm trying to do now is instead of using 2 different documents, I would like to have the templates in 1 document but converting only the specific page. For example, template 1 in page 1 and template 2 in pages 2 - 4.

Below code will only convert the whole document into PDF so I wonder if there is a special method or properties like getpagenumber to convert the specific page.

var docName = "insertdocnamehere";
var custName = "insertcustomernamehere";
var docTemplate = "insertgoogledockeyhere";

var copyId = DriveApp.getFileById(docTemplate)
  .makeCopy(docName+' for '+ custName)
  .getId();

// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);

// Get the document’s body section
var copyBody = copyDoc.getActiveSection();

copyDoc.saveAndClose();
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
Scarlettscarp answered 6/4, 2018 at 12:19 Comment(2)
Pages are a function of document formatting and display parameters. They are not currently available. I suggest you stick to the much more maintainable solution of 1 template = 1 google document. An alternate solution is to use Google Sheets, where you can set up a worksheet per template, reference the named sheet, and control which is printed by simply hiding the others.Alveolate
Thank you. I'll just stick to having 2 templates then.Scarlettscarp

© 2022 - 2024 — McMap. All rights reserved.