Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How to create PDF blob from bytes, string, data uri, html

2,209 views
Skip to first unread message

Alan Wells

unread,
Mar 1, 2020, 8:51:46 PM3/1/20
to Google Apps Script Community
Is there any way to convert an array of bytes, a string, a data uri or html into a JPEG blob?  I've tried converting a JPEG blob to a string or array of bytes and back again, and haven't had any success.


function setImage(po) {
try{
 
var image,sh,ss;
 
 
/*
    po.blobSource - a blob that is an image file type
    po.column - the column to set the image in
    po.functionName - the name of the function to assign to the image
    po.row - the row to set the image in
    po.shName - the name of the sheet tab
  */

  ss
= SpreadsheetApp.getActiveSpreadsheet();//This code is bound to a Sheet
  sh
= ss.getSheetByName(po.shName);
 
  image
= sh.insertImage(po.blobSource, po.column, po.row);//Insert an image and return the image
 
  image
.assignScript(po.functionName);//Assign an Apps Script function to the image
 
 
return true;
}catch(e){
 
Logger.log('Error: ' + e.message)
 
Logger.log('stack: ' + e.stack)
 
return false;
}
}

function testRun() {
 
var blobSrc,contentType,file,files;
 
 
/*
    In a Sheet click Insert and Drawing
    Draw a button
    Save the button to your computer drive
    From Google Drive click "New" and File Upload and upload the file and give it a name
    Get the image file in code as shown in this function
    Convert the image file to a blob
  */

 
  files
= DriveApp.searchFiles('mimeType contains "jpeg" and title contains "Orange Diagnal"');
 
 
if (files.hasNext()) {
   file
= files.next();
 
}
 
  contentType
= 'image/jpeg';
 
 
if (file) {
    blobSrc
= file.getAs(contentType);
 
}
 
 
var blobStr = blobSrc.getDataAsString();
 
 
  blobStr
= Utilities.base64Encode(blobStr);
 
 
//Logger.log(Array.isArray(bytes))
 
//DriveApp.createFile('bytes of orange', blobStr, MimeType.PLAIN_TEXT)
 
//Logger.log('blobStr: ' + blobStr)
 
//Logger.log(blobSrc.getContentType());  

  blobStr
= Utilities.base64Decode(blobStr);
  blobSrc
= Utilities.newBlob(blobStr, contentType).getAs(contentType);

 
Logger.log(blobSrc.getContentType())
 
  setImage
({shName:'Update File',blobSource:blobSrc,row:37,column:1,functionName:'myFunction'});
 
}




Logger.log([Error: Service error: Spreadsheets, []]) [0 seconds]
[20-03-01 20:45:26:132 EST] Logger.log([stack:     at GS_SetUp:36 (setImage)
    at GS_SetUp:88 (testRun)
, []]) [0 seconds]


 

Alex

unread,
Mar 6, 2020, 9:44:54 AM3/6/20
to google-apps-sc...@googlegroups.com
Hi!

BlobSource is an interface for objects that can export their data as a Blob.

You have add content-type and name

/**
 * @param {GoogleAppsScript.Base.BlobSource} blobSource
 * @return {{
 *   base64: string,
 *   name: string,
 *   contentType: string
 * }}
 */

function blobSourceToStringSource_(blobSource) {
 
return {
    base64
: Utilities.base64Encode(blobSource.getBytes()),
    name
: blobSource.getName(),
    contentType
: blobSource.getContentType(),
 
};
}


/**
 * @param {{
 *   base64: string,
 *   name: string,
 *   contentType: string
 * }} stringSource
 * @return {GoogleAppsScript.Base.BlobSource}
 */

function stringSourceToBlobSource_(stringSource) {
 
return Utilities.newBlob(Utilities.base64Decode(stringSource))
   
.setName(stringSource.name || '')
   
.setContentType(stringSource.contentType || '');
}

The sample

/**
 *
 */

function test_insertImage() {
 
var file = DriveApp.searchFiles('mimeType="image/jpeg"').next();
 
var blobSource = file.getBlob();


 
var name = blobSource.getName();
 
var contentType = blobSource.getContentType();


 
var digestedString = blobSourceToStringSource_(blobSource);


 
var digestedBlob = stringSourceToBlobSource_(digestedString);
  digestedBlob
.setName(name);
  digestedBlob
.setContentType(contentType);


 
var sheet = SpreadsheetApp.openById(
   
'1XWadinEE5OauB2XpYGYDxJRSYAPiP7MDjeObzewKdzY'
 
).getSheets()[0];
  insertImage
({ sheet: sheet, blobSource: digestedBlob, column: 1, row: 1 });
}


Best, Alex.
Reply all
Reply to author
Forward
0 new messages