Par conséquent, j'ai décidé de le faire moi même avec une librairie :
org.apache.pdfbox
Dans le fichier BuildConfig.groovy, j'ai ajouté la dépendance :
dependency {
...
compile "org.apache.pdfbox:pdfbox:1.8.9"
...
}
Voici le code dans mon controlleur :
class AppRetailController {
...
def renderFormPDF(){
List<App> apps = new ArrayList<>()
String title = ""
String param = params.get("serverSelect")
...
Fill the list apps
...
title= "Liste de toutes les applications sur " + param
title += " (" + apps.size() + ")"
// Create all needed for Pdf document
PDDocument document = new PDDocument();
PDPage page = new PDPage();
PDPageContentStream contentStream;
// Here I want 30 rows by page
int pageNumber = apps.size()/30 + 1
log.info("Page number :" + pageNumber)
int countApp = 0
// for each page I draw table with datas
for (int i=1; i<=pageNumber; i++) {
log.info("countApp :" + countApp)
page = new PDPage();
contentStream = new PDPageContentStream(document, page);
int max = countApp + 30
if (max > apps.size()) {
max = apps.size() - 1
}
drawTable(page, contentStream, 700, 100, apps[countApp..max], title);
countApp = countApp + 30 + 1
contentStream.close();
document.addPage(page);
}
document.save("report.pdf");
document.close();
render( file:new File("report.pdf"), fileName: "report.pdf")
}
/**
* @param page
* @param contentStream
* @param y the y-coordinate of the first row
* @param margin the padding on left and right of table
* @param content a 2d array containing the table data
* @throws IOException
*/
def drawTable(PDPage page, PDPageContentStream contentStream,
float y, float margin,
List<App> apps, String title) throws IOException {
final int rows = apps.size() + 1;
final int cols = 3;
final float rowHeight = 20f;
final float tableWidth = page.findMediaBox().getWidth()-(2*margin);
final float tableHeight = rowHeight * rows;
final float colWidth = tableWidth/(float)cols;
final float cellMargin=5f;
//draw the rows
float nexty = y ;
for (int i = 0; i <= rows; i++) {
contentStream.drawLine(margin,nexty,(float)(margin+tableWidth),nexty);
nexty-= rowHeight;
}
//draw the columns
float nextx = margin;
for (int i = 0; i <= cols; i++) {
contentStream.drawLine(nextx,y,nextx,(float)(y-tableHeight));
nextx += colWidth;
}
//now add the text
contentStream.setFont(PDType1Font.HELVETICA_BOLD,14);
contentStream.beginText();
contentStream.moveTextPositionByAmount((float)margin+cellMargin+10,(float)(y+20));
contentStream.drawString(title);
contentStream.endText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD,12);
float textx = margin+cellMargin;
float texty = y-15;
//Define colunm title
contentStream.beginText();
contentStream.moveTextPositionByAmount(textx,texty);
contentStream.drawString("Nom");
contentStream.endText();
textx += colWidth;
contentStream.beginText();
contentStream.moveTextPositionByAmount(textx,texty);
contentStream.drawString("Description");
contentStream.endText();
textx += colWidth;
contentStream.beginText();
contentStream.moveTextPositionByAmount(textx,texty);
contentStream.drawString("Chemin dans ARENA");
contentStream.endText();
textx += colWidth;
texty-=rowHeight;
textx = margin+cellMargin;
contentStream.setFont(PDType1Font.HELVETICA,11);
apps.each {
contentStream.beginText();
contentStream.moveTextPositionByAmount(textx,texty);
contentStream.drawString(it.name);
contentStream.endText();
textx += colWidth;
contentStream.beginText();
contentStream.moveTextPositionByAmount(textx,texty);
String desc = it.description
if (desc.equals("EMPTY")) {
desc = ""
}
contentStream.drawString(desc);
contentStream.endText();
textx += colWidth;
contentStream.beginText();
contentStream.moveTextPositionByAmount(textx,texty);
String path = it.arenaPath
if (path == null) {
path = ""
}
contentStream.drawString(path);
contentStream.endText();
textx += colWidth;
texty-=rowHeight;
textx = margin+cellMargin;
}
}
}
En conclusion, on peut dire que ce n'est pas la meilleur solution car il faut tout faire à la main et à mon avis il vaut mieux regarder des solutions telles que le plugin rendering.Par contre, ça fait le job et lorsque vous êtes pressé par le temps, cela fonctionne.
Un grand merci à ce blog pour l'utilisation de la librairie : http://fahdshariff.blogspot.fr/2010/10/creating-tables-with-pdfbox.html
Hope this help,
Voir aussi : http://grails.github.io/grails-doc/2.5.x/ref/Controllers/render.html
Aucun commentaire:
Enregistrer un commentaire