Generate Table/Grid from script in Adobe InDesign - javascript

Documentation doesn't help at all,no Table or Grid is specified...(or I cant find it)
I tried to create a grid from inside InDesign and it shows up as TextFrame,but still I dont understand how to manage it.
The tool I need to do takes a file(CSV/JSON) and generates a Table(or whatever is called in Adobe) from it,but the problem is that I can't find anything about Table generation.

Basically you can make a table from a selected text with the method convertToTable() this way:
var doc = app.activeDocument; // your document
var frame = doc.pages[0].textFrames[0]; // first text frame on first page
frame.texts.everyItem().select(); // select all text inside the frame
var table = app.selection[0].convertToTable(); // convert the selection into a table
Before:
After:
Reference:
Text object
As for the rest... it's need more details about your workflow. JSON and CSV are quite different beasts, it would be different parsing algorithms for each of the formats. Will you copy the contents of the files manually or the script should read all csv or json files from some predefined folder? Or there should be some interface to select a file(s). Or a folder? How it supposed to handle a page size and formatting of the table? Etc...

Related

Tabulator combine multiple tables in a single pdf

I am using http://tabulator.info/ to generate multiple tables on a single HTML document page.
When I am triggering the pdf-download via the button, a pdf document should get generated that includes the tables from the document.
So far, downloading a single table works, but I don't know how I could potentially add more tables to the document before jsPDF finishes.
What I have tried out so far is that I grabbed the lastTable1 = doc.lastAutoTable object inside the documentProcessing function when triggering a download for table1. The plan is to pass it into table2.download() and add it via autoTable: function(doc){doc.autoTable(lastTable1)}.
While I do grab an object with this approach, I can not use it to reconstruct the autotable object (e.g. doc.autoTable(lastTable1) does not produce the same table again).
I have prepared a simple jsfiddle where I generate two tables and a download button. Just to illustrate that the reconstruction of the autotable object does not work, I have added it once more to the doc before creating the pdf.
There is no built in way for tabulator to do this.
But the good news is that it should be easy to add something to handle this yourself.
This Issue shows how you can use jsPDF to merge to PDFs into the same document.
You could use the downloadReady callback built into tabulator to intercept the PDF file created from each table and then combine them using the method outlined in the above issue
var table = new Tabulator("#example-table", {
downloadReady:function(fileContents, blob){
//fileContents - the unencoded contents of the file
//blob - the blob object for the download
mergeWithPreviousPDFs(blob); // call a function based on the previous issue that merges the new PDF with the previous PDF's
return blob; //must return a blob to proceed with the download, return false to abort download
}
});
if you return false from this callback the download will not be triggered, so you can intercept the PDF output on all but the final table to prevent download and then return the combined output from the last table to trigger the download.

Pass an HTML file object from one <input type = file> to another

I want to implement a multiple file uploader in a form wherein I want the files so that the user may sort the priority of the files (I am using draggable and sortable Jquery tool).
Therefore I have added a multiple file input as:
<input type = "file" multiple>
Now when I select some files, it shows say 3 files selected.
But when I select 3 files, I wish to split the file uploader in 3 parts so that the user may set the priority ordering accordingly.
For that I have used the following kind of code:
$('.files').change(function(e){
var filesSelected = e.target.files;
if(filesSelected.length > 1){ //if user selects multiple files, then automatically split the files into multiple divs so that he/she may do the ordering of files
//Here I want to create a list of all the files and implement the draggable and sortable thing.
}
});
The situation is, that I am not able to split the array of objects of FileList and assign each object to another input.
Hope I am clear in my doubt and the question is understandable, as it is the first time I am posting in any such forum.
You cannot set value for <input type="file"> programmatically. It would be a major security flow. Imagine some website automatically uploading arbitrary files from your computer.
You can try to iterate through the selected files and then create a div dynamically with jquery that contains the data from the file like this
$('.files').change(function(e){
var filesSelected = e.target.files;
if(filesSelected.length > 1){
for(var i=0;i<filesSelected.length;i++) { // We iterate through the selected Files
$("#idFromParentElement").append('<div> id=File'+i+'</div'); // Then we create and append the new div to the parent element of our choice
var fileId = 'File'+i;
$("#fileId").data("file",filesSelected[i]); //After that we include a data into the div with the selected file.
}
}
});
From the posts I received and the brief discussions, it is evident that setting file values programmatically will pose security threat and so is not a good option. As far as solving my issue is concerned, best would be to find a way to create multiple divs/fields containing the filenames of the files that are being uploaded and then applying the drag/drop/sort feature in that set of divs. This way the user can easily prioritize the files and while saving the form the array/field containing the priority shall be considered before saving the files data in the database.
Thanks to the responders for their quick response.

How to display 100K XML based DOM data on a JSP client page?

