如何使用 gdrive api 将 docx 或 html 转换为 pdf?

How convert docx or html to pdf use gdrive api?

我需要转换 html 或 docx 使用 grive api。

我的代码

    $client = new \Google_Client();
    $client->setAccessToken($_SESSION['access_token']);
    $service = new \Google_Service_Drive($client);


    $content = file_get_contents('template.html');//template.docx 
    $fileMetadata = new \Google_Service_Drive_DriveFile(array(
        'name' => 'test', // name file
        'mimeType' => 'application/pdf', // mime type for save in gdrive
        //'parents' => array($id_folder) 
            ));
    $file = $service->files->create($fileMetadata, array(
        'data' => $content, // file
        'mimeType' => 'text/html', // application/vnd.openxmlformats-officedocument.wordprocessingml.document
        //'uploadType' => 'multipart',
        'fields' => 'id')); // 

文件上传到 dgrive 正常,但是当我打开它时 test.pdf 我看到这个错误

Failed to load other pages

但是我的文件只有一页。

我该如何解决这个问题?

查看示例 here。特别是我认为您不需要在文档中找到我无法找到的字段元素。当您将文件上传为 text/html 时,请确保您拥有正确的元数据并启用多部分,因为您同时处理内容和元数据。

我还建议分小步进行,首先让它变得非常简单 html 内容并尝试如果可行,则可能是您的内容不让 pdf 正常输出.

如果您完成了其中一些示例,那么如果问题出在元数据、内容或其他任何方面,那么调试起来会很容易。