Grails : Generate text file for GSP - grails

I am working with a Grails 2.3.6 application.
I tried many different things to generate PDF, but most of them failed.
Is it possible to generate a text file of the contents of GSP file? Then have a button called EXPORT and when user clicks on that, the text file will download into there system.
Will it be possible to do that by passing the URL of GSP file?

This is quite straightforward. You just need to specify the response type (text/plain) in the render method from your controller. You can have plain text in the gsp file and use the tags where needed.
def textFile = {
response.setHeader('Content-Disposition', 'Attachment;Filename="textFile.txt"')
render view: 'textFile', contentType: 'text/plain'
}
textFile.gsp:
Dear ${name},
This is a text file.
As for pdf, I recommend the amazingly good grails rendering plugin.

Related

Generate Text (.txt) file for Data displayed in GSP

I have a Grails app developed in 2.3.6
There's a GSP file with HTML and CSS elements in it, and that displays data in multiple tables with headers.
I want this data to be saved into a text file and save it. So basically what i want to do is, there will be a Export button in this GSP page, and when user clicks on it, it will download the text file with all the data from that GSP.
What i tried so far?
def textFile = {
response.setHeader('Content-Disposition', 'Attachment;Filename="textFile.txt"')
render view: 'textFile', contentType: 'text/plain'
}
The problem with above is, it saves not just data, but also HTML & CSS elements.
I don't want any HTML or CSS in the text file. Only data from GSP is needed.
Is there a simple way of doing it.
the answer is simple - you need another view withouth the html and css parts.
The rest of your code looks good. But Grails itself does not convert your view, it just sends the content type to the browser and the browser tries to diesplay the data according to the content type.
If you don't want to write a new view (in most cases, writing the new view is dead simple), you could write your own converter (something which strips the HTML and CSS from your file) by creating an afterView-Filter: http://grails.github.io/grails-doc/2.4.0/guide/single.html#filters
Hope that helps

How do I convert a page of HTML to a page on my Rails site?

I added an upload form so people can upload HTML files to my site. How do I parse a file of HTML to create a page of content on the site? Currently, I just need to get the title and body of a file, so I thought a full-blown parser like Nokogiri would be overkill.
#this takes in a <ActionDispatch::Http::UploadedFile>
def import(file)
#code to get title and body?
end
One of many ways to do this..
You can open and read the file from your controller assuming you saved it to an object somewhere.
#content = File.read(#your_saved_object.attachment.file_name)
And then in your view ( in Haml ) :
#content-container= #content

scaffolding withFormat views is not working

I would like to use withFormat within my scaffolding controller and added a scaffolding view 'list.csv.gsp'. But without generating my list.csv.gsp view, grails doesn't use the scaffolding list.csv.gsp view.
After generating de list.csv.gsp view it works fine. But I won't create all these views, because they are all the same.
My scaffolding controller list action ends with the code below:
withFormat {
html {
[${propertyName}List: ${propertyName}List, ${propertyName}Total: ${propertyName}List.getTotalCount(), params: cleanParams(params)]
}
csv {
response.setContentType('text/csv')
response.setHeader('Content-Disposition', "attachment; filename=\${message(code: '${domainClass.propertyName}.label')}.csv")
[${propertyName}List: ${propertyName}List]
}
}
I don't know if you still have a problem with this but the grails docs for with format have this quote:
Grails ignores the HTTP Accept header unless you add a grails.mime.use.accept.header = true setting to your Config.groovy file. In other words, withFormat() will be completely unaffected by the Accept header without that setting.
See withFormat in the grails docs.
On your view you might need to write it generic and put in a common directory and refer your controller template to use it. Maybe make it as a _template and render it.
hope this helps.

How to use a gsp template to create a text file?

I have used the Grails Rendering Plugin in the past with much success in creating PDFs. Throw now I would like to create a simple text file, using a gsp. I loved the ease of using a model to define how to insert information into the template. I realize I don't need to render a text file, but is there a similar way to use a template to just create an ordinary text file?
Example from how to render a jpg using the Grails Render Plugin: (notice the model use)
def bytes = gifRenderingService.render(template: '/images/coupon', model: [serial: 12345])
// Render to a file
new File("coupon.jpg").withOutputStream { outputStream ->
jpegRenderingService.render(template: '/images/coupon', model: [serial: 12345])
}
If there isn't an easy way like the above example, since my information is coming from multiple domain classes should I just create <g> tags in my gsp template that pulls based on conditions needed? If that is the case.. how would I insert a variable into my gsp template from my service?
You might take a look at the grails.gsp.PageRenderer utility class. It allows you to render .gsp templates as a String:
String gspOutput = groovyPageRenderer.render(view: '<your view>.gsp', model: [ modelObj1: ... ])
... or directly to a Writer or OutputStream:
groovyPageRenderer.renderTo(view: '<your view>.gsp', model: [ modelObj1: ... ], <writer or OS>)
Much more detail can be found here: http://mrhaki.blogspot.com/2012/03/grails-goodness-render-gsp-views-and.html
To render the template as text file you should set the content type of the response to text/plain
I'd think you just create your text template as standard, with <g> tags etc..., then call the standard grails render() function on the template with a contentType of 'text/plain'. No plugin necessary?
Add the following code to the GSP file
<%#page contentType="text/plain"%>

How to get markitup editor, using markdown set, to send markdown instead of html

I'm using rails and have a markItUp editor in place, using the Markdown custom set. The only thing I can't figure out is how to get it to submit raw Markdown instead of converted html. I plan on storing both formats, but I haven't found anything capable of parsing html back to markdown. I've customized the markdown set set.js as we didn't want the entire set of formatting options. Here:
myMarkdownSettings = {
previewParserPath: '',
onShiftEnter: {keepDefault:false, openWith:'\n\n'},
markupSet: [
{name:'Bold', key:'B', openWith:'**', closeWith:'**'},
{name:'Italic', key:'I', openWith:'_', closeWith:'_'},
{name:'Bulleted List', openWith:'- ' },
{name:'Link', key:'L', openWith:'[', closeWith:']([![Url:!:http://]!] "[![Title]!]")', placeHolder:'Your text to link here...' }
]
}
And here's the onready code for the page where the markitup elements appear:
$.editable.addInputType('markitup', {
element : $.editable.types.textarea.element,
plugin : function(myMarkdownSettings, original) {
$('textarea', this).markItUp(myMarkdownSettings);
}
});
$('.editable').editable({type : 'markitup'});
This works, but it submits as html. I was trying to use wmd as there's an option for output which maintains the markdown text as is, but haven't been able to get that to fly. Thanks.
Assuming the textarea contains markdown formatted text, you should be able to grab the contents before form submit with $('.editable').text(), and store it in another hidden field, but you'd have to ensure that you get to the contents before markitup transforms them.
If you really just want to store markdown, you'd be better not to use markitup, and just leave it as simple markdown in a text view, then translate it yourself to html for display with one of the libraries available like rdiscount etc.

Resources