I am a programmer but I hate grammar. Here you will find what I did, doing , will do ... Scattered pieces of knowledge that I have deleted from my mind to the trash bin through out my boring daily coding life. I will also report some of my failures in life so dear to me not success, because I have always learnt much only when I fail.
Sunday, May 23, 2021
Sunday, May 16, 2021
Saturday, May 15, 2021
Thursday, May 13, 2021
18 - Generate PDF with Jasperreport and Spring Boot SOLVED
In this tutorial, I show you how to generate a PDF with Jasperreport and Spring Boot .
application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/YOURDATABASE?autoReconnect=true spring.datasource.username=YOURUSERNAME spring.datasource.password=YOURPASSWORD spring.jpa.generate-ddl=true
net.sf.jasperreports jasperreports 6.16.0 org.springframework spring-context-support
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ServiceJpasql001Application { public static void main(String[] args) { SpringApplication.run(ServiceJpasql001Application.class, args); } }
package com.example.demo.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="student") public class Student{ @Id @Column(name="stid") private int stid; @Column(name="name") private String name; @Column(name="programme") private String Programme; public int getStid() { return stid; } public void setStid(int stid) { this.stid = stid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getProgramme() { return Programme; } public void setProgramme(String programme) { Programme = programme; } public Student(int stid, String name, String programme) { super(); this.stid = stid; this.name = name; Programme = programme; } public Student() { super(); // TODO Auto-generated constructor stub } }
package com.example.demo.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import com.example.demo.model.Student; public interface StudentRepository extends JpaRepository{ List findByName(String name); }
package com.example.demo.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.demo.model.Student; import com.example.demo.repository.StudentRepository; @Service public class StudentService { @Autowired private StudentRepository studentRepository; public Student addStudent(Student student) { return studentRepository.save(student); } public void deleteStudent(Student student) { studentRepository.delete(student); } public ListgetStudent() { return studentRepository.findAll(); } public List getStudentByName(String name) { return studentRepository.findByName(name); } }
package com.example.demo.controller; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.model.Student; import com.example.demo.service.StudentService; import net.sf.jasperreports.engine.JRException; 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.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.util.JRLoader; @RestController public class StudentController { @Autowired StudentService studentService; @RequestMapping("/all") public ListgetStudent() { return studentService.getStudent(); } @RequestMapping("/studentByName/{name}") public List getStudentByName(@PathVariable String name) { return studentService.getStudentByName(name); } @RequestMapping("/save") public Student saveStudent(@RequestBody Student student) { return studentService.addStudent(student); } @RequestMapping("/delete") public void deleteStudent(@RequestBody Student student) { studentService.deleteStudent(student); } @RequestMapping("/pdf") public void getReportsinPDF(HttpServletResponse response) throws JRException, IOException { //Compiled report InputStream jasperStream = (InputStream) this.getClass().getResourceAsStream("/StudentList.jasper"); //Adding attribute names Map params = new HashMap<>(); params.put("stid","stid"); params.put("name","name"); params.put("programme","programme"); // Fetching the student from the data database. final JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(studentService.getStudent()); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, source); response.setContentType("application/x-pdf"); response.setHeader("Content-disposition", "inline; filename=StudentList.pdf"); final ServletOutputStream outStream = response.getOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, outStream); } }
Wednesday, May 12, 2021
Tuesday, May 11, 2021
Sunday, May 9, 2021
Saturday, May 8, 2021
Sunday, May 2, 2021
Saturday, May 1, 2021
Subscribe to:
Posts (Atom)