batch convert images to binary strings - image

I'm trying to batch convert images to binary strings for use in Adobe ExtendScript Panels. The result I'm want to get is a String that looks like this:
\u0089PNG\r\n\x1A\n\x00\x00\x00\rIHDR\x00\x00\x00\x0F\x00\x00\x00\x0F\b\x06\x00\x00\x00;\u00D6\u0095J\x00\x00\x00IIDAT(\u0091c`\x18~ --\u00ED?\x0E\u00BC\u009F\x18\u00CD\r8p\x026\u00C5\x06#\u00EC#\x046 \u00C5\u0099\u0084\u009D\r\x14,\u00C0\u00E3T\u00FC\u00CE&\u00C1\x10\u00BC\u009AIw6\u0092f\u00D2\x02\f\u00E4\x14\"\u00FD\u008B\u00E9l\u0090S(\nm\u00BA\x02\x00\u009Dp\u00B2N\u00F1d\x1D\u00FD\x00\x00\x00\x00IEND\u00AEB`\u0082
But my code generates this:
c289 504e 470d 1a0d 0000 000d 4948 4452
0000 000f 0000 000f 0806 0000 003b c396
c295 4a00 0000 4949 4441 5428 c291 6360
187e 202d 2dc3 ad3f 0ec2 bcc2 9f18 c38d
0d38 7002 36c3 8506 40c3 ac40 0436 20c3
85c2 99c2 84c2 9d0d 142c c380 c3a3 54c3
bcc3 8e26 c381 10c2 bcc2 9a49 7736 c292
66c3 9202 0cc3 a414 22c3 bdc2 8bc3 a96c
c290 5328 0d6d c2ba 0200 c29d 70c2 b24e
c3b1 641d c3bd 0000 0000 4945 4e44 c2ae
4260 c282
What do I have to do to get soemthing like the first string? This is my code (adobe extendscript). If somebody has a solution in a different language I would also take it.
var allImages = loadFiles("*.png");// load files is my own function
if(allImages == null) return;
var folder = allImages[0].parent;
for(var i = 0; i < allImages.length;i++){
var curfile = File (allImages[i]);
curfile.open ('r');
curfile.encoding = 'BINARY';
var str = curfile.read();
curfile.close();
var newfile = new File (folder.fsName + '/' + curfile.name + '.txt');
newfile.open('w');
newfile.encoding = 'UTF-8';
newfile.write(str);
newfile.close();
} // end loop i

Maybe you should the image convert it to Base64.
Here are two threads here on stackoverflow about that option. At both I find the second answer more interesting.
How can you encode to Base64 using Javascript?
JSON encode/decode base64 encode/decode in JavaScript

I just have to write the file content toSource().
var allImages = loadFiles("*.png");// load files is my own function
if(allImages == null) return;
var folder = allImages[0].parent;
for(var i = 0; i < allImages.length;i++){
var curfile = File (allImages[i]);
curfile.open ('r');
curfile.encoding = 'BINARY';
var str = curfile.read();
curfile.close();
var newfile = new File (folder.fsName + '/' + curfile.name + '.txt');
newfile.open('w');
newfile.encoding = 'UTF-8';
newfile.write(str.Source()); // <-- Thats the trick
newfile.close();
} // end loop i
This will result in a file with this content.
(new String("\u0089PNG\r\n\x1A\n\x00\x00\x00\rIHDR\x00\x00\x00\x1E\x00\x00\x00\x19\b\x06\x00\x00\x00&5\u009E\x1A\x00\x00\x00\u008DIDATx\u00DA\u00ED\u00D2\u00BB\r\u00C0 \fE\u00D1\f\u00C9\x00\u00B4H\u00F44\x14)\u00A0\u00A0\u00A1c\x00f`#Go\x01>r$+\u0091\u008B\u00DB\u00D9\x1C\u00C5\u00CA\u00E5\u009C\u00BB%\x02L\x12)\u00AC\u00B0<\\J\u00A11\u00C6,\u00CC\u0088\u00C1?8u\u00CE\u0099j\u00AD'a\u0087\x0F\u00F7\u00DEq\u00C6\u0093\u00B0\u00C3\u0087C\b\x14c<\t;k\u00D8\x18C;Yk\u00C9{?\r3\u009B\u00EF\u00ED\u00C3)\u00A5\u00D5\u00891\u00F3:\u008C/\u00C2\u00C3\u00B30\u00C3\u0084\u00F9\u00F1\u00E1\u00D6\u00DA\u00E9_\u008D\u009D\u00AF\u00C1\u00FC\x14VX\x0E~\x00i\u0096r%\u00B0\u00FF\x1E\x14\x00\x00\x00\x00IEND\u00AEB`\u0082"))

Related

Google appscipt - Exporting multiple PDF with a for loop, using a sheet as a template, unexpected result

I'm trying to automate the pdf export of a sheet ('BL') which is filled depending on a cell value grabbed on a variable list on sheet 'EiBLdata' by 'i' on each loop.
It seems to work... more or less.
Instead of having 1st pdf with 1st value,
2nd pdf with 2nd value,
3rd pdf with 3rd value etc.
I get 1st pdf with 1st value,
2nd pdf with 1st value,
3rd pdf with 2nd value etc.
In the end only the 1st pdf has the right name, there is a shift in all the others and the last value isn't exported.
I'm quite a newbie with JavaScript and I admit there is a lot of copy/paste in my code, adapted to my purpose. I can't find what I'm doing wrong.
function printSelectedRange() {
var nomfeuille = "EiBLData"
var nomBL = "BL"
var cc = SpreadsheetApp.getActiveSpreadsheet();
var feuille = cc.getSheetByName(nomfeuille);
var BL = cc.getSheetByName(nomBL);
var tr = BL.getRange('B2').getValue();
var plage = feuille.getRange('A1:A15').getValues();
var cell0 = feuille.getRange(1,1).getValue();
BL.getRange('B2').setValue(cell0);
for (var i = 1; i <= 16; i++) {
var cell = feuille.getRange(i,1).getValue();
if (cell > 0) {
BL.getRange('B2').setValue(cell)
var sheetName = "BL";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var ssUrl = ss.getUrl();
var sheetId= sheet.getSheetId();
var url = ssUrl.replace(/\/edit.*$/,'')
+ '/export?exportformat=pdf&format=pdf'
+ '&size=A6'
+ '&portrait=false'
+ '&fitw=false'
+ '&scale=4'
+ '&top_margin=0.35'
+ '&bottom_margin=0.00'
+ '&left_margin=0.35'
+ '&right_margin=0.0'
+ '&sheetnames=false'
+ '&printtitle=false'
+ '&pagenum=false'
+ '&gridlines=false'
+ '&fzr=FALSE'
+ '&gid='+sheetId;
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(url, { headers: { 'Authorization': 'Bearer ' + token } });
var pdf = docurl.getAs('application/pdf');
var file = DriveApp.createFile(pdf);
var docId = sheet.getRange('F13').getValue();
var clientName = sheet.getRange('E9').getValue();
var docDate = sheet.getRange('F14').getValue();
var mois = docDate.getMonth()
docDate.setMonth((mois+1) % 12);
var docDateMMYY = docDate.getMonth()+"-"+docDate.getFullYear().toString().substr(-2);
var docName = "BL-"+docId+"-"+clientName+"-"+docDateMMYY ;
var folder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxx");
var finalFile = file.makeCopy(docName,folder);
file.setTrashed(true);
};
};
var cmdes = ss.getSheetByName('cmdes');
var raz = cmdes.getRange('S2:S501').setValue(false);
}
I think I had a good intuition and finally found the reason of my issue:
how-to-pause-app-scripts-until-spreadsheet-finishes-calculation
It looks like the loop runs faster than the spreadsheet calculation.
Sorry for inconvenience...

Spotfire Webplayer crosstable export with title

I Have requirement to export Crosstable data along with Header in Spotfire.
1) First i tried direct excel export with location from temp folder or direct location like C:\Export\Text.xls. these are working perfect in Spotfire client. But in webplayer it is not working. It is throwing Access issue to the folder.
2) Secondly i tried, With help of other forums i developed a code to convert Crosstable to text area through Iron python script and HTML to excel through JAVA script. This is working perfect in webplayer chrome browser. But the issue here is it is working perfectly for the small data. but i have to export around 10 MB. so it hung in both client and webplayer.
Can anyone help me to fix this.
Script 1: Cross table to HTML and then to javascript for download. This script hangs due to size of the data. When i use small data it works perfect in chrome.
from Spotfire.Dxp.Application.Visuals import TablePlot, HtmlTextArea,
CrossTablePlot
ta = visTA.As[HtmlTextArea]()
from System.IO import Path, StreamWriter
from System.Text import StringBuilder
from System.IO import *
tempFilename = MemoryStream();
tp = visDT.As[CrossTablePlot]()
writer = StreamWriter(tempFilename)
tp.ExportText(writer)
tempFilename.Seek(0,SeekOrigin.Begin);
#Build the table
sb = StringBuilder()
#Open the temp file for reading
f = open(tempFilename)
#add some scripting magic from CDN
html = ""
#build the html table
html += " <TABLE id='myTable'>\n"
html += "<THEAD>"
html += " <TR><TH>"
html += "Performance Attribution"
html += " </TH></TR> <TR><TH>"
html += Heading2
html += " </TH></TR> <TR><TH>"
html += Heading6
html += " </TH></TR> <TR><TH>"
html += Heading3
html += " </TH></TR> <TR><TH>"
html += " </TH></TR> <TR><TH><Font Size=3><B>"
html += Heading4
html += "</TD><TD></TD><TD></TD><TD>"
html += Heading5
html += "</TD><TD></TD><TD></TD><TD>"
html += "Attribution Analysis"
html += "</TD><TD></TD></B></Font></TH></TR>"
html += " <TR><TH>"
html += " </TH><TH>".join(f.readline().split("\t")).strip()
html += " </TH></TR>"
html += "</THEAD>\n"
html += "<TBODY>\n"
for line in f:
html += "<TR><TD>"
html += "</TD><TD>".join(line.split("\t")).strip()
html += "</TD></TR>\n"
f.close()
html += "</TBODY>\n"
html += "</TABLE>\n"
ta.HtmlContent = html
Script to Export from Text Area
==============================================================
jQuery.fn.fnExcelReport = function(options)
{
var options = jQuery.extend({
separator: ',',
header: [],
headerSelector: 'th',
columnSelector: 'td',
delivery: 'popup', // popup, value, download
// filename: 'powered_by_sinri.csv', // filename to download
transform_gt_lt: true // make > and < to > and <
},
options);
var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j=0;
//tab = $(this); // id of table
tab = document.getElementById('myTable');
for(j = 0 ; j < tab.rows.length ; j++)
{
tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
//tab_text=tab_text+"</tr>";
}
tab_text=tab_text+"</table>";
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
sa = window.open('data:application/vnd.ms-excel,' +
encodeURIComponent(tab_text));
return (sa);
}
$(document).ready(function () {
$('table').each(function () {
var $table = $(this);
var $button = $("<button type='button'>");
$button.text("Download");
$button.insertBefore($table);
$button.click(function () {
var csv = $table.fnExcelReport({
delivery: 'value'
});
});
});
})
#Script 2: This script is working in client and not in web browsers.
from System.IO import *
from System import Environment, Threading
username = Threading.Thread.CurrentPrincipal.Identity.Name
import clr
clr.AddReference("System.Windows.Forms")
from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
from System.Windows.Forms import SaveFileDialog
from System.Diagnostics import Process, ProcessStartInfo
from Spotfire.Dxp.Application.Visuals import VisualContent
vc=Visuals.As[VisualContent]() #Visuals = Script parameter for Table/Cross
Table visualization
memStream = MemoryStream();
writer =
Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.ExcelXlsDataWriter);
sWriter = StreamWriter(memStream);
#Exporting the data to Memory Stream
vc.ExportText(sWriter); #exports data in tab separated text
sReader = StreamReader(memStream);
memStream.Seek(0, SeekOrigin.Begin);
tempFolder = Path.GetTempPath()
Filenm = "Fixed_Income_Performce_Attribution.csv";
str1='\\'
print str1
newtemp = tempFolder
print newtemp
filename=newtemp+Filenm
print filename
f=open(filename,"w+")
counter=0
j=0
str1=''
f.write("Percent of Total Holdings"+'\n')
f.write(Heading2+'\n')
f.write(Heading3+'\n')
f.write(Heading6+'\n')
f.write('\n'+'\n')
while (sReader.Peek()>=0):
line=[]
counter=counter+1 #counts the number of rows in dataset
a=sReader.ReadLine()
lines=a.split("\t")
for elem in lines:
j=j+1 # counts the number of columns in dataset
#print elem
if str(elem).find(",")<>-1:
elem='"'+elem+'"' # escaping comma already present in string
line.append(elem)
str1 = ','.join(str(e) for e in line)
f.write(str1+'\n')
f.close();
MemoryStream.Dispose(memStream);
sReader.Close()
Process.Start(filename)
Thanks
Venkatesh
For your first query (downloading to C:\Export\Text.xls); when this script runs on the web player, the files will be generated on the node manager machine (since this is where the IP script is executed).
You could output the files to a network/ shared drive, perhaps? You would need to add the network drive path to the allowed path section of Spotfire.Dxp.Worker.Host.exe.config and ensure it can be accessed by the node manager.

