我创建了此函数以生成pdf。它可以正常工作,但知道我有一个更困难的脚本,不像功能中的脚本以及尝试使用它时那样简单。它不起作用
@RequestMapping(value = "/generatePDF", method = RequestMethod.GET)
public @ResponseBody void generatePDF(@RequestParam Long idContrat, HttpServletResponse response)
throws SQLException, ClassNotFoundException, JRException {
Connection conn = null;
Contrat contrat = contratRepo.findOne(idContrat);
try {
String dbURL = "jdbc:postgresql://localhost/interim";
String user = "postgres";
String pass = "root";
conn = DriverManager.getConnection(dbURL, user, pass);
BufferedInputStream jasperStream = (BufferedInputStream) this.getClass()
.getResourceAsStream("/test1.jrxml");
JasperDesign design = JRXmlLoader.load(jasperStream);
JRDesignQuery newQuery = new JRDesignQuery();
String sql = "SELECT interim.contrat.con_num_contrat,\r\n" + " interim.contrat.con_date_contrat,\r\n"
+ " interim.contrat.con_lieu_travail,\r\n" + " interim.contrat.con_essai,\r\n"
+ " interim.client.clt_type_client,\r\n" + " interim.client.clt_raison_sociale,\r\n"
+ " interim.client.clt_nom_voie,\r\n" + " interim.agence.ape,\r\n"
+ " interim.agence.libelle,\r\n" + " interim.agence.siret,\r\n"
+ " interim.entreprise.effectif,\r\n" + " interim.entreprise.ville,\r\n"
+ " interim.entreprise.photo,\r\n" + " interim.entreprise.formjuridique\r\n"
+ "FROM interim.contrat\r\n" + " INNER JOIN interim.client ON \r\n"
+ " interim.contrat.id_client = interim.client.id \r\n" + " INNER JOIN interim.agence ON \r\n"
+ " interim.contrat.id_agence = interim.agence.id \r\n"
+ " AND interim.client.id_agence = interim.agence.id \r\n"
+ " AND interim.client.id_agence = interim.agence.id \r\n"
+ " INNER JOIN interim.entreprise ON \r\n"
+ " interim.agence.id_entreprise = interim.entreprise.id\r\n"
+ " WHERE interim.contrat.id =$P{id_contrat}";
newQuery.setText(sql);
design.setQuery(newQuery);
JasperReport report = JasperCompileManager.compileReport(design);
Map<String, Object> parameterMap = new HashMap<>();
// List<Contrat> contrats = contratRepo.findAll();
// JRDataSource jrDataSource = new JRBeanCollectionDataSource(contrats, false);
parameterMap.put("id_contrat", idContrat);
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameterMap, conn);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline: filename = test1.pdf");
final ServletOutputStream outputStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
} catch (JRException e) {
e.printStackTrace();
logger.info("test file test1.jrxml");
} catch (IOException e) {
e.printStackTrace();
}
}