Skip to content

Commit

Permalink
Added Convocation PDF generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaurel committed Feb 24, 2010
1 parent fec255f commit 665f800
Show file tree
Hide file tree
Showing 11 changed files with 449 additions and 97 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package me.hcl.seekin.Internship


import me.hcl.seekin.Formation.* import me.hcl.seekin.Formation.*
import grails.converters.JSON import grails.converters.JSON
import grails.converters.XML import org.codehaus.groovy.grails.commons.ApplicationHolder

import org.springframework.context.i18n.LocaleContextHolder as LCH
import com.itextpdf.text.* import java.text.DateFormat;
import com.itextpdf.text.pdf.* import java.text.SimpleDateFormat


class ConvocationController { class ConvocationController {
def pdfService
def pdfService


def index = { redirect(action: "list", params: params) } def index = { redirect(action: "list", params: params) }


Expand Down Expand Up @@ -137,63 +138,43 @@ class ConvocationController {
} }
} }


def pdfGenerate = { def export = {
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 uri = grailsAttributes.getApplicationUri(request) + "/planning.pdf" def convocationInstance = Convocation.get(params.id)

if (!convocationInstance)
Document document = new Document(PageSize.A4, 50, 50, 50, 50); {
PdfWriter writer = PdfWriter.getInstance(document, \ flash.message = "convocation.not.found"
new FileOutputStream("web-app/convocation/planning.pdf")); flash.args = [params.id]
document.open(); flash.defaultMessage = "Convocation not found with id ${params.id}"

redirect(action: "list")
convocations?.each { }
document.add( else
new Paragraph( {
it?.internship?.student?.user?.firstName + " " + def url = grailsApplication.config.grails.serverURL.toString() + "/"
it?.internship?.student?.user?.lastName)); def path = ApplicationHolder.getApplication().getParentContext().getServletContext().getRealPath("convocation");
} def gsp = new File( path,"_pdf.gsp")
document.close(); def locale = LCH.getLocale()

def dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
redirect(url: "/seekin/convocation/planning.pdf")
} def model = [:]

model.student = convocationInstance.internship.student.user
def showXml = { model.companyTutor = convocationInstance.internship.companyTutor.user
def convocation = Convocation.get(params.id) model.academicTutor = convocationInstance.internship.academicTutor.user

model.promotion = Promotion.getCurrentForStudent(convocationInstance.internship.student)
render convocation as XML 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 dataTableDataAsJSON = {
def promotion = Promotion.get(params.promotion) def promotion = Promotion.get(params.promotion)
Expand Down Expand Up @@ -222,7 +203,7 @@ class ConvocationController {
building:it.building, building:it.building,
room:it.room, room:it.room,
internship: it.internship?.toString(), 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 urlID: it.id
] ]
} }
Expand Down
32 changes: 0 additions & 32 deletions grails-app/services/me/hcl/seekin/Util/DateService.groovy

This file was deleted.

45 changes: 45 additions & 0 deletions grails-app/services/me/hcl/seekin/Util/PdfService.groovy
Original file line number Original file line Diff line number Diff line change
@@ -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]+", "");
}
}
6 changes: 3 additions & 3 deletions grails-app/taglib/me/hcl/seekin/Util/YUITagLib.groovy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class YUITagLib {


out << actionSubmit(id: action, action:action, value:message(code: value)) out << actionSubmit(id: action, action:action, value:message(code: value))
out << """ out << """
<script> <script type="text/javascript">
yuiButton${action} = new YAHOO.widget.Button("${action}"); yuiButton${action} = new YAHOO.widget.Button("${action}");
</script> </script>
""" """
Expand All @@ -29,15 +29,15 @@ class YUITagLib {


out << """<a id="${action}" href="${createLink(action:"${action}")}">${message(code: value)}</a>""" out << """<a id="${action}" href="${createLink(action:"${action}")}">${message(code: value)}</a>"""
out << """ out << """
<script> <script type="text/javascript">
yuiButton${action} = new YAHOO.widget.Button("${action}"); yuiButton${action} = new YAHOO.widget.Button("${action}");
</script> </script>
""" """
} }


def deleteButton = { def deleteButton = {
out << YUISubmitbutton(value:"delete", action:"delete") out << YUISubmitbutton(value:"delete", action:"delete")
out << """<script> out << """<script type="text/javascript">
function submit(p_oEvent) { function submit(p_oEvent) {
if(yuiButtondelete.hasFocus()) if(yuiButtondelete.hasFocus())
{ {
Expand Down
Binary file added lib/core-renderer.jar
Binary file not shown.
Binary file added lib/iText-2.0.8.jar
Binary file not shown.
Binary file removed lib/iText-5.0.1.jar
Binary file not shown.
17 changes: 17 additions & 0 deletions test/unit/me/hcl/seekin/Util/PdfServiceTests.groovy
Original file line number Original file line Diff line number Diff line change
@@ -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() {

}
}
Loading

0 comments on commit 665f800

Please sign in to comment.