Using Mirth Connect JavaScript to output a barcode font to PDF - javascript

Mirth Connect uses iTextpdf and barcode 128 is not an included font. I downloaded code128.ttf font, but how can I reference this with javascript in Mirth Connect? Maybe someone has a Mirth channel that does this or something similar?
Another possibility is Mirth Connect has a Document Writer template option which can use HTML/CSS to reference the local font. I tried many variations of HTML/Inline CSS to no avail.

There are a number of barcode image generator libraries for Java. Create your own Java class that does everything you need, deploy to the /custom-lib folder in the Mirth installation and call the class in your channel's source or destination transformer JS step.

I did it by writing the barcode to a temporary file and referencing that in the Document Writer HTML.
Add a JavaScript transformer step to your destination Document Writer:
// Generate your barcode
var barcodeContents = "0123456789 hello"
var code128 = new Packages.com.lowagie.text.pdf.Barcode128();
code128.setCode(barcodeContents);
// Convert to image
var image = code128.createAwtImage(java.awt.Color.BLACK, java.awt.Color.WHITE);
var bufferedImage = new java.awt.image.BufferedImage(image.getWidth(),image.getHeight(), java.awt.image.BufferedImage.TYPE_INT_RGB);
var graphics = bufferedImage.getGraphics();
graphics.drawImage(image, 0, 0, null);
// Write to JPG file
var filename = UUIDGenerator.getUUID().toString() + '.jpg';
javax.imageio.ImageIO.write(bufferedImage, "jpg", new java.io.File("C:\\Temp\\Barcodes\\" + filename));
// Save the filename to use in your Document Writer
$c('BarcodeFilename', filename);
Then just reference the image in your HTML template:
<img src="/temp/Barcodes/${BarcodeFilename}"/>
As a bonus you can add a Javascript Writer destination to delete the image afterwards:
var filename = $c('BarcodeFilename');
FileUtil.delete("C:\\Temp\\Barcodes\\" + filename);

Related

Uploading image file using html and javascript returning fakepath

I am trying to upload image on my website to add it to the database as base64 encoded string. The problem is I can not receive the file path to upload it by java. On the front-end I am using HTML and javascript, here is the HTML tag:
<div class="col-xs-6">
<label>passport image </label><input id="image-input" type="file"
placeholder="file">
</div>
in javascript I added:
document.getElementById("image-input").value;
The path of the image that arrives to me is 'C:\fakepath\IMAGE_NAME'.
I need a way to send the photo or it's path to the java code, how can I do this while I am not using jsf nor spring.
Some browsers have a security feature that prevents JavaScript from
knowing your file's local full path. It makes sense - as a client, you
don't want the server to know your local machine's filesystem.
I use the object FileReader on the input onchange event for your input
file type! This example uses the readAsDataURL function and for that
reason you should have an tag. The FileReader object also has
readAsBinaryString to get the binary data, which can later be used to
create the same file on your server
Example:
var input = document.getElementById("inputFile");
var fReader = new FileReader();
fReader.readAsDataURL(input.files[0]);
fReader.onloadend = function(event){
var img = document.getElementById("yourImgTag");
img.src = event.target.result;
}
try this ! using : replace
var imgpath = document.getElementById("image-input").value;
var newPath = imgpath .replace("C:\\fakepath\\", "")

Using Google Chrome extensions to import/export JSON files?