How to convert binary data to utf8 string after dart2js

I'm storing my data in the localStorage. Because I store a lot of data, I need to compress it. I do it with GZip. Everything works, but I found out that my convertion from List which the result of GZip to Utf8 is extremely slow. On 500k of data, it can take up to 5 min on a fast computer.
The problem appear mainly during the loading function.
Load:
import 'dart:convert';
import 'package:archive/archive.dart';
import 'package:crypto/crypto.dart';
var base64 = CryptoUtils.base64StringToBytes(window.localStorage[storageName]);
var decompress = new GZipDecoder().decodeBytes(base64);
storage = JSON.decode(new Utf8Decoder().convert(decompress));
Save:
var g = JSON.encode(storage);
List<int> bz2 = new GZipEncoder().encode(new Utf8Encoder().convert(g));
window.localStorage[storageName] = CryptoUtils.bytesToBase64(bz2);
Am I doing something wrong? Is there other way of converting a List to a String in Dart? Also, I do both save and load, so there is no need of utf8.
I tried Ascii, but it throws error for unsupported character. I got kind of working latin1, for which a much higher performance, but I still get some unsupported character.
If UTF-8 is really the bottleneck, and you don't need it, you can just use Dart's native String encoding which is UTF-16.
var compressed = new GZipEncoder().encode(str.codeUnits);
var decompressed = new GZipDecoder().decodeBytes(compressed);
var finalStr = new String.fromCharCodes(decompressed);
This is just a hack and is not the best, but it works and it's giving me good performance for now:
String convertListIntToUtf8(List<int> codeUnits) {
var endIndex = codeUnits.length;
StringBuffer buffer = new StringBuffer();
int i = 0;
while (i < endIndex) {
var c = codeUnits[i];
if (c < 0x80) {
buffer.write(new String.fromCharCode(c));
i += 1;
} else if (c < 0xE0) {
buffer.write(new String.fromCharCode((c << 6) + codeUnits[i+1] - 0x3080));
i += 2;
} else if (c < 0xF0) {
buffer.write(new String.fromCharCode((c << 12) + (codeUnits[i+1] << 6) + codeUnits[i+2] - 0xE2080));
i += 3;
} else {
buffer.write(new String.fromCharCode((c << 18) + (codeUnits[i+1] << 12) + (codeUnits[i+2] << 6) + codeUnits[i+3] - 0x3C82080));
i += 4;
}
if (i >= endIndex) break;
}
return buffer.toString();
}

