How to convert PDF to image using Google Apps Script? [duplicate] - google-apps-script

is there a way to create an image (e.g. a png) from a google document?
I really mean an image, not just a pdf. GetAS only creates pdf, but returns an error if contentType is set to image/png or other equivalent formats.
My (actually trivial) code is
function convertFile() {
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var source = DriveApp.getFileById(SOURCE_TEMPLATE);
var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);
var target = source.makeCopy(source,targetFolder);
var newFile = DriveApp.createFile(target.getAs('image/png'));
}
When I run this code, I get the following error (my translation):
The conversion from application/vnd.google-apps.document to image/png is not supported.
Ty

How about this answer?
Reason of error:
makeCopy() returns File object. getAs() cannot be used for this. By this, the error occurs.
Workaround:
Unfortunately, in the current stage, Google Document cannot be directly exported as PNG images. So it is required to think of workarounds. Google Document can be converted to PDF. This answer uses this. As a workaround, I would like to propose to use an external API which is ConvertAPI. I thought that using the external API, the script becomes simple. This a method (PDF to PNG API) of API can be converted from PDF data to PNG data.
When you try this, for example, you can also test this using "Free Package". When you try using "Free Package", please Sign Up at "Free Package" and retrieve your Secret key.
Sample script:
Before you run this script, please retrieve your Secret key and set it.
var secretkey = "###"; // Please set your secret key.
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var url = "https://v2.convertapi.com/convert/pdf/to/png?Secret=" + secretkey;
var options = {
method: "post",
payload: {File: DriveApp.getFileById(SOURCE_TEMPLATE).getBlob()},
}
var res = UrlFetchApp.fetch(url, options);
res = JSON.parse(res.getContentText());
res.Files.forEach(function(e) {
var blob = Utilities.newBlob(Utilities.base64Decode(e.FileData), "image/png", e.FileName);
DriveApp.getFolderById(TARGET_FOLDER).createFile(blob);
});
References:
makeCopy()
getAs()
ConvertAPI
PDF to PNG API of ConvertAPI
Updated on January 11, 2023:
In the current stage, Google Apps Script can use V8 runtime. By this, there are some Javascript libraries that can be used with Google Apps Script. Ref1, Ref2 In this question, in the current stage, by using pdf-lib, all pages in a PDF file can be converted to PNG images using Google Apps Script. The sample script is as follows.
Sample script:
This method uses Drive API. Please enable Drive API at Advanced Google services.
Please set SOURCE_TEMPLATE and TARGET_FOLDER, and run main().
/**
* This is a method for converting all pages in a PDF file to PNG images.
* PNG images are returned as BlobSource[].
* IMPORTANT: This method uses Drive API. Please enable Drive API at Advanced Google services.
*
* #param {Blob} blob Blob of PDF file.
* #return {BlobSource[]} PNG blobs.
*/
async function convertPDFToPNG_(blob) {
// Convert PDF to PNG images.
const cdnjs = "https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js";
eval(UrlFetchApp.fetch(cdnjs).getContentText()); // Load pdf-lib
const setTimeout = function (f, t) { // Overwrite setTimeout with Google Apps Script.
Utilities.sleep(t);
return f();
}
const data = new Uint8Array(blob.getBytes());
const pdfData = await PDFLib.PDFDocument.load(data);
const pageLength = pdfData.getPageCount();
console.log(`Total pages: ${pageLength}`);
const obj = { imageBlobs: [], fileIds: [] };
for (let i = 0; i < pageLength; i++) {
console.log(`Processing page: ${i + 1}`);
const pdfDoc = await PDFLib.PDFDocument.create();
const [page] = await pdfDoc.copyPages(pdfData, [i]);
pdfDoc.addPage(page);
const bytes = await pdfDoc.save();
const blob = Utilities.newBlob([...new Int8Array(bytes)], MimeType.PDF, `sample${i + 1}.pdf`);
const id = DriveApp.createFile(blob).getId();
Utilities.sleep(3000); // This is used for preparing the thumbnail of the created file.
const link = Drive.Files.get(id, { fields: "thumbnailLink" }).thumbnailLink;
if (!link) {
throw new Error("In this case, please increase the value of 3000 in Utilities.sleep(3000), and test it again.");
}
const imageBlob = UrlFetchApp.fetch(link.replace(/\=s\d*/, "=s1000")).getBlob().setName(`page${i + 1}.png`);
obj.imageBlobs.push(imageBlob);
obj.fileIds.push(id);
}
obj.fileIds.forEach(id => DriveApp.getFileById(id).setTrashed(true));
return obj.imageBlobs;
}
// Please run this function.
async function myFunction() {
const SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
const TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
// Use a method for converting all pages in a PDF file to PNG images.
const blob = DriveApp.getFileById(SOURCE_TEMPLATE).getBlob();
const imageBlobs = await convertPDFToPNG_(blob);
// As a sample, create PNG images as PNG files.
const folder = DriveApp.getFolderById(TARGET_FOLDER);
imageBlobs.forEach(b => folder.createFile(b));
}
When this script is run, all pages of the inputted PDF file are converted to PNG images, and those images are created in the destination folder.
Note:
I think that the above script works. But, in this case, when you directly copy and paste the Javascript retrieved from https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js to your Google Apps Script project, the process cost for loading it can be reduced.
References:
pdf-lib
copyPages of pdf-lib
addPage of pdf-lib

