ソースを参照

Fix PDF export in old_g_letter.php using correct Drive API method

files->copy() with a mimeType option does not perform format
conversion — it copies the file as-is. Use files->export() with
'alt' => 'media' to correctly export the Google Doc as a PDF binary.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Benjamin Harris 2 週間 前
コミット
e659d3a857
1 ファイル変更5 行追加3 行削除
  1. 5 3
      old_g_letter.php

+ 5 - 3
old_g_letter.php

@@ -164,9 +164,11 @@ $driveService->files->update($driveFile->getId(), null, [
     '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']);
+// Export the Google Doc as a PDF using the Drive export endpoint.
+// files->copy() with a mimeType option does not convert formats — export() must be used instead.
+$pdfResponse = $driveService->files->export($driveFile->getId(), 'application/pdf', ['alt' => 'media']);
+$pdfBytes    = $pdfResponse->getBody()->getContents();
+file_put_contents($file_name . '.pdf', $pdfBytes);
 
 
 echo 'Letter copied, variables replaced, and saved in the client folder.';