setAuthConfig('oauth-credentials.json'); $client->setScopes([Google_Service_Drive::DRIVE, Google_Service_Docs::DOCUMENTS]); $service = new Google\Service\Drive($client); // Authorize with Google if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']); $client->setAccessToken($token); // store in the session also $_SESSION['upload_token'] = $token; // redirect back to the example header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); } // set the access token as part of the client if (!empty($_SESSION['upload_token'])) { $client->setAccessToken($_SESSION['upload_token']); if ($client->isAccessTokenExpired()) { if ($client->getRefreshToken()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); $_SESSION['upload_token'] = $client->getAccessToken(); } else { unset($_SESSION['upload_token']); } } } else { $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier(); $authUrl = $client->createAuthUrl(); } // Create Google Drive and Google Docs service $driveService = new Google\Service\Drive($client); $docsService = new Google\Service\Docs($client); // Following may be required to future proof, here you can specify the title of the document instead of the document id /* // Specify the title (name) of your Google Doc $docTitle = 'Your Google Doc Title'; // List files in your Google Drive $files = $driveService->files->listFiles([ 'q' => "name='$docTitle' and mimeType='application/vnd.google-apps.document'", ]); if (count($files->getFiles()) > 0) { // Get the first matching document $documentId = $files->getFiles()[0]->getId(); } else { echo 'Google Doc not found.'; die; } */ // Replace with your Google Doc's Template file ID $documentId = '1RaPRLceht4bElaIqwFVWBaH92428PJciu9sCZ5lQimU'; // Copy the template into a new file — replacements happen on the copy, not the template $driveFile = new Google\Service\Drive\DriveFile(['name' => $file_name]); $driveFile = $driveService->files->copy($documentId, $driveFile); // Replace template placeholders using the Docs batchUpdate API. // getBody()->getContent() returns an array of objects, not a string — str_replace cannot be used here. $replacements = [ '<>' => $date, '<>' => $name, '<>' => $first_name, '<>' => $company, '<>' => $postal_address, '<>' => $drg, '<>' => $building_description, '<>' => $site_address, '<>' => $agent, '<>' => $agent_company, ]; $requests = []; foreach ($replacements as $placeholder => $value) { $requests[] = new Google\Service\Docs\Request([ 'replaceAllText' => [ 'containsText' => ['text' => $placeholder, 'matchCase' => true], 'replaceText' => $value, ], ]); } $docsService->documents->batchUpdate( $driveFile->getId(), new Google\Service\Docs\BatchUpdateDocumentRequest(['requests' => $requests]) ); // Find the Specified Folder to save documents in, // Specify the title (name) of your Google Doc $folderTitle = $drg . ' - ' . $site_address_street; // List files in your Google Drive $files = $driveService->files->listFiles([ 'q' => "name='$folderTitle' and mimeType='application/vnd.google-apps.folder'", ]); if (count($files->getFiles()) > 0) { // Get the first matching document $folderId = $files->getFiles()[0]->getId(); } else { echo 'Google folder not found.'; // Create the folder if it doesnt exist, // folder needs to be created inside folder called [03]Projects 2023 which is "[0Y] Projects YYYY" // $parent_folder = '[0' . substr(date(y), -1) . '] Projects' . date(Y); } // Move the copied file to a specific folder $driveService->files->update($driveFile->getId(), null, [ 'addParents' => '1ZwFTZ-YzFwYs1mN772WSET570E9ec-7O', 'removeParents' => $driveFile->getParents()[0], ]); // Create PDF $pdfFile = new Google_Service_Drive_DriveFile(['name' => $file_name . '.pdf']); $pdfFile = $driveService->files->copy($driveFile->getId(), $pdfFile, ['mimeType' => 'application/pdf']); echo 'Letter copied, variables replaced, and saved in the client folder.';