I know this is an older question, but I thought I'd answer, since I believe I've found a solution that doesn't involve paying for a third-party subscription.
This can be accomplished by accessing the thumbnail of the Doc and creating a new PNG file from that thumbnail. Try this:
function convertFile() {
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var source = DriveApp.getFileById(SOURCE_TEMPLATE).getThumbnail().getAs('image/png');
var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);
TARGET_FOLDER.createFile(source);
}
However, I've found that getting the thumbnail of the Doc is not as high quality as getting the thumbnail of a PDF created from the Doc. You can try the code below to compare which version of the new PNG you prefer.
To do this, you will also need to enable Advanced Services on your project, specifically the Drive API service. To do this, follow these instructions to add a new Service to your Google Apps Script project:
Open the Apps Script project.
At the left, click Editor < >.
At the left, next to Services, click Add a service +.
Select Drive API and click Add.
Once you do that, you'll be able to use the Drive command in your script, which is different than DriveApp. Note also the update to source.makeCopy() to only include the TARGET_FOLDER:
function convertFile() {
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var source = DriveApp.getFileById(SOURCE_TEMPLATE);
var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);
var target = source.makeCopy(targetFolder);
var pdfBlob = target.getAs(MimeType.PDF);
var newPDF = TARGET_FOLDER.createFile(pdfBlob).setName('Some Name.pdf');
var newId = newPDF.getId();
Drive.Files.update({
title: newPDF.getName(), mimeType: MimeType.PDF
}, newId, pdfBlob);
var newFile = DriveApp.getFileById(newId).getThumbnail().getAs('image/png');
TARGET_FOLDER.createFile(newFile);
target.setTrashed(true);
newPDF.setTrashed(true);
}
This code will create a copy of your Google Doc file, convert it to a PDF, then grab the thumbnail of the PDF as a PNG, and then delete the copy of the Doc file and the PDF that were created.
The Drive.Files.update() function is the critical part of this code, as it finalizes the creation of the PDF file in your Drive. Trying to run the code without that portion will just return the new PDF file as null since the new PDF hasn't completely finished being created at that point.
Hope this helps!

Related

Prevent Google Drive Service API from extracting text when converting images