Transparent getJSON request does not work properly

I have stuck to make it work: after I put 2 getJSON strings ($.getJSON... ...return false) inside base64 function getjson request runs successfully but the rest of encode function does not return anything then (it must return base64 encoded text). How to fix it? Thanks.
Update: without 'return false;' getjson does not send anything, so that I think there would be a solution replacing 'return false;' to return something else, is not it?
Code:
var Base64 = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=|",
// public method for encoding
encode : function (input) {
$.getJSON('http://debug.loc/savedata.php', $("#somedata").serialize(), function(data){});
return false;
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
This is expected - actually, it returns value false, not "nothing".
The line return false; below the call to getJSON() effectively returns (exits) from the function. Returning from a function means that the rest of the code does not get executed.
I understand that you want the function to continue executing, encode given value and return it. For this to happen, simply remove the line return false;.

Print a header on set page using Abc PDF

I have created pdf from HTML page using Abc PDF now my problem is I want to print a table header on next page, but only if table data is display within a another page, if not display header on a different page, any one have idea about how we can do this using Abc pdf.
What you need to do is create the page with some space at the top, then once your document is built in abc PDF loop though the pages and add a header.
The code below is what I use to add a header, the header in this case has three bits a image at the top and two boxes with text in.
Remember the cord in abc pdf are from the bottom right not the top left.
private static Doc AddHeader(Doc theDoc, Core.Property propertyDetails)
{
int theCount = theDoc.PageCount;
int i = 0;
//Image header
for (i = 1; i <= theCount; i++)
{
theDoc.Rect.Width = 590;
theDoc.Rect.Height = 140;
theDoc.Rect.Position(0, 712);
theDoc.PageNumber = i;
//Check Which office to use.
string imagefilePath = HttpContext.Current.Server.MapPath("/images/pdf/pdf-header.png");
Bitmap myBmp = (Bitmap)Bitmap.FromFile(imagefilePath);
theDoc.AddImage(myBmp);
}
//page header boxes.
//Grey header box
theDoc.Rect.String = "20 15 590 50";
theDoc.Rect.Position(13, 672);
System.Drawing.Color colour = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
theDoc.Color.Color = colour;
theDoc.PageNumber = 1;
theDoc.FillRect();
theDoc.Rect.String = "20 15 586 50";
theDoc.Rect.Position(30, 660);
System.Drawing.Color pageoneText = System.Drawing.ColorTranslator.FromHtml("#50474A");
theDoc.Color.Color = pageoneText;
string thePageFont = "Century Gothic";
theDoc.Font = theDoc.AddFont(thePageFont);
theDoc.FontSize = 16;
theDoc.PageNumber = 1;
theDoc.AddText("My Text!!!!!");
theDoc.Rect.String = "20 15 590 50";
theDoc.Rect.Position(13, 630);
System.Drawing.Color greyBox = System.Drawing.ColorTranslator.FromHtml("#468DCB");
theDoc.Color.Color = greyBox;
theDoc.PageNumber = 1;
theDoc.FillRect();
theDoc.Rect.String = "20 15 586 50";
theDoc.Rect.Position(30, 620);
System.Drawing.Color greyText = System.Drawing.ColorTranslator.FromHtml("#ffffff");
theDoc.Color.Color = greyText;
string thePageFontTwo = "Century Gothic";
theDoc.Font = theDoc.AddFont(thePageFontTwo);
theDoc.FontSize = 14;
theDoc.PageNumber = 1;
theDoc.AddText("This is more text");
return theDoc;
}
Then once the pdf file is created just call
var theDoc = new Doc();
/// Your document creation stuff!!!
theDoc = AddHeader(theDoc, propertyDetails);

Resources