Jasper Report ฉบับทำงาน 1/3

Phai Panda
3 min readJul 19, 2023

--

สำหรับเพื่อนๆที่ไม่เคยจับหรือเขียน Jasper Report มาก่อน หรือสาย Java มองหาไลบรารี่สักตัวเพื่อสร้างรายงาน PDF, Jasper Report นั้นยังคงเป็นที่หนึ่ง

Jasper Report ฉบับทำงานนี้ผมจะเสนอในมุมมองของนักพัฒนาที่จับโปรเจกต์นี้จากที่มีอยู่ก่อนแล้ว (พัฒนาต่อ) ว่าเขาได้ใส่ความคิดและมุมมองใดบ้าง อย่างไรก็ตามเพื่อให้มือใหม่สามารถอ่านและทำความเข้าใจได้ไม่ยาก ผมจะเริ่มต้นให้พวกเขาด้วยแต่จะไม่ลงรายละเอียดมากเกินไป

โครงสร้าง

Jasper Report ดั่งเดิมก็คือไฟล์ .jrxml หรือก็คือไฟล์ XML ที่มีการแบ่งโครงสร้างออกเป็นส่วนต่างๆ เรียกว่า Band มีอยู่หลายประเภท ตามลำดับต่อไปนี้

  • Title
  • Page Header
  • Column Header
  • Detail
  • Column Footer
  • Page Footer
  • Summary

รายละเอียดของ Bands ที่นี่

Elements

สิ่งนี้ก็คือ graphical object พื้นฐานที่มีส่วนในการแสดงข้อมูล เช่น ข้อความ รูปภาพ ซึ่งเราจะนำมันไปเขียนไว้ในส่วนที่เรียกว่า Band นั่นเอง ที่ใช้บ่อยได้แก่

  • Static text
  • Text field
  • Image
  • Frame
  • Subreport

รายละเอียดของ Elements ที่นี่

Tools

เครื่องมือหลักที่ใช้ ขอแค่ 2 อย่าง

  1. IntelliJ IDEA รุ่น community หรือจ้าวอื่นก็ได้ที่ใช้เขียน Spring Boot
  2. Jaspersoft Studio รุ่น community (CE) ที่นี่

เพื่อนๆนักพัฒนาทราบอยู่แล้วว่าสามารถเขียนโค้ดโดยไม่ใช้ IDE ก็ได้ แต่ ​IDE ก็ทุ่นแรงได้เยอะกว่าทั้งในแง่ของเวลาและลดความผิดพลาด

Init Project

ใช้ start.spring.io สร้างโปรเจกต์ให้ ตามนี้

เปิดด้วย IntelliJ IDEA เขียน controller แล้วลองเรียก

create report controller for test spring web

ผล

call generate method successful

Basic Generate PDF Report

ในที่นี้เราจะสร้าง PDF report แบบง่ายที่สุดกันก่อน โดยนำเข้าไลบรารี่ของ Jasper Report เพื่ออ่านไฟล์ .jrxml แปลงเป็น .pdf ดังรูป

ขอขอบคุณ คุณจิตกร ที่มา https://www.jittagornp.me/blog/how-to-create-jasper-report/

นำเข้าไลบรารี่ Jasper Report จาก Maven ที่นี่

<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.20.5</version>
</dependency>

หรือจะโหลด .jar ก็ได้ ที่นี่

โค้ดที่ controller เพิ่มไปก่อนว่า

package com.example.pros.jasperreportdemo;

import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.*;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@Slf4j
@RestController
public class ReportController {
@GetMapping(value = "/", produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> generate() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
File file;
try {
file = ResourceUtils.getFile("classpath:reports/hello.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(file.getAbsolutePath());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<>());
JasperExportManager.exportReportToPdfStream(jasperPrint, out);
} catch (IOException | JRException e) {
log.error(e.getMessage());
}
return ResponseEntity.ok(out.toByteArray());
}
}

เมื่อ classpath:reports/hello.jrxml คือไฟล์ชื่อ hello.jrxml ที่ถูกสร้างไว้ที่ src/main/resources/reports/hello.jrxml

วิธีการสร้างไฟล์ hello.jrxml

ให้เพื่อนๆเปิด Jaspersoft Studio เลือกไปที่เมนู File > Open Projects from File System…

เลือกไปที่ src/main/resources ของโปรเจกต์ของเรา

find and select resources folder

สร้าง folder (หรือไม่สร้างก็ได้ — ตามใจ) ชื่อ reports ไว้ภายใน resources folder

create reports from resources folder

เลือก Blank A4 แล้วตั้งชื่อ hello.jrxml

under reports folder, type to hello.jrxml

ยังไม่เลือก data adapter ใดๆ

no select data adapter

รันโปรเจกต์ใหม่อีกครั้ง

ผล

call generate method get PDF report successful

กำหนด Fonts

เพื่อให้ PDF สามารถแสดงตัวอักษรภาษาไทยได้ ทั้งในขณะที่ preview ด้วย Jaspersoft Studio หรือ generate จากโปรเจกต์ สมมติต้องการ Sarabun New font

ดาวน์โหลด Sarabun New ที่นี่

บอกกับ Jaspersoft Studio ให้รู้จัก (ไว้ preview) ค้นหาเมนู Settings…

ค้นหาด้วยคำว่า fonts มองหากลุ่มของ Jaspersoft Studio ดังรูป กดปุ่มเพิ่ม แล้ว browse ไปยังตัว font นามสกุล .ttf ตามชนิดของมัน (normal, bold, italic and bold italic)

ตั้งชื่อ font และ browse ตามชนิด

สำหรับ PDF ก็เลือกเป็น identity-H encoding

ตัวอย่างจากโปรเจกต์อื่น

เกือบลืม!

ตัว fonts ทั้งหมดควรอยู่ในโปรเจกต์เพื่อให้โปรเจกต์ใช้อ้างอิงได้ด้วย แนะนำว่าให้ย้ายไปไว้ใน resources folder เป็นว่า resources/fonts ก็ได้ แล้วจึงให้ Jaspersoft Studio นั้น browse ไปหาอีกที

ที่ Jaspersoft Studio ให้ลาก Static text element มาไว้ใน band ชื่อ Detail 1 เลือก font เป็น Sarabun New ตามที่ได้ตั้งชื่อไว้

ทดสอบ preview

Jaspersoft Studio can preview PDF Thai font

อย่าลืมบันทึกการเปลี่ยนแปลงทุกครั้งที่แก้ไข report ด้วยนะ

สำหรับโปรเจกต์ให้สามารถแสดง Sarabun New ได้ โปรดติดตาม 2/3 ครับ

อ้างอิง

ออก report เป็น PDF ด้วย Jasper Report ที่นี่

--

--