I've got a script that processes files uploaded to Google Drive and if they are not pdf files it converts them into Google Docs, then converts the Google Doc file to pdf. This all works as required.
The problem I have is if the file to be converted to Google Docs is an image file, the "new" Google Docs file not only contains the image but also text extracted via OCR (I assume). I've tried setting that option to false to no avail.
Please help, all I want is to convert image files to pdf without extracting any text from the images.
function afterUpload(form_upload_items, suid, lname,scans_folder){
var pdf_file_ids, pdf_files, processed_folder, converted_file;
form_upload_items.forEach (function (form_upload_item_id) {
var uploaded_file = DriveApp.getFileById(form_upload_item_id);
const uft = uploaded_file.getMimeType();
Logger.log("item has name of '%s' and is of type '%s'", uploaded_file.
getName(), uft);
processed_folder = scans_folder.getFoldersByName("Processed").next();
var file_description = uploaded_file.getDescription();
if (file_description==null){
file_description=" "
}
if (file_description.includes("Scanned")){
Logger.log("Already scanned '%s'", uploaded_file.getName());
}
else{
uploaded_file.setDescription("Scanned");
file_description = uploaded_file.getDescription();
}
if (file_description.includes("Processed")) {
Logger.log("Already processed %s", uploaded_file.getName());
}
else{
if (uft !=MimeType.PDF && !file_description.includes("Converted")){ // convert file to pdf if required
//Convert to Google docs
const file_blob = uploaded_file.getBlob();
const options ={
ocr: false,
supportsAllDrives: true
}
const res = Drive.Files.insert({ // Drive API of Advanced Google services is used.
"mimeType": "application/vnd.google-apps.document",
"title": uploaded_file.getName() + "_gdocs_conv"
}, file_blob,options);
uploaded_file.moveTo(processed_folder);
uploaded_file.setDescription(file_description+", Converted");
converted_file = DriveApp.getFileById(res.id);
converted_file.moveTo(processed_folder);
const blobPDF = converted_file.getAs(MimeType.PDF);
//finally convert google docs to PDF
uploaded_file = scans_folder.createFile(blobPDF);
Logger.log("Converted %s to pdf",uploaded_file.getName());
}
}
});
const query = "title contains '" +suid+"'"
const scanned_files = scans_folder.searchFiles(query);
pdf_file_ids = [];
pdf_files = [];
while (scanned_files.hasNext()){
const scanned_file = scanned_files.next();
Logger.log("%s %s",scanned_file.getName(), scanned_file.getDescription());
pdf_file_ids.push(scanned_file.getId());
pdf_files.push(scanned_file);
}
var function_result = {};
var documents_folder = scans_folder.getParents().next();
function_result["file_name_prefix"] = suid+" "+lname;
function_result["pdf_files"] = pdf_file_ids;
function_result["documents_folder_id"] = documents_folder.getId();
}
The problem is that you are converting image files into a Google Document, instead of creating a file type that could be converted into a PDF by Google Drive. To do this, insert the image to this newly created file then convert the resulting document into a PDF. Depending on the result that you requires you might create an HTML file, Google Document or a Google Slides (Google Drawings might be the best but there isn't a Google Apps Script service for that).
The "easier" solution is to create an HTML file. Here is a sample adapted from Tanaike's answer to Show images inside a pdf created with Gloogle Apps Script Blob
function myFunction() {
// image
var url = "https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a";
var blob = UrlFetchApp.fetch(url).getBlob();
var b64 = blob.getContentType() + ';base64,'+ Utilities.base64Encode(blob.getBytes());
var html =`<img src="data:${ b64 }">`;
var h = HtmlService.createHtmlOutput(html);
// Create from HTML to PDF file.
DriveApp.createFile(h.getAs("application/pdf").setName("text.pdf"));
}
Related
getBlob() causing "Invalid image data." Error. Google Apps Script

Download HEIC file in Google Photos as JPG

I am using this code to upload the heic file and finding a way to convert heic as jpg or png via apps script (not right click save as)
I have my code
function uploadMediaItems() {
const albumId = "AKwCquOrmWbEihvwEqIM7Jy_H8wrkM_dof1eIGwXq8YZV-uuj9jWvkMT5oWkG6P0a-w_w9VyTOxM"; // Please set the album ID.
var arrId =
[
"1ssNYJFSEdfRI55IUi_tte3iC_JcPLG-0"
];
const items = arrId.map(id => {
const file = DriveApp.getFileById(id);
return {blob: file.getBlob(), filename: file.getName()};
});
const res = GPhotoApp.uploadMediaItems({albumId: albumId, items: items});
console.log(JSON.stringify(res))
}
Source: Google Apps Scripts: import (upload) media from Google Drive to Google Photos?
I believe your goal is as follows.
You want to upload the HEIC file to Google Photos and export it as a Jpeg or PNG file using the sample script at this thread.
From your script, the HEIC file is put on your Google Drive.
I thought that in your goal, you might have wanted to convert the HEIC file to Jpeg or PNG file. In this case, how about the following sample scripts?
Sample script 1:
In this sample script, the sample script at this thread is used. So, in this case, a Google Apps Script library of GPhotoApp is used. Ref The sample script is as follows.
In this script, it supposes that your Google Apps Script project has already been linked with Cloud Platform Project, and Photos API has already been enabled, and also, the required scopes have already been added. Please be careful this.
function myFunction() {
const arrId = ["###"]; // Please set the file ID of your HEIC file.
const albumId = "###"; // Please set your albumn ID.
const items = arrId.map(id => {
const file = DriveApp.getFileById(id);
return { blob: file.getBlob(), filename: file.getName() };
});
const res1 = GPhotoApp.uploadMediaItems({ albumId: albumId, items: items });
const mediaItemId = res1.newMediaItemResults[0].mediaItem.id;
const res2 = GPhotoApp.getMediaItems({ mediaItemIds: [mediaItemId] });
const mediaItem = res2.mediaItemResults[0].mediaItem;
const blob = UrlFetchApp.fetch(mediaItem.baseUrl).getBlob();
DriveApp.createFile(blob.setName(mediaItem.filename.replace(".heic", ".png")));
}
When this script is run, the PNG file is created to the root folder.
Sample script 2:
The HEIC file is put on your Google Drive. And, you want to convert the HEIC file to Jpeg or PNG. In this case, I thought that this can be achieved using the following simple script. In this case, Photos API is not required to be used.
When you use this script, please enable Drive API at Advanced Google services.
function myFunction2() {
const fileId = "###"; // Please set the file ID of your HEIC file.
const obj = Drive.Files.get(fileId);
const blob = UrlFetchApp.fetch(obj.thumbnailLink.replace(/=s.+/, "=s2000")).getBlob();
DriveApp.createFile(blob.setName(obj.title.replace(".heic", ".png")));
}
When this script is run, the PNG file is created to the root folder.
References:
Photos Library API
GPhotoApp
Files: get

Slow Request/Response - Google OCR API using App Inventor

I am coding an app using App Inventor, the objective of the app is to OCR some image taken from camera/ selected from gallery, and display the ocr content.
i used base64 encoding to transform the image to string, send to google drive by google script, conduct ocr by google script and return the result back to my app.
it worked but for each image (150-1000kB), it take around 7-10 sec to process, as i would like to ocr over 100 images on site operation, the processing time is just too long for practical use.
the following are my App Inventor Block
Base64 Encoding
Add Picture from Gallery
Send request to Google Script
Display result from Google Script
the following are the google script
function doPost(e) {
var data = Utilities.base64Decode(e.parameters.data);
var blob = Utilities.newBlob(data, e.parameters.mimetype, e.parameters.filename);
//provide here a folder ID for the creation of the image file
var fileID = DriveApp.getFolderById('<Folder ID>').createFile(blob).getId();
if ( fileID !== "" ) {
try {
// Fetch the image from drive
var imageBlob = DriveApp.getFileById(fileID).getBlob();
var resource = { title: e.parameters.filename, mimeType: imageBlob.getContentType() };
// OCR on .jpg, .png, .gif, (or .pdf uploads)
var options = { ocr: true };
var docFile = Drive.Files.insert(resource, imageBlob, options);
var doc = DocumentApp.openById(docFile.id);
// Extract the text body of the Google Document
var text = doc.getBody().getText().replace("\n", "");
// Send the documents to trash
Drive.Files.remove(docFile.id);
Drive.Files.remove(fileID);
status = text;
} catch (error) {
status = "ERROR with OCR: " + error.toString();
}
} else {
status = "ERROR with OCR: No image specified";
}
return ContentService.createTextOutput(status);
}```

Convert a gdoc into image

is there a way to create an image (e.g. a png) from a google document?
I really mean an image, not just a pdf. GetAS only creates pdf, but returns an error if contentType is set to image/png or other equivalent formats.
My (actually trivial) code is
function convertFile() {
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var source = DriveApp.getFileById(SOURCE_TEMPLATE);
var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);
var target = source.makeCopy(source,targetFolder);
var newFile = DriveApp.createFile(target.getAs('image/png'));
}
When I run this code, I get the following error (my translation):
The conversion from application/vnd.google-apps.document to image/png is not supported.
Ty
How about this answer?
Reason of error:
makeCopy() returns File object. getAs() cannot be used for this. By this, the error occurs.
Workaround:
Unfortunately, in the current stage, Google Document cannot be directly exported as PNG images. So it is required to think of workarounds. Google Document can be converted to PDF. This answer uses this. As a workaround, I would like to propose to use an external API which is ConvertAPI. I thought that using the external API, the script becomes simple. This a method (PDF to PNG API) of API can be converted from PDF data to PNG data.
When you try this, for example, you can also test this using "Free Package". When you try using "Free Package", please Sign Up at "Free Package" and retrieve your Secret key.
Sample script:
Before you run this script, please retrieve your Secret key and set it.
var secretkey = "###"; // Please set your secret key.
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var url = "https://v2.convertapi.com/convert/pdf/to/png?Secret=" + secretkey;
var options = {
method: "post",
payload: {File: DriveApp.getFileById(SOURCE_TEMPLATE).getBlob()},
}
var res = UrlFetchApp.fetch(url, options);
res = JSON.parse(res.getContentText());
res.Files.forEach(function(e) {
var blob = Utilities.newBlob(Utilities.base64Decode(e.FileData), "image/png", e.FileName);
DriveApp.getFolderById(TARGET_FOLDER).createFile(blob);
});
References:
makeCopy()
getAs()
ConvertAPI
PDF to PNG API of ConvertAPI
Updated on January 11, 2023:
In the current stage, Google Apps Script can use V8 runtime. By this, there are some Javascript libraries that can be used with Google Apps Script. Ref1, Ref2 In this question, in the current stage, by using pdf-lib, all pages in a PDF file can be converted to PNG images using Google Apps Script. The sample script is as follows.
Sample script:
This method uses Drive API. Please enable Drive API at Advanced Google services.
Please set SOURCE_TEMPLATE and TARGET_FOLDER, and run main().
/**
* This is a method for converting all pages in a PDF file to PNG images.
* PNG images are returned as BlobSource[].
* IMPORTANT: This method uses Drive API. Please enable Drive API at Advanced Google services.
*
* #param {Blob} blob Blob of PDF file.
* #return {BlobSource[]} PNG blobs.
*/
async function convertPDFToPNG_(blob) {
// Convert PDF to PNG images.
const cdnjs = "https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js";
eval(UrlFetchApp.fetch(cdnjs).getContentText()); // Load pdf-lib
const setTimeout = function (f, t) { // Overwrite setTimeout with Google Apps Script.
Utilities.sleep(t);
return f();
}
const data = new Uint8Array(blob.getBytes());
const pdfData = await PDFLib.PDFDocument.load(data);
const pageLength = pdfData.getPageCount();
console.log(`Total pages: ${pageLength}`);
const obj = { imageBlobs: [], fileIds: [] };
for (let i = 0; i < pageLength; i++) {
console.log(`Processing page: ${i + 1}`);
const pdfDoc = await PDFLib.PDFDocument.create();
const [page] = await pdfDoc.copyPages(pdfData, [i]);
pdfDoc.addPage(page);
const bytes = await pdfDoc.save();
const blob = Utilities.newBlob([...new Int8Array(bytes)], MimeType.PDF, `sample${i + 1}.pdf`);
const id = DriveApp.createFile(blob).getId();
Utilities.sleep(3000); // This is used for preparing the thumbnail of the created file.
const link = Drive.Files.get(id, { fields: "thumbnailLink" }).thumbnailLink;
if (!link) {
throw new Error("In this case, please increase the value of 3000 in Utilities.sleep(3000), and test it again.");
}
const imageBlob = UrlFetchApp.fetch(link.replace(/\=s\d*/, "=s1000")).getBlob().setName(`page${i + 1}.png`);
obj.imageBlobs.push(imageBlob);
obj.fileIds.push(id);
}
obj.fileIds.forEach(id => DriveApp.getFileById(id).setTrashed(true));
return obj.imageBlobs;
}
// Please run this function.
async function myFunction() {
const SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
const TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
// Use a method for converting all pages in a PDF file to PNG images.
const blob = DriveApp.getFileById(SOURCE_TEMPLATE).getBlob();
const imageBlobs = await convertPDFToPNG_(blob);
// As a sample, create PNG images as PNG files.
const folder = DriveApp.getFolderById(TARGET_FOLDER);
imageBlobs.forEach(b => folder.createFile(b));
}
When this script is run, all pages of the inputted PDF file are converted to PNG images, and those images are created in the destination folder.
Note:
I think that the above script works. But, in this case, when you directly copy and paste the Javascript retrieved from https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js to your Google Apps Script project, the process cost for loading it can be reduced.
References:
pdf-lib
copyPages of pdf-lib
addPage of pdf-lib
I know this is an older question, but I thought I'd answer, since I believe I've found a solution that doesn't involve paying for a third-party subscription.
This can be accomplished by accessing the thumbnail of the Doc and creating a new PNG file from that thumbnail. Try this:
function convertFile() {
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var source = DriveApp.getFileById(SOURCE_TEMPLATE).getThumbnail().getAs('image/png');
var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);
TARGET_FOLDER.createFile(source);
}
However, I've found that getting the thumbnail of the Doc is not as high quality as getting the thumbnail of a PDF created from the Doc. You can try the code below to compare which version of the new PNG you prefer.
To do this, you will also need to enable Advanced Services on your project, specifically the Drive API service. To do this, follow these instructions to add a new Service to your Google Apps Script project:
Open the Apps Script project.
At the left, click Editor < >.
At the left, next to Services, click Add a service +.
Select Drive API and click Add.
Once you do that, you'll be able to use the Drive command in your script, which is different than DriveApp. Note also the update to source.makeCopy() to only include the TARGET_FOLDER:
function convertFile() {
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var source = DriveApp.getFileById(SOURCE_TEMPLATE);
var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);
var target = source.makeCopy(targetFolder);
var pdfBlob = target.getAs(MimeType.PDF);
var newPDF = TARGET_FOLDER.createFile(pdfBlob).setName('Some Name.pdf');
var newId = newPDF.getId();
Drive.Files.update({
title: newPDF.getName(), mimeType: MimeType.PDF
}, newId, pdfBlob);
var newFile = DriveApp.getFileById(newId).getThumbnail().getAs('image/png');
TARGET_FOLDER.createFile(newFile);
target.setTrashed(true);
newPDF.setTrashed(true);
}
This code will create a copy of your Google Doc file, convert it to a PDF, then grab the thumbnail of the PDF as a PNG, and then delete the copy of the Doc file and the PDF that were created.
The Drive.Files.update() function is the critical part of this code, as it finalizes the creation of the PDF file in your Drive. Trying to run the code without that portion will just return the new PDF file as null since the new PDF hasn't completely finished being created at that point.
Hope this helps!

Add convert xls to Google sheets feature to function of download

I have working code for download any file in google docs by link. Most of files in xlx or xlsx format and need to be convert in google sheets type. Ive been tried to use some methods, that I find in Internet, but they dont work for me. In code below, I comment with code wich I test. And I guess, that google had changed some documentation
function downloadFile(fileURL,folder) {
var fileName = "";
var fileSize = 0;
var fileId = "";
var response = UrlFetchApp.fetch(fileURL, {muteHttpExceptions: true});
var rc = response.getResponseCode();
if (rc == 200) {
var fileBlob = response.getBlob() //.getAs(MimeType.GOOGLE_SHEETS) - get error
var folder = DriveApp.getFolderById(folder);
if (folder != null) {
var file = folder.createFile(fileBlob);//.getAs(MimeType.GOOGLE_SHEETS) - get error
fileName = file.getName();
fileSize = file.getSize();
fileId = file.getId();
}
}
//file.setMimeType("application/vnd.google-apps.spreadsheet") - not work
//makeCopy('ssssss', folder, {convert: true}) - get error
var fileInfo = [ rc, fileName, fileSize, fileId ];
return fileInfo;
}
I call function like this:
downloadFile("http://www.beltools.ru/prais_rar/price%20TD%20RI.xls","0B_E2P3ZhQySBY3BBMzdlazBLcTA")
In order to convert Excel to Spreadsheet, DriveApp cannot do it. So Drive API has to be used. You can use Drive API v2 from Advanced Google services. "Drive API v2" can be used at Google Apps Script by enabling Drive API of Advanced Google services and of Google API Console.
How to use it is as follows.
In the script editor, select Resources > Advanced Google services
In the dialog that appears, click the on/off switch for Drive API v2.
At the bottom of the dialog, click the link for the Google API Console.
In the console, click into the filter box and type part of the name of the API "Drive API", then click the name once you see it.
On the next screen, click Enable API.
Close the Developers Console and return to the script editor. Click OK in the dialog. The advanced service you enabled is now available in autocomplete.
The detail information is https://developers.google.com/apps-script/guides/services/advanced.
At the sample script, at first, a file is downloaded by fileURL, which is Excel file, as blob. The blob data is uploaded to Google Drive using Drive API. In this case, access token is not required. For downloadFile(), the Input data and output data are same to your downloadFile(). File name is retrieved from fileURL.
Script :
function downloadFile(fileURL, folder) {
var filename = fileURL.match(".+/(.+?)([\?#;].*)?$")[1];
var response = UrlFetchApp.fetch(fileURL);
var rc = response.getResponseCode();
var blob = response.getBlob();
var resource = {
"mimeType": "application/vnd.google-apps.spreadsheet",
"parents": [{id: folder}],
"title": filename
};
var res = Drive.Files.insert(resource, blob);
var fileInfo = [rc, res.title, blob.getBytes().length, res.id];
return fileInfo;
}
Result :
[
200,
sample.xlsx,
10000.0,
## file id ##
]
If I misunderstand your question, I'm sorry.