首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Google Doc导出Google Drive Api Pdf生成空响应

从Google Doc导出Google Drive Api Pdf生成空响应
EN

Stack Overflow用户
提问于 2017-12-06 18:04:44
回答 1查看 1.6K关注 0票数 1

我正在使用导出的Google Drive API来检索Google Doc格式的Pdf:https://developers.google.com/drive/v3/reference/files/export

我遇到了以下问题:对于大于特定大小的文档(我不知道确切的阈值,但即使是相对较小的文件,大约1,5MB也会发生这种情况),API返回200响应代码,结果为空(通常它应该包含以字节流形式表示的pdf数据),如以下截图所示:

我可以通过GoogleDrive/GoogleDoc使用"File -> Download as.. -> Pdf“命令成功地导出文件,尽管这需要一些时间。

这是用于测试的文件(从Google Doc导出的1.180 KB ),我将其共享给您,以便您可以访问尝试导出:https://docs.google.com/document/d/18Cz7kHfEiDLeTWHyyoOi6U4kFQDMeg0D-CCJzILMMCk/edit?usp=sharing

下面是我用来执行该操作的(Java)代码:

代码语言:javascript
复制
@Override
public GoogleDriveDocumentContent downloadFileContentAsPDF(String executionGoogleUser, String fileId) {

    GoogleDriveDocumentContent documentContent = new GoogleDriveDocumentContent();

    String conversionMimeType = "application/pdf";

    try {

        getLogger().info("GDrive APIs - Downloading file content in PDF format ...");

        InputStream gDriveFileData = getDriveService(executionGoogleUser).files()
                .export(fileId, conversionMimeType)
                .executeMediaAsInputStream();

        getLogger().info("GDrive APIs - File content as PDF format downloaded.");

        documentContent.setFileName(null);
        documentContent.setMimeType(conversionMimeType);
        documentContent.setData(gDriveFileData);

    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return documentContent;

}

有没有人有同样的问题并且知道如何解决它?目标是从Google Doc生成一个pdf。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-12-06 18:32:01

我认为你应该尝试使用媒体下载,你将不得不改变它为谷歌驱动器,而不是存储服务。

代码语言:javascript
复制
{
    // Create the service using the client credentials.
    var storageService = new StorageService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "APP_NAME_HERE"
        });
    // Get the client request object for the bucket and desired object.
    var getRequest = storageService.Objects.Get("BUCKET_HERE", "OBJECT_HERE");
    using (var fileStream = new System.IO.FileStream(
        "FILE_PATH_HERE",
        System.IO.FileMode.Create,
        System.IO.FileAccess.Write))
    {
        // Add a handler which will be notified on progress changes.
        // It will notify on each chunk download and when the
        // download is completed or failed.
        getRequest.MediaDownloader.ProgressChanged += Download_ProgressChanged;
        getRequest.Download(fileStream);
    }
}

static void Download_ProgressChanged(IDownloadProgress progress)
{
    Console.WriteLine(progress.Status + " " + progress.BytesDownloaded);
}

摘自here的代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47671567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档