Is there a way to convert API PDF Code response into PDF in Google App Script? - google-apps-script

Using API I can obtain the PDF Coding for the invoice - in python I could write this response string directly in pdf and create therefore formatted document.
Is there a way to write this PDF code into PDF in google app script and get pdf formatted file?
Python Code Example (I need the same in google app script)
for list_invoice in list_invoices['invoices']:
response = requests.get('https://books.zoho.com/api/v3/invoices/'+str(list_invoice['invoice_id'])+'?organization_id=41285962&accept=pdf',
headers = {
"Content-Type" : "application/x-www-form-urlencoded;charset=UTF-8",
"Authorization" : "Zoho-oauthtoken "+authoken
}
)
#getting pdf response - writing pdf encoding and making respective pdf invoice on the desktop
with open('/Users/office/Desktop/'+str(invoice_number_hellocash_zoho)+'.pdf', 'wb') as f:
f.write(response.content)
Here is the google app script.
I get the error - Conversion from text/plain to application/pdf failed.
var url = 'https://books.zoho.com/api/v3/invoices/'+invoice_data+'?organization_id=41285962&accept=pdf';
var data = {
"Content-Type" : "application/x-www-form-urlencoded;charset=UTF-8",
"Authorization" : "Zoho-oauthtoken "+refresh_token_correct
}
var options = { "method":"GET",
"headers" : data
};
var pdf_encoded_response = UrlFetchApp.fetch(url, options);
var pdf_encoded_response_content = pdf_encoded_response.getContentText();
var pdfContent = pdf_encoded_response_content;
var intermediate = DriveApp.createFile('tempFile',pdfContent,MimeType.PLAIN_TEXT);
var blob = intermediate.getAs(MimeType.PDF);
Logger.log(blob.getContentType());
var pdfFile = DriveApp.createFile(blob);
DriveApp.getFileById(intermediate.getId()).setTrashed(true);

Related

Google Slides to PNG with Apps Script [duplicate]

Good Morning All. I have written a short script which batch-creates [single page] Google Slides based on rows from a spreadsheet. While in the loop for each creation, I would like to create a PNG of the Slide in Google Drive (or download on the user's desktop). These pictures should be the same specs as if a user clicked File>Download>PNG - the heavy small text requires full projector HD - so I don't believe I can use the "Thumbnail" function which appears limited to 1600 pixels.
My code below generates the error "Converting from text/html to image/png is not supported" - so I'm not sure if this is a limitation of the API or a problem with my coding. Thank you in advance.
var options =
{
"contentType" : "image/PNG"
};
var url = 'https://docs.google.com/presentation/d/' + presentationCopyId + '/export/PNG';
var response = UrlFetchApp.fetch(url, options);
var image = response.getAs(MimeType.PNG);
image.setName(SlideName);
DriveApp.createFile(image);
Yes, It is possible.
You can use Google slide API and make a PNG file of every page of Google slide.
Here is the code,
BUT first you have to enable API as following
open script editor
click on resources-> Advanced Google Services
Enable Google slide API and Drive API .
click on ok
now copy and paste this code,
and write your slide ID in presentationID.
function generateScreenshots() {
var presentationId = "***ADD YOUR Google Slide ID Only***";
var presentation = SlidesApp.openById(presentationId);
var baseUrl =
"https://slides.googleapis.com/v1/presentations/{presentationId}/pages/{pageObjectId}/thumbnail";
var parameters = {
method: "GET",
headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() },
contentType: "application/json",
muteHttpExceptions: true
};
// Log URL of the main thumbnail of the deck
Logger.log(Drive.Files.get(presentationId).thumbnailLink);
// For storing the screenshot image URLs
var screenshots = [];
var slides = presentation.getSlides().forEach(function(slide, index) {
var url = baseUrl
.replace("{presentationId}", presentationId)
.replace("{pageObjectId}", slide.getObjectId());
var response = JSON.parse(UrlFetchApp.fetch(url, parameters));
// Upload Googel Slide image to Google Drive
var blob = UrlFetchApp.fetch(response.contentUrl).getBlob();
DriveApp.createFile(blob).setName("Image " + (index + 1) + ".png");
screenshots.push(response.contentUrl);
});
return screenshots;
}
This answer is outdated, leaving up for documentation purposes but please see other answer.
Answer:
Unfortunately it is not possible to export a Slides as a PNG file using the the Slides nor Drive APIs.
More Information:
According to the documentation, there are only four available MimeTypes for exporting Presentations files:
application/vnd.openxmlformats-officedocument.presentationml.presentation
application/vnd.oasis.opendocument.presentation
application/pdf
text/plain
Attempting to export to the image/png MIME Type results in the following error:
Converting from text/html to image/png is not supported
For testing purposes, I tried using the /export/pdf endpoint and making a second conversion to PNG from there like so:
function slidesAsPngAttempt() {
var presentationCopyId = "1Loa...pQs";
var options =
{
"contentType" : "application/pdf"
};
// for exporting to pdf the /export/pdf needs to be all lower case to avoid 404
var url = 'https://docs.google.com/presentation/d/' + presentationCopyId + '/export/pdf';
var response = UrlFetchApp.fetch(url, options);
var pdfAsblob = response.getBlob();
var image = pdfAsblob.getAs('image/png');
image.setName(DriveApp.getFileById(presentationCopyId).getName());
DriveApp.createFile(image);
}
Unfortunately, a similar error occurs when running var image = pdfAsblob.getAs('image/png'):
Converting from application/pdf to image/png is not supported.
From the same export MIME types reference documentation, the only export types available for PDF files are:
text/csv
text/tab-separated-values
application/zip
References:
G Suite documents and corresponding export MIME types
Google Apps Script - method Blob.getAs(contentType)

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

