process_form.php 3.7 KB

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