Bonjour les gars j'essaie de générer un PDF avec jasper report en utilisant java.
J'ai écris le code lorsque j’exécute le code sans paramètre les données apparaissent sur le pdf.
Lorsque je fais le code avec un paramètre il me génère un document vide.
Voici le code que j'ai écris
1 Le fichier .jrxml

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.4.final using JasperReports Library version 6.0.4  -->
<!-- 2017-01-21T16:58:14 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="EtudiantParClasse" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4896d099-e24a-4f82-ab9d-b814be689eb7">
	<property name="com.jaspersoft.studio.data.defaultdataadapter" value="connectEcole"/>
	<parameter name="Parameter1" class="java.lang.Integer"/>
	<queryString language="SQL">
		<![CDATA[select*from etudiant,classe where etudiant.id_classe=classe.id
and classe.id= $P{Parameter1}]]>
	</queryString>
	<field name="id_etudiant" class="java.lang.Integer">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="nom" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="date_naissance" class="java.sql.Date">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="id_classe" class="java.lang.Integer">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="matricule" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="id" class="java.lang.Integer">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<field name="libelle" class="java.lang.String">
		<fieldDescription><![CDATA[]]></fieldDescription>
	</field>
	<group name="id_etudiant">
		<groupExpression><![CDATA[$F{id_etudiant}]]></groupExpression>
	</group>
	<group name="nom">
		<groupExpression><![CDATA[$F{nom}]]></groupExpression>
	</group>
	<group name="date_naissance">
		<groupExpression><![CDATA[$F{date_naissance}]]></groupExpression>
	</group>
	<group name="id_classe">
		<groupExpression><![CDATA[$F{id_classe}]]></groupExpression>
	</group>
	<group name="matricule">
		<groupExpression><![CDATA[$F{matricule}]]></groupExpression>
	</group>
	<group name="id">
		<groupExpression><![CDATA[$F{id}]]></groupExpression>
	</group>
	<group name="libelle">
		<groupExpression><![CDATA[$F{libelle}]]></groupExpression>
	</group>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="79" splitType="Stretch"/>
	</title>
	<pageHeader>
		<band height="35" splitType="Stretch"/>
	</pageHeader>
	<columnHeader>
		<band height="61" splitType="Stretch">
			<staticText>
				<reportElement x="200" y="30" width="100" height="30" uuid="05a741da-e938-4853-b6ec-c4e528474b96"/>
				<text><![CDATA[nom]]></text>
			</staticText>
			<staticText>
				<reportElement x="100" y="31" width="100" height="30" uuid="87d27eba-a908-467c-9de3-a58570a3b77d"/>
				<text><![CDATA[matricule]]></text>
			</staticText>
			<staticText>
				<reportElement x="300" y="30" width="100" height="30" uuid="c377eda7-aae3-4a61-b79e-30c705f178df"/>
				<text><![CDATA[date_naissance]]></text>
			</staticText>
			<staticText>
				<reportElement x="400" y="31" width="100" height="30" uuid="c1792033-8870-42c6-8ab8-e1f8892b14c7"/>
				<text><![CDATA[libelle]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="125" splitType="Stretch">
			<textField>
				<reportElement x="200" y="0" width="100" height="30" uuid="7c26f7d4-8753-4d23-b4ad-144b6532cfc0"/>
				<textFieldExpression><![CDATA[$F{nom}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="100" y="0" width="100" height="30" uuid="d76cd88b-eb48-4ec9-bab1-09d73192710d"/>
				<textFieldExpression><![CDATA[$F{matricule}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="300" y="0" width="100" height="30" uuid="b339901d-0116-44af-ae09-09cae8bafb34"/>
				<textFieldExpression><![CDATA[$F{date_naissance}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="400" y="0" width="100" height="30" uuid="8db8bfb0-56ac-42f7-876e-07b087060b01"/>
				<textFieldExpression><![CDATA[$F{libelle}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<columnFooter>
		<band height="45" splitType="Stretch"/>
	</columnFooter>
	<pageFooter>
		<band height="54" splitType="Stretch"/>
	</pageFooter>
	<summary>
		<band height="42" splitType="Stretch"/>
	</summary>
</jasperReport>
Le code java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Pdfjasper;
 
import com.mysql.jdbc.Driver;
import infrastructure.Globales;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
 
/**
 *
 * @author PEFOF
 */
public class GenerateEudiantClasseParametre {
 
    private String cheminLocal;
 
    public String GetJasperDocumentEtudiantClasseParamPDF(Integer idClasse) throws FileNotFoundException {
        cheminLocal = Globales.PATH_JASPER_REPORT;
        // - Paramètres de connexion à la base de données
        String url = "jdbc:mysql://localhost/ecole";
        String login = "root";
        String password = "admin";
        Connection connection = null;
 
        try {
            // - Connexion à la base
            Driver monDriver = new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(monDriver);
            connection = DriverManager.getConnection(url, login, password);
 
            // - Chargement et compilation du rapport
            JasperDesign jasperDesign = JRXmlLoader.load(Globales.CheminDossierJasper4);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
 
            // - Paramètres à envoyer au rapport
 
            HashMap parameters = new HashMap();
 
            parameters.put("idClasse",idClasse.intValue());
 
            // - Execution du rapport
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
            Date aujourdhui = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
            File documentPath = new File(cheminLocal + sdf.format(aujourdhui));
 
            if (!documentPath.exists()) {
                documentPath.mkdir();
            }
            cheminLocal += sdf.format(aujourdhui);
            sdf = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
            cheminLocal += "\\" + sdf.format(aujourdhui) + ".pdf";
            OutputStream outputStream = new FileOutputStream(new File(cheminLocal));
 
 
            // - Création du rapport au format PDF
            JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);
        } catch (JRException e) {
 
            e.printStackTrace();
        } catch (SQLException e) {
 
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
 
                e.printStackTrace();
            }
        }
        return cheminLocal;
 
    }
}
Le service rest en java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@GET
    @Path("/GetJasperDocumentEtudiantClasseParamPDF")
    @Consumes("application/json")
    @Produces("application/pdf")
    public Response GetJasperDocumentEtudiantClasseParamPDF(@QueryParam("id") Integer id) throws IOException, ParseException {
 
        String path = "";
        GenerateEudiantClasseParametre generateEudiantClasseParametre = new GenerateEudiantClasseParametre();
 
        path = generateEudiantClasseParametre.GetJasperDocumentEtudiantClasseParamPDF(id);
 
        File file = new File(path);
 
        Response.ResponseBuilder response = Response.ok((Object) file);
        response.header("Content-Disposition", "attachment; filename=Bon_Livraison.pdf");
 
        return response.build();
    }