我希望使用谷歌应用程序脚本将EPS文件转换为JPG或PDF,我想知道这是否可能。我从这个代码开始:
var file = DriveApp.getFileById(fileID);
var conversion = file.makeCopy().getAs('image/jpeg');
然而,当我这样做时,我会收到以下错误消息:
"不支持从application/postscript转换为image/jpeg">
有可能进行这种转换吗?
我相信你的目标如下。
- 您希望使用Google Apps脚本将EPS文件(
application/postscript
(转换为Jpeg格式(image/jpeg
(
问题和解决方法:
不幸的是,在当前阶段,getAs
无法直接从application/postscript
转换为image/jpeg
。我认为这是目前的规范。因此,在这种情况下,需要使用一种变通方法。在这个答案中,我想提出这个变通办法。此解决方法的流程如下。
- 使用";文件:获取"在驱动器API中
- 将EPS文件(
application/postscript
(检索为PNG格式,并将其转换为Jpeg格式 - 将Jpeg数据创建为文件
当上面的流被用作脚本时,它变成如下。
示例脚本:
当您使用此功能时,请在高级谷歌服务中启用Drive API。在这个示例脚本中,EPS文件被转换为Jpeg格式。
var fileID = "###"; // Please set the file ID of the EPS file.
// 1. Retrieve file metadata using Drive API.
var res = Drive.Files.get(fileID);
// 2. Retrieve the EPS file (`application/postscript`) as PNG format, and convert it to Jpeg format.
var blob = UrlFetchApp
.fetch(res.thumbnailLink.replace("s220", "s1000"))
.getBlob()
.getAs(MimeType.JPEG)
.setName(res.title.split(".")[0] + ".jpg");
// 3. Create the Jpeg data as a file.
DriveApp.createFile(blob);
注:
- 在上述解决方案中,使用Drive API将EPS格式转换为PNG格式,使用
getAs
将PNG格式转换为Jpeg格式。但在这种变通方法中,它无法转换为PDF格式。因此,如果你想将EPS格式转换为PDF格式,我认为外部APIhttps://www.convertapi.com/eps-to-pdf可能是合适的
参考文献:
- getAs(contentType(
- 文件:获取
根据文档,您应该能够使用getAs('application/pdf')
对于大多数Blob,"application/pdf'"是唯一有效的选项。对于图像BMP、GIF、JPEG或PNG格式,"image.BMP"、"image/GIF"中的任何一种,"image/jpeg"或"image/png"也是有效的。