documentapp.getactivedocument().getBlob gives pdf

i am trying to send the google document's content to my backend service.
In the app script i am using
if(host == 'sheets'){
var content = SpreadsheetApp.getActiveSpreadsheet().getBlob();
}else if (host == 'docs') {
var content = DocumentApp.getActiveDocument().getBlob();
}
I take the blob and sent it through multi part form request in URLFetchApp.fetch() through the payload parameter.
But the content for both the docs and sheets is converted/sent to my service as pdf.
Is there any way to preserve/send the files in google format itself ?
if not in google format then in Microsoft office formats ?
Best Regards,
Saurav
As mentioned in the similar post, this behavior is expected. If you want to get the file's content in Microsoft office formats,
you can check the following options:
(OPTION 1): Get export URL from Advanced Drive Service
Sample Code:
function getDocument(){
var host = "docs";
var fileId;
var exportFormat;
if(host == 'sheets'){
fileId = SpreadsheetApp.getActiveSpreadsheet().getId();
exportFormat = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}else if (host == 'docs') {
fileId = DocumentApp.getActiveDocument().getId();
exportFormat = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
var url = Drive.Files.get(fileId).exportLinks[exportFormat];
Logger.log(url);
var oauthToken = ScriptApp.getOAuthToken();
var content = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + oauthToken
}
}).getBlob();
Logger.log(content.getContentType());
content.setName("TestFile");
MailApp.sendEmail("email#sample.com", "Test", "Test", {attachments: content});
}
Pre-requisite:
You need to enable Advanced Drive Service to get the export links of the file using Drive.Files.get(). This request will return a File Resource where you can get the exportLinks that can be accessed using a key based on the supported export MIME Types
What it does?
Depending on the host set, get the file id and export format based on supported export MIME Types
Get the file resource using Drive.Files.get() and get the export link based on the export format key set in step 1.
Use UrlFetchApp.fetch(url, params) and get the file's blob using HTTPResponse.getBlob()
Output:
(OPTION 2):Create export URL manually using the template url
Sample Code:
function getDocument(){
var host = "docs";
var fileId;
var url;
if(host == 'sheets'){
fileId = SpreadsheetApp.getActiveSpreadsheet().getId();
url = "https://docs.google.com/spreadsheets/export?id="+fileId+"&exportFormat=xlsx"
}else if (host == 'docs') {
fileId = DocumentApp.getActiveDocument().getId();
url = "https://docs.google.com/feeds/download/documents/export/Export?id="+fileId+"&exportFormat=docx";
}
Logger.log(url);
var oauthToken = ScriptApp.getOAuthToken();
var content = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + oauthToken
}
}).getBlob();
Logger.log(content.getContentType());
}
What it does?
Depending on the host set, get the file id and create an export link using this templates:
EXCEL: https://docs.google.com/spreadsheets/export?id=<fileId>&exportFormat=xlsx
WORD: https://docs.google.com/feeds/download/documents/export/Export?id=<fileId>&exportFormat=docx
Use UrlFetchApp.fetch(url, params) and get the file's blob using HTTPResponse.getBlob()
Note:
Based on Quotas for Google Services, Url Fetch Call has a daily quota of 20,000 for Consumer and G Suite free edition, while 100,000 for Google Workspace accounts

