When automating the generation of physical assets like event tickets, inventory labels, or security badges using Google Docs, you often need to bridge the digital and physical worlds. A common requirement is dynamically embedding a QR code into the document that links back to a specific database entry or URL. Because Google Docs does not have a native “Insert QR Code” formula (unlike Google Sheets), developers must utilize Google Apps Script in conjunction with an external image generation API to automatically fetch, size, and inject these codes into dynamic Document templates.
Setting Up the Google Docs Template
First, you must create a standard Google Doc that will serve as your template. Within this document, use unique text placeholders wrapped in double curly braces (e.g., {{GUEST_NAME}}, {{TICKET_ID}}). To dictate exactly where the QR code should be injected, insert a specific placeholder string like {{QR_CODE_HERE}}.
This approach allows the Apps Script to easily find the target location, delete the placeholder text, and insert the generated image element in its exact position without disrupting the surrounding formatting.
Utilising an External QR Code API
Apps Script cannot mathematically generate the QR code pixels natively with acceptable performance. Instead, you should rely on a robust, free public API, such as the Google Chart API (which, although officially deprecated for some charts, remains highly reliable for QR codes) or QuickChart.io.
The URL structure for generating a QR code via QuickChart is simple. For example, to generate a 300×300 pixel QR code containing the text “ID-12345”, you construct the following URL:
https://quickchart.io/qr?size=300&text=ID-12345
Writing the Apps Script Injection Logic
Open your Google Doc, click Extensions, and select Apps Script. The core logic involves fetching the image blob from the API and replacing the placeholder text in the document body.
function generateAndInjectQRCode() {
// 1. Define the data for the QR code
var uniqueTicketId = "TKT-8675309";
var qrCodeUrl = "https://quickchart.io/qr?size=300&text=" + encodeURIComponent(uniqueTicketId);
// 2. Fetch the image blob from the external API
var response = UrlFetchApp.fetch(qrCodeUrl);
var imageBlob = response.getBlob();
// 3. Access the active document body
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
// 4. Locate the placeholder text
var targetText = "{{QR_CODE_HERE}}";
var searchResult = body.findText(targetText);
if (searchResult) {
// 5. Get the specific text element and its position
var textElement = searchResult.getElement();
var textOffset = searchResult.getStartOffset();
// 6. Navigate up to the parent paragraph
var parentParagraph = textElement.getParent();
// 7. Clear the placeholder text
textElement.asText().replaceText(targetText, "");
// 8. Inject the image blob directly into the paragraph
var inlineImage = parentParagraph.asParagraph().appendInlineImage(imageBlob);
// 9. Optional: Resize the image for consistency
inlineImage.setWidth(150);
inlineImage.setHeight(150);
} else {
Logger.log("Placeholder not found in the document.");
}
}
Automating Batch Generation
While the script above processes a single active document, the true power of this workflow lies in batch automation. In an enterprise scenario, this script would typically be bound to a Google Sheet containing hundreds of rows of guest data.
The script would iterate through each row in the Sheet, make a copy of the Google Docs template (using DriveApp.getFileById().makeCopy()), open the newly copied document by its ID, execute the text replacements, fetch the unique QR code for that specific row, inject it, and finally export the finished document as a PDF to be emailed directly to the guest. This entirely eliminates manual data entry and formatting inconsistencies when generating hundreds of physical assets.