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()) { 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'; // Retrieve the Google Doc $doc = $docsService->documents->get($documentId); // Replace variables $content = $doc->getBody()->getContent(); $content = str_replace('<>', $date, $content); $content = str_replace('<>', $name, $content); $content = str_replace('<>', $first_name, $content); $content = str_replace('<>', $company, $content); $content = str_replace('<>', $postal_address, $content); $content = str_replace('<>', $drg, $content); $content = str_replace('<>', $building_description, $content); $content = str_replace('<>', $site_address, $content); $content = str_replace('<>', $agent, $content); $content = str_replace('<>', $agent_company, $content); // Create a new Google Doc $newDoc = new Google_Service_Docs_Document(); $newDoc->setBody(new Google_Service_Docs_Body(['content' => $content])); // Save the new Google Doc to Google Drive $driveFile = new Google_Service_Drive_DriveFile(['name' => $file_name]); $driveFile = $driveService->files->copy($documentId, $driveFile); // 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.';