How to add an image to word document and download using grails? - grails

I use the code below to download image directly but I need to put it in word doc and want to download the doc.
response.contentType = "application/octet-stream"
response.setHeader("Content-disposition", "attachment;filename=image.jpg")
response.outputStream << inputstream
response.outputStream.flush()
Can anyone know how to add an image to word document and download using grails?
Any help is appreciated.
Thanks

as suggested by Dónal, Apache POI might be a solution. But you also might want to try the following approach:
Create a word document with an image
save it as xml
create such an xml document in grails and replace the image with your own
deliver it through grails with content-type "application/msword"
at least this works fine for excel files...

I use http://blogs.bytecode.com.au/glen/2010/11/04/creating-word-docx-documents-dynamically-from-grails-or-java.html and http://blog.iprofs.nl/2012/10/22/adding-images-and-layout-to-your-docx4j-generated-word-documents-part-1/ to solve my code so I use docx4j for inserting image into word document using grails.
Thanks

Related

uploading a specific style to paperclip

I know that with modern paperclip I can do
model.source = URI.parse source_url
and that will download the file and handle everything for me.
However, what if I want to provide styles that have already been processed internally.
Something like
model.upload_source 'video/mp4', (URI.parse source_url_mp4)
model.upload_source 'video/webm', (URI.parse source_url_webm)
I'm not finding this documented anywhere.
Have you considered finding the mimetype in your application code after loading a source_url?

How upload text file in Vaadin to textField?

I know how upload and save file. But how to upload the file without saving on disk and display text-data in TextArea or TextField.
When using the vaadin upload you should be able to use a ByteArrayOutputStream instead of a FileOutputStream.
So just use the sample from the book of vaadin but use a ByteArrayOuputStream instead.
When the uploadSucceeded method is called, you take the ByteArrayOutputStream, convert it into a string (Beware of the correct encoding) and set it as the value of your TextArea/Field.

How to export data to excel using mvc without using third party control

I want to export data to excel both in .xls and .xlsx format in asp.net
MVC without using any third party control.
I tried using following code but it doesn't support .xlsx format.
Response.AddHeader("Content-Disposition", "attachment;
filename=test.xlsx");
Response.ContentType = "application/ms-excel";
Response.Write(sw.ToString());
Response.End();
Pls give me solution.
For your lines maybe this work, but I'm not sure about what are you trying to do.
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AppendHeader("Content-Disposition", "attachment; filename=test.xlsx")
Response.Write(sw.ToString())
Response.End()
Besides Open XML SDK 2.0 described by #amurra, you can try OpenExcel. It is excellent library and is quite fast.
A quick and dirty option is to put all your data in a table and write that table only to response stream with the headers you are trying already, like:
Response.AddHeader("Content-Disposition", "attachment; filename=test.xlsx");
Response.ContentType = "application/ms-excel";
//NOW WRITE TABLE
Response.Write("<table><tr><td>SrNo</td></tr><tr><td>1</td></tr></table>");
Response.End();
I've not used this hack but have seen it used in some application. When opening it Excel gives user a warning that the data is not excel format, but opens it successfully.
You should use the Open XML SDK 2.0 to achieve that task since it is the supported Microsoft way of creating Office documents.
The other hacky way is to create a partial view and export the html to Excel. This would give a readonly view to the data since its not in the proper excel format, but works when you don't want to spend the time to write the Open XML.
Also the correct MIMEConentType for .xlsx is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" and for .xls it is "application/vnd.ms-excel"

In Ruby on Rails, how can I convert html to word?

how can I convert html to word
thanks.
I have created a Ruby html to word gem that should help you do just that. You can check it out at https://github.com/nickfrandsen/htmltoword - You simply pass it a html string and it will create a corresponding word docx file.
def show
respond_to do |format|
format.docx do
file = Htmltoword::Document.create params[:docx_html_source], "file_name.docx"
send_file file.path, :disposition => "attachment"
end
end
end
Hope you find it helpful.
I am not aware of any solution which does this, i.e. convert HTML to Word format. If you literally mean that, you will have to parse the HTML document first using something like Nokogiri. If you mean you want to output data persisted in your model objects, there is obviously no need to parse HTML! As far as outputting to Word, I'm afraid it looks as if you will have to directly interface with a running instance of Microsoft Word via OLE!
A quick google search for win32ole ruby word will get you started:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/241606
Good luck!
I agree with CodeJoust that it is better to generate a PDF. However, if you really need to generate a Word document then you can do the following:
If your server is a Windows machine, you can install Office in it and use ruby's OLE binding to generate the Word document into the public folder and then deliver the file in the response.
To use ruby's OLE binding, see the "Programming Ruby" ebook that comes with the one-click ruby installer for Windows. You may have to use custom logic to convert from HTML to Word unless you can find a function in the OLE api of Word to do that.
http://prawn.majesticseacreature.com/
You could allow the user to download a PDF or a .html file, but there aren't any helpful ruby libraries to do that. You're better off generating a 'printable and downloadable' version, without much styling, and/or a pdf version using a library like prawn.
You could always generate a simple .rtf file, I think word'll be pretty happy reading that...

How to export data to pdf in asp.net MVC?

I don't have grid view as I am working on asp.net MVC.
So can I create Response object which writes data into pdf format
Response.AddHeader("Content-Disposition", "attachment; filename=data.pdf");
Response.ContentType = "application/.pdf";
I use these two lines but I don't know in which format, I should write data?
you must use a pdf library like iTextSharp. just do a google search.
I usually generate my pdf reports on the fly using report viewer control (client-side mode).
LOL.
You need to get PDF converter to convert from whichever format you use to PDF. The last I checked there were no fully functional converters for free. Buy it, buddy.

Resources