Home > Software design >  Can we insert html contents into google docs using google docs api with PHP?
Can we insert html contents into google docs using google docs api with PHP?

Time:02-24

I am working on google docs api with PHP code. I want to insert html contents into google docs. Is there any way doing this and if yes, How we can achieve that as I have searched a lot but couldn't found a proper answer or solution to this problem.

CodePudding user response:

I believe your goal is as follows.

  • You want to upload an HTML file to Google Drive by converting it to Google Document using googleapis for PHP.

In this case, how about the following sample script?

Sample script:

$service = new Google_Service_Drive($client); // Please use your authorization script.

$html_file = "./sample.html"; // Please set the filename with the path of HTML file.
$folder_id = "###"; // Please set the folder ID you want to put.

$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => 'sample',
    'parents' => [$folder_id ],
    'mimeType' => 'application/vnd.google-apps.document'
));
$res = $service->files->create($fileMetadata, array(
    'data' => file_get_contents($html_file),
    'mimeType' => 'text/html',
    'uploadType' => 'multipart',
    'fields' => 'id'
));
$file_id = $res->getId();
print($file_id);
  • In this script, the HTML file is uploaded to Google Drive, and the HTML data is converted to Google Document.

Reference:

  • Related