Post a file with urlFetchApp to a google web app

I am trying to post any type of file using Google Apps Script to a web app but I am not familiar with doPost.
My code looks like this:
function call(){
var file = getFile('/2.html'); //gets a file from my Google Drive (no point in posting all the code here for that)
var options = {
'method' : 'post',
'payload' : {file : file}
};
var response = UrlFetchApp.fetch('https://script.google.com/macros/s/AKfycbx0aIU_XjOHPXh0P6y2dTMmvGpI6WAuac_Cq5BOGw7nDLRlodT-/exec',options)
Logger.log(response)
This seems to work, although I have read I need to base64 encode a file for it to work properly. That's something I don't really understand.
On the web app side I made a doPost(e) but no matter what I tried to do with 'e' I can't work out what kind of object it is and how to process it. All I want to do really is to save it to Google Drive.
You may be wondering why I am going to these lengths to post the file to disk via a web app when I could save it directly. The reason is I am trying to do the process asynchronously using UrlFetchApp.fetchAll() so I can speed up the process of writing many files to disk at once.
If variable file is of type File, Get Blob from File and base64 encode it:
var options = {
'method' : 'post',
'payload' : Utilities.base64EncodeWebSafe(file.getBlob().getBytes())
};
On receiver web -app, Decode it back:
const doPost = e => {
const file = DriveApp.createFile(
Utilities.newBlob(
Utilities.base64DecodeWebSafe(
e.postData.contents
)
)
)
}
Thanks to #TheMaster. I made some modifications to the code because it resulted in an error because a file name was needed. you'll need your own apps script web app url to go here [APPS SCRIPT WEB APP URL]:
const doPost = e => {
const file = DriveApp.createFile(
Utilities.newBlob(
Utilities.base64DecodeWebSafe(
e.parameter.file
),e.parameter.contentType,e.parameter.fileName
)
)
}
function sendFile(file){
var fileName = file.getName();
var contentType = file.getBlob().getContentType();
var url = "[APPS SCRIPT WEB APP URL]";
var payload = {
fileName: fileName,
contentType : contentType,
file: Utilities.base64EncodeWebSafe(file.getBlob().getBytes())
};
var options = {
method: "POST",
payload: payload,
muteHttpExceptions : true,
};
var res = UrlFetchApp.fetch(url, options).getContentText();
}

Google Spreadsheet Script: UrlFetchApp upload string as file

I saw this SO thread to upload a file using UrlFetchApp.
Also I saw this SO thread to post a string as file in python requests (without having a file or creating a temp file).
I want to send a string as file in Google Spreadsheet Script, and based on the two threads above I think it should be possible to do it without creating a temp file in google drive.
My question is how in Google Spreadsheet Script I can pretend the string is coming from a file and pass it to UrlFetchApp?
Here is the base code I am working on:
var string = 'test';
var url = "https://api.telegram.org/.../sendDocument";
var chat_id = "12345";
var data = {'chat_id': chat_id,
'text':text} ;
var headers = { "Accept":"application/json",
"Content-Type":"application/json"
};
var options = { "method":"POST",
"headers": headers,
"payload" : payload
};
var response = UrlFetchApp.fetch(url, options);
Here is how I got it right to post a string as file:
var url = "https://api.telegram.org/.../sendDocument";
var blob = Utilities.newBlob('Test!', 'text/plain', 'report.txt');
var data = {
'chat_id' : '12345',
'document': blob
};
var options = {
'method' : 'POST',
'payload' : data,
};
UrlFetchApp.fetch(url, options);
Thanks to comment from #Jack Brown and google developer example for posting blobs (example titled: Make a POST request with form data.).