How to Print a Document or Its Parts Using Google Apps Script

Printing documents programmatically can be very useful in automating workflows, especially when dealing with repetitive tasks. Google Apps Script provides a way to print documents from Google Docs, either as a whole or selectively. In this blog post, I’ll guide you through creating a Google Apps Script that prints an entire document or specific parts of it.

Prerequisites

Before we start, ensure you have:

  1. A Google account.
  2. A Google Doc you want to print.
  3. Basic understanding of Google Apps Script.

Step-by-Step Guide

Step 1: Create a Google Document

First, create or open the Google Doc that you want to print.

  1. Go to Google Drive.
  2. Click on New > Google Docs.
  3. Write or open the document you want to work with.

Step 2: Open Google Apps Script

  1. In the Google Doc, click on Extensions > Apps Script.
  2. Delete any default code in the script editor.

Step 3: Write the Google Apps Script

Add the following code to the script editor:

function printDocumentParts() {
// ID of the Google Doc to be printed
var docId = 'YOUR_DOC_ID';

// Open the document by ID
var doc = DocumentApp.openById(docId);

// Get the body of the document
var body = doc.getBody();

// Create a new temporary document for selected parts
var tempDoc = DocumentApp.create('Temp Print Document');
var tempBody = tempDoc.getBody();

// Define the parts you want to print (using paragraph indices)
var partsToPrint = [0, 2, 4]; // Example: print the 1st, 3rd, and 5th paragraphs

// Extract and append the parts to the temporary document
partsToPrint.forEach(function(index) {
var element = body.getChild(index).copy();
tempBody.appendParagraph(element.getText());
});

// Save and close the temporary document
tempDoc.saveAndClose();

// Fetch the temporary document content as a PDF blob
var pdfBlob = DriveApp.getFileById(tempDoc.getId()).getAs('application/pdf');

// Create a new blob output in the format of a PDF
var pdfOutput = DriveApp.createFile(pdfBlob);

// Log the URL of the PDF file
Logger.log('PDF file created: ' + pdfOutput.getUrl());

// Optionally, send the PDF to a printer via an external service or Google Cloud Print (deprecated)
// sendToPrinter(pdfOutput);

// Delete the temporary document
DriveApp.getFileById(tempDoc.getId()).setTrashed(true);
}

function printDocument() {
// ID of the Google Doc to be printed
var docId = 'YOUR_DOC_ID';

// Open the document by ID
var doc = DocumentApp.openById(docId);

// Fetch the document content as a PDF blob
var pdfBlob = doc.getAs('application/pdf');

// Create a new blob output in the format of a PDF
var pdfOutput = DriveApp.createFile(pdfBlob);

// Log the URL of the PDF file
Logger.log('PDF file created: ' + pdfOutput.getUrl());

// Optionally, send the PDF to a printer via an external service or Google Cloud Print (deprecated)
// sendToPrinter(pdfOutput);
}

Step 4: Explanation

  1. Document ID:
    • Replace 'YOUR_DOC_ID' with the ID of the Google Doc you want to print. The ID can be found in the URL of the document.
  2. Print Specific Parts:
    • This example uses paragraph indices to specify which parts of the document to print.
    • body.getChild(index).copy() fetches and copies the specified paragraph element.
    • appendParagraph(element.getText()) appends the text of the paragraph to the temporary document.
  3. Save and Close Temporary Document:
    • tempDoc.saveAndClose() ensures the temporary document is properly saved and closed before converting it to PDF.
  4. Creating PDF:
    • DriveApp.getFileById(tempDoc.getId()).getAs('application/pdf') fetches the temporary document as a PDF blob.
    • DriveApp.createFile(pdfBlob) creates a new PDF file in Google Drive.
    • The URL of the PDF file is logged for reference.

Step 5: Running the Script

  1. Save your script by clicking on File > Save.
  2. Click on the Run button (the triangle icon) to execute the printDocument or printDocumentParts function.
  3. You might need to authorize the script to access your Google Drive and Docs. Follow the on-screen prompts to grant the necessary permissions.

Step 6: Testing

Check your Google Drive for the newly created PDF file. You can also check the Apps Script logs for the URL of the PDF file.

Conclusion

By following these steps, you can automate the process of printing entire documents or specific parts of them using Google Apps Script. This method is efficient and helps you manage document printing in a more streamlined manner.