I need some help in understanding what to do next.
I need to write a web based search function to find medical records from an XML file.
The operator can enter either part or all of a patient name and Hit Search on the JSP web page.
The server is suppose to then return a list of possible patient names with the opportunity for the operator to go to next page until a possible patient is found. They can then select the person and view more details.
On the Server side
I have an XML file with about 100,000 records. There are five different types of records in the file. (This is roughly about 20,000 x 5 = 100,000).
I have a java class to source the xml file and create a DOM to traverse the data elements found on the file.
-- XML File Begin
100k - XML file outline
<hospital>
<infant key="infant/0002DC15" diagtype="general entry" mdate="2015-02-18">
<patient>James Holt</patient>
<physician>Michael Cheng</physician>
<physician>David Long</physician>
<diagnosisCode>IDC9</diagnosisCode>
..
</infant>
<injury key="injury/0002IC15" diagtype="general entry" mdate="2015-03-14">
<patient>Sara Lee</patient>
<physician>Michael Cheng</physician>
<diagnosisCode>IEC9</diagnosisCode>
..
</injury>
<terminal key="terminal/00X2IC15" diagtype="terminal entry" mdate="2015-05-14">
<patient>Jason Man</patient>
<physician>John Hoskin</physician>
<diagnosisCode>FEC9</diagnosisCode>
<diagnosisCode>FXC9</diagnosisCode>
..
</terminal>
<aged key= xxxx ... >
...
</aged>
<sickness key= xxxx ... >
...
</sickness>
</hospital>
approx 5 ( )x 20,000 = 100K records.
Key and patient are the only mandatory fields. The rest of the elements are Optional or multiple elements.
-- XML File End
Here is where I need help
Once I have the DOM how do I go forward in letting the client know what was found in the XML file?
Do I create a MAP to hold the element node links and then forward say 50 links at a time to the JSP and then wait to send some more links when the user hits next page?
Is there an automated way of displaying the links, either via a Java Script, Jquery, XSLT or do I just create a table in HTML and place patient links inside the rows? Is there some rendering specific thing I have to do in order to display the data depending on the browser used by client?
Any guidance, tutorials, examples or books I can refer to would be greatly appreciated.
Thank you.
I don't know an automatic way to match the type in jQuery, but you can test the attributes, something like verify if a non optional attribute in the object is present:
// Non optional Infant attribute
if(obj.nonOptionalAttribute) {
// handle Infant object
}
Or you may add an attribute to differentiate the types (something like a String or int attribute to test in your Javascript).
if(obj.type == 'infant') {
// handle Infant object
}
#John.west,
You can try to bind the XML to a list of objects (something like Injure implements MyXmlNodeMapping, Terminal implements MyXmlNodeMapping, Infant implements MyXmlNodeMapping and go on and have a List) to iterate and search by the value at the back end or you can pass this XML file to a Javascript (if you are using jQuery you can use a get or a post defining the result type as XML) and iterate over the objects to find what the user is trying to find...
Your choice may be based on the preference to use processor time in the server side or in the client side...

How to choose where to append data?

How do I choose where to append data with fs.appendFileSync()?
My current code:
var fd = fs.openSync('test.js', 'a');
fs.appendFileSync('test.js', "/**/");
fs.closeSync(fd);
This just appends the data at the end of the file. The options object gives me no way to choose where to append it.
fs.appendFileSync() can only append data to the end of the file. It does not have the option you are asking for.
In fact, the only way (with a normal file system) to insert data into the middle of a file is to rewrite data that is in the file to move it later in the file and then write to the block where you want the new data to go. For this, you would use fs.read() and fs.write() passing it the desired file positions.

Create a textfile from the input of an html-form with the browser (client-side)

I have an html site with a form in it and I want the user to be able to create a text/xml file depending on the input. But I wan't to avoid setting up a webserver only for this task.
Is there a good way, to do that, e.g. with Javascript? I think you can't create files with Javascript, but maybe create a data url and pass the text, so the user can save it to file?
Or is there another way to achieve this simple task without a webserver?
Solved it, somehow. I create a data url data:text/xml;charset=utf-8, followed by the XML.
function createXML() {
var XML = 'data:text/xml;charset=utf-8,<MainNode>';
var elements = document.getElementsByTagName('input'),i;
for (i in elements) {
if (elements[i].checked == true) {
XML += elements[i].value;
}
}
XML += '</MainNode>';
window.open(XML);
}
So the url looks like data:text/xml;charset=utf-8,<MainNode><SubNode>...</SubNode>...</MainNode>
Unfortunately this doesn't work for me on Chromium(Chrome) and on Firefox. It just displays the XML instead of showing a save dialog. But I think that's because of my settings and at least you can save it as a XML-file manually.
I haven't tried this but it should work.
After getting form data, system will call page A.
page A will have javascript that gets query strings and builds the page accordingly.
After finishing page build, user can save current page with following statement in javascript
document.execCommand('SaveAs',true,'file.html');

Categories