process_form.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', 1);
  4. require_once 'vendor/autoload.php'; // Include the Google API Client Library
  5. // Initialize the Google API Client
  6. //$client = new Google_Client();
  7. $client = new Google\Client();
  8. $client->setAuthConfig('oauth-credentials.json'); // Path to your OAuth credentials JSON file
  9. $client->addScope(Google_Service_Docs::DOCUMENTS); // Set the appropriate scope for Google Docs
  10. //$client->addScope(Google\Service\Drive::DRIVE);
  11. $client->setAccessType('offline'); // offline access
  12. $client->setIncludeGrantedScopes(true); // incremental auth
  13. $redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  14. $client->setRedirectUri($redirect_uri);
  15. // Create a Google Docs service
  16. $service = new Google_Service_Docs($client);
  17. if (isset($_GET['code'])) {
  18. $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
  19. $client->setAccessToken($token);
  20. // store in the session also
  21. $_SESSION['upload_token'] = $token;
  22. // redirect back to the example
  23. header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  24. }
  25. // set the access token as part of the client
  26. if (!empty($_SESSION['upload_token'])) {
  27. $client->setAccessToken($_SESSION['upload_token']);
  28. if ($client->isAccessTokenExpired()) {
  29. unset($_SESSION['upload_token']);
  30. }
  31. } else {
  32. $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
  33. $authUrl = $client->createAuthUrl();
  34. }
  35. // Create a Google Docs service
  36. $service = new Google_Service_Docs($client);
  37. // Form data
  38. $clientName = $_POST['first_name']; // Assuming you have a form input named 'first_name'
  39. // Define the Google Doc ID of your template document
  40. $templateDocumentId = '1RaPRLceht4bElaIqwFVWBaH92428PJciu9sCZ5lQimU'; // Replace with the actual template document ID
  41. // Create a copy of the template document using Google Drive API
  42. $driveService = new Google_Service_Drive($client);
  43. $driveCopyMetadata = new Google_Service_Drive_DriveFile(array(
  44. 'name' => 'Copy of Template Document', // Set the name of the copy
  45. ));
  46. $copyDocument = $driveService->files->copy($templateDocumentId, $driveCopyMetadata);
  47. // Access the copied document using Google Docs API
  48. $service = new Google_Service_Docs($client);
  49. // Define the content you want to replace
  50. $replacements = [
  51. '<<CLIENT>>' => $clientName,
  52. // Add more placeholders and corresponding form values here
  53. ];
  54. // Loop through the placeholders and replace them in the copy document
  55. foreach ($replacements as $placeholder => $value) {
  56. $searchReplaceRequest = new Google_Service_Docs_Request(array(
  57. 'replaceAllText' => array(
  58. 'containsText' => array(
  59. 'text' => $placeholder,
  60. 'matchCase' => true,
  61. ),
  62. 'replaceText' => $value,
  63. ),
  64. ));
  65. $service->documents->batchUpdate($copyDocument->documentId, new Google_Service_Docs_BatchUpdateDocumentRequest(array(
  66. 'requests' => [$searchReplaceRequest],
  67. )));
  68. }
  69. // Export the copy document as a PDF (or any other format you prefer)
  70. $pdfFile = 'copied_document.pdf'; // Define the path where you want to save the copied document
  71. $pdfContent = $service->documents->get($copyDocument->documentId, array('exportFormat' => 'PDF'));
  72. file_put_contents($pdfFile, $pdfContent->getBody());
  73. // Provide a link to download the modified document (PDF)
  74. echo json_encode(array('download_link' => $pdfFile));
  75. ?>