process_form.php 3.7 KB

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