package com.example.demo import net.sf.jasperreports.engine.JREmptyDataSource import net.sf.jasperreports.engine.JasperCompileManager import net.sf.jasperreports.engine.JasperExportManager import net.sf.jasperreports.engine.JasperFillManager import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter import net.sf.jasperreports.export.Exporter import net.sf.jasperreports.export.SimpleExporterInput import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication import org.springframework.core.io.ResourceLoader import org.springframework.http.MediaType import org.springframework.util.FileCopyUtils import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMethod import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController import java.io.ByteArrayInputStream import java.io.File import java.nio.file.Files import java.nio.file.Path import javax.servlet.http.HttpServletResponse @SpringBootApplication @RestController class DemoApplication @Autowired constructor( private val resourceLoader: ResourceLoader, ) { @RequestMapping("/report1", method = [RequestMethod.POST, RequestMethod.GET]) fun getReport1( response: HttpServletResponse, @RequestParam(value = "report", required = false) reportForm: String?, ) { val jrxmlString = """ <band height="79" splitType="Stretch"/> """.trimIndent() val reportFromString = (reportForm ?: jrxmlString).toByteArray() val inputStream = ByteArrayInputStream(reportFromString) data class Item ( var name: String, var qty: Int, ) val db = JRBeanCollectionDataSource( mutableListOf( Item("Apple", 1), Item("Microsoft", 3), ) ) val report = JasperCompileManager.compileReport(inputStream) val map = mutableMapOf( "customerName" to "Sambo", "staffName" to "Chea", "date" to "11-20-2020", "cookNumber" to "CK100212", "function" to Function(), ) val print = JasperFillManager.fillReport( report, map, db) response.contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE // val file = JasperExportManager.exportReportToPdf(print) // JasperExportManager.exportReportToHtmlFile(print, "output.html") val exporter = JRDocxExporter() exporter.setExporterInput(SimpleExporterInput(print)) exporter.exporterOutput = SimpleOutputStreamExporterOutput(response.outputStream) exporter.exportReport() // val file = File("output.html").readBytes() // FileCopyUtils.copy(file, response.outputStream) } } class Function { fun sayHello() = "hello, Sambo!" } fun main(args: Array) { runApplication(*args) }