Lle.4
Lle.4

Reputation: 606

Convert application/postscript to JPG

I'm hoping to convert an EPS file to a JPG or a PDF using Google Apps Script, and I'm wondering if it's possible. I started with this code:

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

However, when I do this, I get the following error message:

"Converting from application/postscript to image/jpeg is not supported."

Is it possible to make this conversion?

Upvotes: 0

Views: 594

Answers (2)

Tanaike
Tanaike

Reputation: 201378

I believe your goal as follows.

  • You want to convert EPS file (application/postscript) to the Jpeg format (image/jpeg) using Google Apps Script.

Issue and workaround:

Unfortunately, in the current stage, getAs cannot directly convert from application/postscript to image/jpeg. I think that this is the current specification. So in this case, it is required to use a workaround. In this answer, I would like to propose this workaround. The flow of this workaround is as follows.

  1. Retrieve file metadata using the method of "Files: get" in Drive API.
  2. Retrieve the EPS file (application/postscript) as PNG format, and convert it to Jpeg format.
  3. Create the Jpeg data as a file.

When above flow is used as a script, it becomes as follows.

Sample script:

When you use this, please enable Drive API at Advanced Google services. In this sample script, the EPS file is converted to Jpeg format.

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);

Note:

  • In above workaround, the EPS format is converted to the PNG format using Drive API, and the PNG format is converted to the Jpeg format using getAs. But in this workaround, it cannot convert to the PDF format. So if you want to convert the EPS format to the PDF format, I think that the external API like https://www.convertapi.com/eps-to-pdf might be suitable.

References:

Upvotes: 2

PaulProgrammer
PaulProgrammer

Reputation: 17620

According to the documentation, you should be able to use 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.

Upvotes: 0

Related Questions