我使用导出Google云端硬盘API将Google文档检索为Pdf:https://developers.google.com/drive/v3/reference/files/export
我遇到以下问题:对于大于特定尺寸的文档(我不确切知道阈值,但即使文件大小相当于1.5 MB也会发生),API会返回200个响应代码,结果为空(通常它应包含pdf数据作为字节流),如下面的屏幕截图所示:
我可以使用"文件 - >成功通过GoogleDrive / GoogleDoc用户界面导出文件下载为.. - > 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文档生成pdf。
由于
答案 0 :(得分:0)
我认为你应该尝试使用媒体下载器,你必须为Google驱动器而不是存储服务改变它。
{
// 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