将 application/postscript 转换为 JPG

Convert application/postscript to JPG

我希望使用 Google Apps 脚本将 EPS 文件转换为 JPG 或 PDF,我想知道这是否可行。我从这段代码开始:

var file = DriveApp.getFileById(fileID);
var conversion = file.makeCopy().getAs('image/jpeg');

但是,当我这样做时,我收到以下错误消息:

“不支持从 application/postscript 转换为 image/jpeg。”

是否可以进行此转换?

根据 the documentation,您应该可以使用 getAs('application/pdf')

For most blobs, 'application/pdf' is the only valid option. For images in BMP, GIF, JPEG, or PNG format, any of 'image/bmp', 'image/gif', 'image/jpeg', or 'image/png' are also valid.

我相信你的目标如下。

  • 您想使用 Google Apps 脚本将 EPS 文件 (application/postscript) 转换为 Jpeg 格式 (image/jpeg)。

问题和解决方法:

遗憾的是,在现阶段,getAs无法直接从application/postscript转换为image/jpeg。我认为这是当前的规范。所以在这种情况下,需要使用变通方法。在这个答案中,我想提出这个解决方法。此解决方法的流程如下。

  1. 使用 Drive API 中的“Files:get”方法检索文件元数据。
  2. 将 EPS 文件 (application/postscript) 检索为 PNG 格式,并将其转换为 Jpeg 格式。
  3. 将 Jpeg 数据创建为文件。

以上流程作为脚本使用时,变成如下

示例脚本:

当您使用它时,请在高级 Google 服务中启用驱动器 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);

注:

  • 在上述解决方法中,使用驱动器 API 将 EPS 格式转换为 PNG 格式,使用 getAs 将 PNG 格式转换为 Jpeg 格式。但在此解决方法中,它无法转换为 PDF 格式。所以如果你想把EPS格式转换成PDF格式,我觉得像https://www.convertapi.com/eps-to-pdf这样的外部API可能比较合适。

参考文献: