Google Drive API version 3 - Create file with content

1,278 views
Skip to first unread message

Alan Wells

unread,
Feb 13, 2020, 9:07:20 PM2/13/20
to Google Apps Script Community
With the Advanced Drive Service, there is a parameter for the file content:

Drive.Files.insert(resource, mediaData);//Create a new file

The parameter mediaData can take a blob.

The Advanced Drive Service uses the Google Drive API version 2, but there is now a version 3.
Version 3 doesn't have an "insert" method to create a new file, but instead has a "create" method.

I've been studying the documentation for the version 3 Drive API, but I can't figure out where the file content goes.

The request body doesn't have a parameter for the file content.  In the guide, step 3 it states:

Add the file's data to the request body


But how do I do that with a UrlFetchApp.fetch(url,options) call?

I'm assuming that it's in the payload of the options?
But what is the key name for the blob?

Tanaike

unread,
Feb 13, 2020, 9:34:50 PM2/13/20
to google-apps-sc...@googlegroups.com
In this case, it is required to request with the request body created for multipart/form-data in order to include the metadata and media body. There are several methods for Javascript and Node.js. But in the current stage, there are no built-in methods for creating the request body of multipart/form-data in Class UrlFetchApp. So it is required to manually create it using a script.

So I created a GAS library for achieving this.

You can see the sample script for the method of files.create in Drive API at this sample.

In this case, the maximum file size is 5 MB. Ref

If you want to upload a large file, please use Resumable upload. In this case, this might be useful.

If I misunderstood your question and this was not the direction you want, I apologize.

Alan Wells

unread,
Feb 14, 2020, 8:42:36 AM2/14/20
to Google Apps Script Community
Thank you.  I haven't tried it yet, but at first glance, I'm guessing that it will work for me.  My "use case" is converting HTML to a pdf blob and then creating a PDF file.  Are there any issues with that situation?

Tanaike

unread,
Feb 14, 2020, 6:31:05 PM2/14/20
to google-apps-sc...@googlegroups.com
Of course, the method of Files: create of Drive API can be converted HTML data to PDF data. In this case, at first, the HTML data is required to be converted to Google Document. Then, it can be exported as the PDF data. I think that the above GAS library can be also used for this.

As other method, when the HTML data is converted to a PDF data, there is also a method without using Drive API. For this, Cass Utilities is used. The sample script can be seen at https://stackoverflow.com/a/59711113/7108653

var blob = Utilities.newBlob(pdf_html, MimeType.HTML, form.name).getAs(MimeType.PDF);


Above script is that the HTML data can be converted to a PDF data. At Google Apps Script, when the HTML data created by the text is converted to a blob and give the mimeType of html, the blob can be converted to the PDF format using "getAs" method. On the other hand, when the mimeType of text is given at above script, an error occurs at "getAs". Please be careful this.

Alan Wells

unread,
Feb 15, 2020, 9:58:33 AM2/15/20
to Google Apps Script Community
Thank you again.  I was having trouble creating a PDF file from HTML.  I won't need to use the Drive API to convert the HTML to a PDF blob.
I had done a search on this issue, but the search had not returned the Stack Overflow post that you provided.  I've up-voted both the question and answer at Stack Overflow to give it more visibility.
I don't know if the issue that you have pointed out is a bug or expected behavior?  Does the error come from trying to convert a blob as a string to a PDF?

Tanaike

unread,
Feb 15, 2020, 6:18:37 PM2/15/20
to Google Apps Script Community
Thank you for replying. I apologize for the inconvenience. Unfortunately, I cannot replicate your situation. Because I'm not sure about your HTML. I thought that this might be due to the HTML data, and there might be HTML tags and so on which can be used and cannot be used. So can you test the following simple sample script?

For example, when the following sample script including a simple HTML data is used, the PDF file can be created without the error. In this case, this works with and without V8.

var html = '<b><font size="5" color="blue">sample text</font></b>';
var blob = Utilities.newBlob(html, MimeType.HTML, "sample.pdf").getAs(MimeType.PDF);
DriveApp.createFile(blob);


When you use above script in your environment, what result will you get?

Alan Wells

unread,
Feb 15, 2020, 7:58:10 PM2/15/20
to Google Apps Script Community
What I ended up doing is:

var html = "<html>Content here</html>"

var htmlOut = HtmlService.createHtmlOutput(html);
var pdfBlob = htmlOut.getAs('application/pdf');



The HtmlService.createHtmlOutput(html) returns a

htmlOutput object

which seems to be able to process the html correctly into a PDF blob with

var pdfBlob = htmlOutput.getAs('application/pdf');

That way I'm avoiding the use of the "Utilities.newBlob()" step.

I don't need to add an image to the HTML or process a template at the point where I need to convert the HTML to a pdf blob, so it seems like the shortest route.

Thank you for helping me out.  Unless something unexpected happens, it should be fixed now.

Tanaike

unread,
Feb 15, 2020, 8:50:12 PM2/15/20
to Google Apps Script Community
Thank you for replying. I'm glad you issue was resolved.

Martin MULLAN

unread,
Jul 7, 2022, 7:42:15 PM7/7/22
to Google Apps Script Community
Hi Tanaike,
I have been trying to automatically create a pdf of my htmloutput dashboards for my students. They contain several Gsheet linked dynamic data tables and ApexCharts. When the PDF is created it only has the main headers from the html template page in it: no data is present. It looks like the pdf is being created before the data is retrieved and presented; any idea on how I might fix the issue?
Regards< Mart.

Reply all
Reply to author
Forward
0 new messages