Clientside Javascript --> Serverside Java --> user is served a .doc - java

I am helping someone out with a javascript-based web app (even though I know next to nothing about web development) and we are unsure about the best way to implement a feature we'd like to have.
Basically, the user will be using our tool to view all kinds of boring data in tables, columns, etc. via javascript. We want to implement a feature where the user can click a button or link that then allows the user to download the displayed data in a .doc file.
Our basic idea so far is something like:
call a Java function on the server with the desired data passed in as a String when the link is clicked
generate the .doc file on the server
automatically "open" a link to the file in the client's browser to initiate the download
Is this possible? If so, is it feasible? Or, can you recommend a better solution?
edit: the data does not reside on the server; rather, it is queried from a SQL database

Yep, its possible. Your saviour is the Apache POI library. Its HWPF library will help you generate Microsoft word files using java. The rest is just clever use of HTTP.

Your basic idea sounds a bit Rube-Goldbergesque.
Is the data you want in the document present on the server? If so, then all you need to do is display a plain HTML link with GET parameters that describes the data (i.e. data for customer X from date A to date B). The link will be handled on the server by a Servlet that gets the data and produces the .DOC file as its output to be downloaded by the browser - a very simple one-step process that doesn't even involve any JavaScript.

Passing large amount data as GET/POST around might not be the best idea. You could just pass in the same parameters you used to generate the HTML page earlier. You don't even need to use 3rd party library to generate DOC. You could just generate a plain old HTML file with DOC extension and Word will be happy to open it.

Sounds like Docmosis Java library could help - check out theonline demo since shows it something similar to what you're asking - generating a real doc file from a web site based on selections in the web page. Docmosis can query from databases and run pretty much anywhere.

Related

What is best approach for pdf generation front end angular or back end Java?

I would like to here few expert advise to make this decision.
We have web app angular as UI and spring boot as back end.
requirement is to generate PDF of dashboard which we are showing on UI contains few data table with client side pagenation and also need to add extra header footer in pdf which we are not showing currently on UI. font requirement is given by client and must follow.
Considering following two options--
1. Itext pdf generation in java where we will design pdf similar to UI dashboard.
2. use some js library like jspdf and write dashboard inner html to pdf.
Need to understand what is pros and cons of each approach,which is good out of both?
Since you have client and server side options I suggest it's worth prototyping a typical document in both approaches (if you can spare the time). The reason is that there are advantages to each and it depends really how specifically you want your PDF to be like your html.
Generally I prefer server-side generation because:
you have access to data possibly not on the client (eg what is going into your header and footer maybe)
you can store the document server side (if relevant) then deliver to the client
However you said "font requirement is given by client". If the client is subject to change and you need that reflected automatically in the paginated PDF, then Amedee's suggestion of html to pdf might be your best option.
I hope that helps.
Paul.

How to implement a Java back end to put web form input into a database?

So if you want to take input from an html form and put that into a database, you can have the form's action direct to a php file like example.php. That php file can then manage database calls with like mysqli_connect() and whatnot.
My question is, what is the equivalent of this when you want to use a Java back end instead? Do I have the form's action direct to a .java file? I know how to use Hibernate for databases, but I don't know how to connect the input from the web form to my Java code.
I have no clue what terms to even google for. I tried "java back end tutorial" but that wasn't useful at all. I'd appreciate any simple explanations, or even the correct technologies to look up.
You can develop Web application and Web site with Java. Google it and you will see a lot of tutorials on how to do it.
The initial technology to develop Web apps with Java is Servlet or JSP as said above by user2963623.

GAE how to serve an html file dynamically from the server

I'm working with GAE (Java) in my GWT application.
When my users enter a certain URL I'd like to dynamically create an html page on the server side and serve it to the client.
How can I do this? Using HttpServlet?
I'm quite lost here, do I need to have an html template file on the server side that I dynamically complete and serve to the client?
You should start with the tutorial to learn the basics. You can generate the whole HTML dynamically, but that tends to get awkward. It's better to separate the HTML to a template and fill in the details with the logic implemented in the GAE application.
https://developers.google.com/appengine/docs/java/gettingstarted/
You can use a library like this one https://github.com/alexmuntean/java-url-rewrite . Read the readme to understand more.
You can just take the request and serve anything you want (jsp, jsf, html static). And you can also write gwt code to do actions(effects or ajax for more things. Etc) with the existing html (just add ids to elements) And write another entry point for that page and just include the generated js in your page
I am planning to do a tutorial and POC on how to make a gwt website indexable by google

Read form data from doc or rtf file with java

i have a word document with a form that is filled by users and , after that, it is saved. I would like to access form field data from a Java application in order to automatize some business process.
I have tested Apache POI but it is unabled to access form elements.
Is there some api to access form element data with java?
You can use: Class FFDataBaseAbstractType (org.apache.poi.hwpf.model.types).
The FFData structure specifies form field data for a text box, check box, or drop-down list box.
You may try Aspose.Words for Java which allows you to access form fields and data in Java applications. The API is quite simple and easy to use. The component is a simple Jar file which is easy to deploy with your application. Also note that you do not need to install MS Word in order to use this component. Please see if this might help in your scenario.
Disclosure: I work as developer evangelist at Aspose.

Programatically convert complex Excel file into HTML format

I have a set of complex Excel files (with figures in it) that I want to show in a web browser. So I need to convert them into HTML page first. Since the excel files are very complex, I can not just parse them and generate a HTML table with HTML tags. The current manual solution that works fine is when I use Microsoft Excel software to save the spreadsheet as a HTML page. I want to automate this task in some way since I want to do it progrmatically through Java. Is their any existing solution or a way to do it? Thanks.
EDIT - I was able to create a macro for it but could not figure it out that how can I execute a macro on excel file from a Java program. Does somebody know?
If open office does a good job of the export then you could take a look at the source to see how it does it. OO is a combination of Java and C++ I believe so you might get lucky and find a Java solution.
Otherwise, I would try and use Excel itself to do the export and find some way of calling it programmatically. If you go down this path you'd be better of using a Microsoft stack (C# would be the most similar to Java) as I would expect it to have all the functions you need already defined.
You might look into POI:
http://poi.apache.org/
I think your best bet is to call Excel from Java using JACOB
Creating direct COM calls (which is what you'll be doing from JACOB) is a bit tough, but you'll get the hang of it. I can't imagine that the Excel VBA macro is horribly complicated. Take a look at the sample code (Usage and Documentation) in the JACOB link for what this will look like.
One other thing: Remember to explicitly clear references. JACOB will release COM handles when objects are garbage collected, but if you are doing any sort of high performance work, you will want to close those connections as quickly as possible. We generally write all of our COM code in a series of try/finally statements - the code is messy, but robust.
Try using hypernumbers. (Disclaimer, I'm the CEO)
I ended up using the Scribd API. I uploaded the document to their server through their API in realtime and pasted an iframe with a link in it which is returned by Scribd.

Categories