Permalink
Browse files

Added Convocation PDF generation

  • Loading branch information...
1 parent fec255f commit 665f800be2d3a98803536e1952d686815d2c7948 @tmaurel committed Feb 24, 2010
@@ -2,13 +2,14 @@ package me.hcl.seekin.Internship
import me.hcl.seekin.Formation.*
import grails.converters.JSON
-import grails.converters.XML
-
-import com.itextpdf.text.*
-import com.itextpdf.text.pdf.*
+import org.codehaus.groovy.grails.commons.ApplicationHolder
+import org.springframework.context.i18n.LocaleContextHolder as LCH
+import java.text.DateFormat;
+import java.text.SimpleDateFormat
class ConvocationController {
- def pdfService
+
+ def pdfService
def index = { redirect(action: "list", params: params) }
@@ -137,63 +138,43 @@ class ConvocationController {
}
}
- def pdfGenerate = {
- def convocation = Convocation.get(params.id)
- def internship = convocation?.internship
- def student = internship?.student
-
- def uri = grailsAttributes.getApplicationUri(request) + "/test.pdf"
-
- Document document = new Document(PageSize.A4, 50, 50, 50, 50);
- PdfWriter writer = PdfWriter.getInstance(document, \
- new FileOutputStream("web-app/convocation/test.pdf"));
- document.open();
- document.add(new Paragraph(student?.user?.firstName + " " + student?.user?.lastName));
- document.add(new Paragraph(internship?.subject))
- document.add(new Paragraph(convocation?.date.toString() + " " + convocation.building + " " + convocation.room))
- document.close();
-
- redirect(url: "/seekin/convocation/test.pdf")
- }
-
- def pdfGeneratePlanning = {
- def promotion = Promotion.get(params.id)
- def internships
-
- promotion?.students?.internships.each {
- if(!it.isEmpty()) {
- internships = it.find {
- it2 ->
- it2.millesime == promotion.millesime
- }
- }
- }
-
- def convocations = internships?.convocation
+ def export = {
- def uri = grailsAttributes.getApplicationUri(request) + "/planning.pdf"
-
- Document document = new Document(PageSize.A4, 50, 50, 50, 50);
- PdfWriter writer = PdfWriter.getInstance(document, \
- new FileOutputStream("web-app/convocation/planning.pdf"));
- document.open();
-
- convocations?.each {
- document.add(
- new Paragraph(
- it?.internship?.student?.user?.firstName + " " +
- it?.internship?.student?.user?.lastName));
- }
- document.close();
-
- redirect(url: "/seekin/convocation/planning.pdf")
- }
-
- def showXml = {
- def convocation = Convocation.get(params.id)
-
- render convocation as XML
- }
+ def convocationInstance = Convocation.get(params.id)
+ if (!convocationInstance)
+ {
+ flash.message = "convocation.not.found"
+ flash.args = [params.id]
+ flash.defaultMessage = "Convocation not found with id ${params.id}"
+ redirect(action: "list")
+ }
+ else
+ {
+ def url = grailsApplication.config.grails.serverURL.toString() + "/"
+ def path = ApplicationHolder.getApplication().getParentContext().getServletContext().getRealPath("convocation");
+ def gsp = new File( path,"_pdf.gsp")
+ def locale = LCH.getLocale()
+ def dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
+
+ def model = [:]
+ model.student = convocationInstance.internship.student.user
+ model.companyTutor = convocationInstance.internship.companyTutor.user
+ model.academicTutor = convocationInstance.internship.academicTutor.user
+ model.promotion = Promotion.getCurrentForStudent(convocationInstance.internship.student)
+ model.manager = model.promotion.formation.manager?.user
+ model.date = dateFormat.format(convocationInstance.date)
+ model.time = new SimpleDateFormat("HH:mm").format(convocationInstance.date)
+ model.convocation = convocationInstance
+
+ response.setContentType "application/pdf"
+ response.setHeader "Content-Disposition", "inline;filename=convocation-" +
+ pdfService.removeSpecialCharacters(model.student.firstName) + "-" +
+ pdfService.removeSpecialCharacters(model.student.lastName) + ".pdf"
+
+ pdfService.buildPdf(gsp, model, response, url)
+ return
+ }
+ }
def dataTableDataAsJSON = {
def promotion = Promotion.get(params.promotion)
@@ -222,7 +203,7 @@ class ConvocationController {
building:it.building,
room:it.room,
internship: it.internship?.toString(),
- pdf:[name:"<img src=\"../images/icons/pdf.png\" />", link:g.createLink(controller: 'convocation', action: 'pdfGenerate', id:it.id)],
+ pdf:[name:"<img src=\"../images/icons/pdf.png\" />", link:g.createLink(controller: 'convocation', action: 'export', id:it.id)],
urlID: it.id
]
}
@@ -1,32 +0,0 @@
-package me.hcl.seekin.Util
-
-import org.springframework.context.i18n.LocaleContextHolder as LCH
-import java.text.DateFormat
-import java.text.SimpleDateFormat
-import java.util.Locale
-
-class DateService {
-
- def test() {
- Date.metaClass.formatDate = { ->
-
- def locale = LCH.getLocale()
- def pattern
- if(locale.toString() == "fr") {
- pattern = "dd/MM/yyyy"
- }
- else {
- pattern = "MM/dd/yyyy"
- }
- def formatter = new SimpleDateFormat(pattern, locale);
-
- return formatter.format(delegate)
- }
- }
-
- def formatDate(request, date) {
-
- return "blabla"
- }
-
-}
@@ -0,0 +1,45 @@
+package me.hcl.seekin.Util
+
+import groovy.text.SimpleTemplateEngine
+import org.xhtmlrenderer.pdf.ITextRenderer;
+import javax.xml.parsers.DocumentBuilder
+import javax.xml.parsers.DocumentBuilderFactory
+import org.w3c.dom.Document
+
+
+class PdfService {
+
+ boolean transactional = false
+
+ def buildPdf(file, model, response, url)
+ {
+
+ def reader = file.newReader('UTF-8')
+
+ SimpleTemplateEngine ste = new SimpleTemplateEngine()
+
+ def template = ste.createTemplate( reader )
+ ByteArrayOutputStream temp = new ByteArrayOutputStream()
+ temp << template.make( model )
+ def content = temp.toString()
+
+ InputStream stream = new ByteArrayInputStream(content.getBytes("UTF-8"))
+ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance()
+ documentBuilderFactory.setFeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
+ DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
+ Document doc = builder.parse(stream)
+
+
+ ITextRenderer renderer = new ITextRenderer()
+ renderer.setDocument(doc, url)
+ renderer.layout()
+
+ renderer.createPDF response.outputStream
+
+ }
+
+ def removeSpecialCharacters(str)
+ {
+ return str.replaceAll("[^a-zA-Z]+", "");
+ }
+}
@@ -17,7 +17,7 @@ class YUITagLib {
out << actionSubmit(id: action, action:action, value:message(code: value))
out << """
- <script>
+ <script type="text/javascript">
yuiButton${action} = new YAHOO.widget.Button("${action}");
</script>
"""
@@ -29,15 +29,15 @@ class YUITagLib {
out << """<a id="${action}" href="${createLink(action:"${action}")}">${message(code: value)}</a>"""
out << """
- <script>
+ <script type="text/javascript">
yuiButton${action} = new YAHOO.widget.Button("${action}");
</script>
"""
}
def deleteButton = {
out << YUISubmitbutton(value:"delete", action:"delete")
- out << """<script>
+ out << """<script type="text/javascript">
function submit(p_oEvent) {
if(yuiButtondelete.hasFocus())
{
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
@@ -0,0 +1,17 @@
+package me.hcl.seekin.Util
+
+import grails.test.*
+
+class PdfServiceTests extends GrailsUnitTestCase {
+ protected void setUp() {
+ super.setUp()
+ }
+
+ protected void tearDown() {
+ super.tearDown()
+ }
+
+ void testSomething() {
+
+ }
+}
Oops, something went wrong.

0 comments on commit 665f800

Please sign in to comment.