124 lines
5.0 KiB
Kotlin
124 lines
5.0 KiB
Kotlin
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 = """
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!-- Created with Jaspersoft Studio version 6.16.0.final using JasperReports Library version 6.16.0-48579d909b7943b64690c65c71e07e0b80981928 -->
|
|
<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="Blank_Letter" pageWidth="226" pageHeight="595" columnWidth="186" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3f96121c-c5f3-482b-b107-f57a057aada0">
|
|
<queryString>
|
|
<![CDATA[]]>
|
|
</queryString>
|
|
<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"/>
|
|
</columnHeader>
|
|
<detail>
|
|
<band height="125" splitType="Stretch">
|
|
<staticText>
|
|
<reportElement x="0" y="10" width="180" height="100" uuid="495273c7-a6de-42c9-ad0c-97e50e27b43f"/>
|
|
<textElement textAlignment="Center">
|
|
<font fontName="Kh Content" size="14" pdfEncoding=""/>
|
|
</textElement>
|
|
<text><![CDATA[Hello World - សួស្តីពិភពលោក]]></text>
|
|
</staticText>
|
|
</band>
|
|
</detail>
|
|
<columnFooter>
|
|
<band height="45" splitType="Stretch"/>
|
|
</columnFooter>
|
|
<pageFooter>
|
|
<band height="54" splitType="Stretch"/>
|
|
</pageFooter>
|
|
<summary>
|
|
<band height="42" splitType="Stretch"/>
|
|
</summary>
|
|
</jasperReport>
|
|
""".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<String, Any?>(
|
|
"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<String>) {
|
|
runApplication<DemoApplication>(*args)
|
|
}
|