Où avez vous codé le contenu du PDF ?
Vous avez mis je pense un template qui est textuel et ne peut être utilisé que pour des formats d’impression textuels (html, xml, vcard, …).
Pour un PDF comme pour XLS, il faut implémenter une méthode Java dans votre objet et qui retourne un document binaire.
Il y a un exemple sur la démo : DemoOrder-PDF / méthode printReceipt, petit résumé :
Dans DemoOrder :
/** Publication: PDF receipt */
public Object printReceipt(PrintTemplate pt) {
try {
pt.setFilename(getDisplay() + "-" + getFieldValue(NUMBER_FIELDNAME) + ".pdf");
return DemoCommon.orderReceipt(this); // Implemented in common class
} catch (Exception e) {
AppLog.error("Unable to publish " + pt.getName(), e, getGrant());
return e.getMessage();
}
}
Et dans DemoCommon, le code qui génère un PDF formatté avec les champs de l’objet “order” :
/**
* Order receipt publication as PDF
* @param ord Order object
*/
public static byte[] orderReceipt(ObjectDB ord) throws IOException, DocumentException {
try (ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream()) {
Document pdf = PDFTool.open(bos);
// Logo
pdf.add(PDFTool.getImageFromResource(ord.getGrant(), "DEMO_LOGO"));
pdf.add(new Paragraph(ord.getGrant().T("DEMO_RECEIPT"), PDFTool.TITLE1));
ObjectField f = ord.getField("demoOrdNumber");
pdf.add(new Paragraph(f.getDisplay() + ": " + f.getValue(), PDFTool.TITLE2));
f = ord.getField("demoOrdDate");
pdf.add(new Paragraph(f.getDisplay() + ": " + ord.getGrant().toFormattedDate(f.getValue())));
f = ord.getField("demoOrdDeliveryDate");
pdf.add(new Paragraph(f.getDisplay() + ": " + ord.getGrant().toFormattedDatetime(f.getValue())));
PdfPTable t = PDFTool.getTable(2, false);
t.addCell(PDFTool.getHeaderCell(ord.getField("demoOrdCliId").getDisplay(), java.awt.Color.LIGHT_GRAY));
t.addCell(PDFTool.getHeaderCell(ord.getField("demoOrdPrdId").getDisplay(), java.awt.Color.LIGHT_GRAY));
PdfPCell c = PDFTool.getCell(null);
f = ord.getField("demoOrdCliId.demoCliCode");
c.addElement(new Paragraph(f.getDisplay() + ": " + f.getValue()));
f = ord.getField("demoOrdCliId.demoCliFirstname");
c.addElement(new Paragraph(f.getDisplay() + ": " + f.getValue()));
f = ord.getField("demoOrdCliId.demoCliLastname");
c.addElement(new Paragraph(f.getDisplay() + ": " + f.getValue()));
c.addElement(new Paragraph("\n"));
f = ord.getField("demoOrdCliId.demoCliAddress1");
c.addElement(new Paragraph(f.getDisplay() + ": " + f.getValue()));
f = ord.getField("demoOrdCliId.demoCliAddress2");
c.addElement(new Paragraph(f.getDisplay() + ": " + f.getValue()));
f = ord.getField("demoOrdCliId.demoCliZipCode");
c.addElement(new Paragraph(f.getDisplay() + ": " + f.getValue()));
f = ord.getField("demoOrdCliId.demoCliCountry");
c.addElement(new Paragraph(f.getDisplay() + ": " + f.getValue()));
t.addCell(c);
c = PDFTool.getCell(null);
f = ord.getField("demoOrdPrdId.demoPrdSupId.demoSupName");
c.addElement(new Paragraph(f.getDisplay() + ": " + f.getValue()));
String v = ord.getField("demoOrdPrdId.demoPrdSupId.demoSupLogo").getValue();
if (!Tool.isEmpty(v)) {
Image i = PDFTool.getImageFromDBDoc(ord.getGrant(), v);
i.scaleAbsoluteWidth(100);
i.setSpacingBefore(10);
i.setSpacingAfter(10);
c.addElement(i);
}
f = ord.getField("demoOrdPrdId.demoPrdReference");
c.addElement(new Paragraph(f.getDisplay() + ": " + f.getValue()));
f = ord.getField("demoOrdPrdId.demoPrdName");
c.addElement(new Paragraph(f.getDisplay() + ": " + f.getValue()));
v = ord.getField("demoOrdPrdId.demoPrdPicture").getValue();
if (!Tool.isEmpty(v)) {
Image i = PDFTool.getImageFromDBDoc(ord.getGrant(), v);
i.scaleAbsoluteWidth(150);
i.setSpacingBefore(10);
i.setSpacingAfter(10);
c.addElement(i);
}
t.addCell(c);
pdf.add(t);
f = ord.getField("demoOrdQuantity");
pdf.add(new Paragraph(f.getDisplay() + ": " + f.getValue(), PDFTool.TITLE1));
f = ord.getField("demoOrdUnitPrice");
pdf.add(new Paragraph(f.getDisplay() + ": " + ord.getGrant().toFormattedFloat(f.getValue(), 10, 2) + CURRENCY, PDFTool.TITLE2));
f = ord.getField("demoOrdTotal");
pdf.add(new Paragraph(f.getDisplay() + ": " + ord.getGrant().toFormattedFloat(f.getValue(), 10, 2) + CURRENCY, PDFTool.TITLE1));
f = ord.getField("demoOrdVAT");
pdf.add(new Paragraph(f.getDisplay() + " (" + ord.getGrant().toFormattedFloat(ord.getGrant().getParameter("DEMO_VAT"), 10, 2) + "%): " + ord.getGrant().toFormattedFloat(f.getValue(), 10, 2) + CURRENCY, PDFTool.TITLE2));
PDFTool.close(pdf);
return bos.toByteArray();
}
}
Les PDFTool de Simplicité sont des helpers qui utilisent les librairies iText.
Mais vous pouvez utiliser d’autres libs comme pdfbox.