我正在使用导出的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)代码:
@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。
谢谢
发布于 2017-12-06 18:32:01
我认为你应该尝试使用媒体下载,你将不得不改变它为谷歌驱动器,而不是存储服务。
{
// 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的代码
https://stackoverflow.com/questions/47671567
复制相似问题