Convert application/postscript to JPG - google-apps-script

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?

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.
Retrieve file metadata using the method of "Files: get" in Drive API.
Retrieve the EPS file (application/postscript) as PNG format, and convert it to Jpeg format.
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:
getAs(contentType)
Files: get

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.

Related

How do I extract the text from a docx file using Apps Script?

The files are saved in a Drive folder. I need to send the text content of all .docx file as an API payload. I've tried using Blob but to no avail. Is there a way to get this done?
If I understand you correctly, you want to send the text content of a docx file that you have in Drive. If that's correct, then you can do the following:
function docx() {
var docxId ="your-docx-id";
var docx = DriveApp.getFileById(docxId);
var blob = docx.getBlob();
var file = Drive.Files.insert({}, blob, {convert:true});
var id = file["id"];
var doc = DocumentApp.openById(id);
var text = doc.getBody().getText();
return text;
}
This code uses Advanced Drive Service to create a Docs file out of the blob you get from the docx, via Drive.Files.insert. Then, you can easily access this newly created file via DocumentApp and use getText.
Bear in mind that this will create a new file every time you run it. Use Files.delete to avoid that.
I hope this is of any help.

Convert html to google docs or docx using drive api v3

I'm trying to convert a html file (or preformatted html String) to Google Docs using drive api v3 and android studio, using these lines:
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("report.html")
.setMimeType("text/html")
.build();
(I extract the code from android-demos-master examples )
If I try put another mimetype like "application/vnd.google-apps.document", my app crash. I want to upload the file and convert to Gdocs editor or Docx. I need convert before or after upload the file. Can someone guide me?
Using the python libraries, I found I had to specify two mimetypes:
Use 'application/vnd.google-apps.document' when creating the metadata for the Drive file. This is the type of file you want created - a Google Document.
Use 'text/html' for the object representing uploaded data, as that is the type of the content. In python, this were objects of type io.MediaUpload (file upload) or io.MediaIoBaseUpload (in-memory content).
I imagine it's something similar in Java.

Attempting to Attach a CSV, Converting to PDF

I have a code where I am attempting to attach a CSV saved in Drive (automatically replaced) to an email. At the moment, I can get it to add using fileBlob - DriveApp.getFileById(...).getBlob() but, it's attaching as a PDF.
Per Mogsdad's answer on this question there is a way to attach a CSV, but looking through the documentation I can't seem to find anything that will allow using a CSV. In fact, according to the documentation for getAS() it literally says: "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."
Does this mean in is impossible to attach a CSV (or, really, anything other than PDF) from Drive to an email using Apps Scripts?
Assuming the CSV file is in your Drive, you can use this snippet to attach it to an email.
function sendCSV() {
var blob = DriveApp.getFileById(FILE_ID).getBlob();
MailApp.sendEmail("email#example.com", "Subject", "CSV", {attachments:[blob]});
}
As per following Google Documentation: -
https://developers.google.com/apps-script/reference/base/blob-source.html
Following is mentioned: -
For most blobs, 'application/pdf' is the only valid option.
Following expected work-around: -
https://gist.github.com/nirajkadam/0b41a01b8e739800c964

Javascript Writing to PDF or Excel

Hey Related to your answer on Can I write files with HTML5/JS?
How would I modify this for say Excel or PDF. I tried but was unable to get it to work. I get a corrupt file downloaded when I change MIME to application/pdf
I am trying to link it to data stored in localstorage which I have all in a variable.
My current code is:
function setSaveFile(contents, file_name, mime_type) {
var a = document.getElementById('save');
mime_type = mime_type || 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; // text/html, image/png, et c
if (file_name) a.setAttribute('Log.xls', file_name);
a.href = 'data:'+ mime_type +';base64,'+ btoa(contents || 'Description' + ' ' + 'Notes' + ' ' + 'Date\n\n' + pdftimeLog);
}
I want to be able to export it as a CSV with each of those headings: DATE, description, notes in different cells. I want it mobile friendly.Thanks
Excellent Options for CSV Everyone:
http://snapshotmedia.co.uk/blog/jspdf
Is it possible to use any HTML5 fanciness to export local storage to Excel?
http://css-tricks.com/localstorage-examples/
Local Storage manipulation and possible sending
Thanks!
For a file to be a valid pdf or excelfile their contents need to conform to the standard that defines the respective format, just changing mimetypes won't do much good.
For PDF there's a library jspdf that might work for you.
I haven't seen a solution for Excel yet, it will be a whole lot harder as the (xls) file format is quite complicated, for the time being it's probably preferable to generate the file on the server.
As an alternative to CSV, it's also possible to write an Excel file by writing HTML to a file with a .xls extension. Excel opens it up based on the extension. Haven't yet explored how deeply it can paginate, format.

Need to convert html to google doc native format using Java

I've read through the API documentation at https://developers.google.com/drive/v2/reference/ however I cannot find the answer to my question. And attempts to google a solution have failed.
I have a series of previously uploaded small HTML files sitting in Google Drive. What I want to do is write a short application to convert each of these to native Google Document format (mime type "application/vnd.google-apps.document").
I want to do this using Java code and not using GAS code.
The approach I used was to query drive for the File object corresponding to the item I want to convert. Then I pull that file's content as a string. Then I create a new file of mime type "application/vnd.google-apps.document" and upload it with the HTML content. Not surprisingly it didn't work.
So then I tried a different approach: Upload the content as "text/html" but set the "convert" flag to "true". Well I didn't see any direct API to set the convert flag to true. So I tried:
File oBody = new File() ;
oBody.setTitle ( sTitle ) ;
oBody.setDescription ( sDescription ) ;
oBody.setMimeType ( sMimeType ) ;
oBody.set("convert", bConvert);
This did not fail. But it did not create a Google Document either. It just created a text file identical to the original file.
How do I upload a document containing "text/html" content and get Google Drive to convert it automatically to a Google Document?
The convert flag has to be set in the files.insert request and not the File resource.
Using the snippet in the files.insert documentation as reference, this is what you should do:
...
File file = service.files().insert(body, mediaContent).setConvert(true).execute();
...