Reputation: 606
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
Reputation: 201378
I believe your goal as follows.
application/postscript
) to the Jpeg format (image/jpeg
) using Google Apps Script.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.
application/postscript
) as PNG format, and convert it to Jpeg format.When above flow is used as a script, it becomes as follows.
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);
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.Upvotes: 2
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