I'm creating a Google Chrome extension at the moment and I was wondering if it's possible for it to both create JSON files to download (export) and create a button where users can make the extension open and parse JSON files that they have saved in their local file system or on a USB stick (import)?
The parsing of each JSON from the local file system would simply involve reading off each key-value pair and doing something with the data. It would only have to deal with strings, so nothing complicated.
**EDIT: **My question is not a duplicate of this one because I'm not interested in changing the user's download path. All I want is to enable them to, with their consent, download a file to their normal download directory (which Filesaver.js can do). Plus, that post says nothing about importing.
You can fake link to "download" imaginary array MyData or whatever,:
var MyArray = [elem1, elem2, ....];
var _myArray = JSON.stringify(MyArray , null, 4); //indentation in json format, human readable
var vLink = document.createElement('a'),
vBlob = new Blob([_myArray], {type: "octet/stream"}),
vName = 'watever_you_like_to_call_it.json',
vUrl = window.URL.createObjectURL(vBlob);
vLink.setAttribute('href', vUrl);
vLink.setAttribute('download', vName );
vLink.click();
this will export/download your array into json file named as vName variable.
If you wish to import/read file:
create input element (type=file) and make it invisible (here I'm having html element and then adding js listener in script)
<input type="file" id="importOrig" accept=".json" style="display:none"/>
script
importOrig.addEventListener("change", importFun, false);
make button fakeImp (or any element), that you can style as you wish and that will be used as trigger for importing event
fakeImp.onclick = function () {importOrig.click()}
import function (from listener)
function importFun(e) {
var files = e.target.files, reader = new FileReader();
reader.onload = _imp;
reader.readAsText(files[0]);
}
function _imp() {
var _myImportedData = JSON.parse(this.result);
//here is your imported data, and from here you should know what to do with it (save it to some storage, etc.)
......
importOrig.value = ''; //make sure to clear input value after every import
}

save a field value to a file on desktop

I am developing a custom application in "ServiceNow" which requires Javascript and HTML coding. So, I have a field say, "description" on my form. How may I save this field's value to a word document on the desktop?
While JavaScript cannot create a file for you to download by itself, ServiceNow does have a way for you to create one. Creating a Word document is impossible without the use of a MID server and some custom Java code, but if any file type will do you can create an Excel file using an export URL. To test this out, I made a UI Action in a developer instance running Helsinki on the Problem table. I made a list view that contains only the field that I wanted to save, and then used the following code in the UI action:
function startDownload() {
window.open("https://dev13274.service-now.com/problem_list.do?EXCEL&sysparm_query=sys_id%3D" +
g_form.getUniqueValue() + "&sysparm_first_row=1&sysparm_view=download");
}
When the UI action is used, it opens a new tab that will close almost immediately and prompt the user to save or open an Excel file that contains the contents of that single field.
If you want to know more about the different ways you can export data from ServiceNow, check their wiki-page on the subject.
You can use the HTML5 FileSystem API to achieve that
window.requestFileSystem(window.PERSISTENT, 1024*1024, function (fs) {
fs.root.getFile('file.txt', {create: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
var blob = new Blob([description.value], {type: 'text/plain'});
fileWriter.write(blob);
});
});
});
FYI, chrome supports webkitRequestFileSystem.
Alternatively, use a Blob and generate download link
var text = document.getElementById("description").value;
var blob = new Blob([text], {type:'text/plain'});
var fileName = "test.txt";
var downloadLink = document.createElement("a");
downloadLink.download = fileName;
downloadLink.href = window.webkitURL.createObjectURL(textFile);
downloadLink.click();
Javascript protects clients against malicious servers who would want to read files on their computer. For that reason, you cannot read or write a file to the client's computer with javascript UNLESS you use some kind of file upload control that implicitely asks for the user's permission.

How to lift inline files/images from an email using Google App Script?

I would like to gather the name, content type and byes of all inline files in an email using Google App Script. Message object's have a getAttachments() function in App Script however this only returns an array of Gmail Attachments that are not inline.
When I look at the raw content of the email I can see that the data for an inline image is there but parsing it is difficult and I wanted to check it there was any Google utilities that I'm not aware of?
This was a rather fun one to crack, so here goes.
Get your email's raw contents with .getRawContent(). This contains all the content, included base64 encoded attachments like images.
Parse the email content to narrow it down to an image/gif attachment type
Narrow it down more to the base64 encoded string that is your image
Use the Utilities.base64Decode() utility to get your Byte array
Here is what I came up with:
Note: This will just get the first image, I'm sure you can take this concept and adapt to your own needs.
function myFunction() {
var emails = GmailApp.getThreadById(myThreadID).getMessages();
var contents = emails[0].getRawContent();
var firstImageStart = contents.substring(contents.indexOf('Content-Type: image/gif;'), contents.length); //Finds the image/gif type
var name = firstImageStart.substring(firstImageStart.indexOf('name=') + 5, firstImageStart.indexOf('\r\n')); //get name from raw data
var attachmentStringStart = firstImageStart.substring(firstImageStart.indexOf('X-Attachment-Id:'), firstImageStart.length); //Finds where the attachment ID line is
var startOfBase64 = attachmentStringStart.substring(attachmentStringStart.indexOf('\r\n\r\n') + 4, attachmentStringStart.length); //Finds the end of that line and the start of the base64 encoded attachment
var base64String = startOfBase64.substring(0, startOfBase64.indexOf('\r\n--')); //Finds the end of the base64 encoded attachment
var byteArray = Utilities.base64Decode(base64String); //Retrieves a byteArray of the base64 encoded image
var blob = Utilities.newBlob(byteArray, 'image/gif', name.substring(1, name.length - 1)); //Create blob
var newfile = DriveApp.createFile(blob);
DriveApp.getRootFolder().addFile(newfile); //Write new file to drive root
}
This works, and wrote the image to my drive, which shows as a proper image.
I just followed the pattern the raw content has it's data laid out in, you can view this by clicking the Show Original link in gmail on the dropdown beside the reply button.

Is there a way to input a text file from phone's sdcard?

I'm making a Phonegap app that requires a text file input from the user. I've tried using <input type="file"> but it only allows me to choose a file from the gallery or a music file.
Is there any way for the user to browse the phone's sdcard using the native file browsers for a text file on both iOS and Android? Or is making my own file browser the only option available? Is there no simple way to let the user plug in a file to the app?
To put it simply: No
iOS don't have an accessible file system not even for developers. Also none of the iOS devices support SD cards.
In android you can more do it.
To make your app compatible with standard text files by registering it into it's manifest file. Take a look at this example from google.
To access the files in the SD card, if for instance you want to scan for a certain file type, take a look at this guide.
//Find the directory for the SD Card using the API
//Don't hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);
//Set the text
tv.setText(text);

Categories

Resources