Skip to content

Commit

Permalink
jasper reports unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Lagnada committed Aug 10, 2013
1 parent 28a34fd commit cca4810
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 126 deletions.
Expand Up @@ -36,84 +36,68 @@
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Controller
public class JasperReportController
{
public class JasperReportController {

enum OutputType
{
enum OutputType {
pdf, xls
}

//@Value("classpath:JasperDesign.jrxml")
//private Resource resource;

@RequestMapping(value = "/reports", method = GET)
public String handleJasper()
{
public String handleJasper() {
return "reports";
}

private JasperDesign loadJasperDesign(String designName)
{
private JasperDesign loadJasperDesign(String designName) {
String uri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/jasper-designs/" + designName)
.build()
.toUri().toASCIIString();
InputStream inputStream = null;
JasperDesign jasperDesign = null;
Resource resource = null;
try
{
try {
resource = new UrlResource(uri);
inputStream = resource.getInputStream();
jasperDesign = JRXmlLoader.load(resource.getInputStream());
}
catch (IOException e)
{
} catch (IOException e) {
IOUtils.closeQuietly(inputStream);
}
catch (JRException e)
{
} catch (JRException e) {
throw new IllegalStateException(e);
}
return jasperDesign;
}

private JasperReport compileJasperReport(String designName)
{
try
{
private JasperReport compileJasperReport(String designName) {
try {
return JasperCompileManager.compileReport(loadJasperDesign(designName));
}
catch (JRException e)
{
} catch (JRException e) {
throw new IllegalStateException(e);
}
}


@RequestMapping(value = "/reports", method = POST)
public void handleJasperExport(@RequestParam(value = "type", defaultValue = "pdf") OutputType outputType, HttpServletRequest request, HttpServletResponse response) throws Exception
{
public void handleJasperExport(@RequestParam(value = "type", defaultValue = "pdf") OutputType outputType, HttpServletRequest request, HttpServletResponse response) throws Exception {
long start = System.currentTimeMillis();

JasperReport jasperReport = compileJasperReport("JasperDesign.jrxml");

Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("ReportTitle", "Pohjoisen kesän yöttömät yöt,");
// parameters.put("ReportTitle", "Pohjoisen kesän yöttömät yöt,");
parameters.put("ReportTitle", "敏捷的棕色狐狸跃过那只懒狗。");
parameters.put("Logo", "http://jasperreports.sourceforge.net/jasperreports.png");

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanCollectionDataSource(getCustomers()));
JRExporter exporter;
if (outputType == OutputType.xls)
{
if (outputType == OutputType.xls) {
exporter = new JRXlsExporter();
response.setContentType("application/xls");
response.setHeader("Content-Disposition", "attachment; filename=jasper.xls");
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
}
else
{
} else {
exporter = new JRPdfExporter();
response.setContentType("application/pdf");
//response.setHeader("Content-Disposition", "attachment; filename=example.pdf");
Expand All @@ -126,27 +110,23 @@ public void handleJasperExport(@RequestParam(value = "type", defaultValue = "pdf
}

@RequestMapping(value = "/reports/unicode", method = GET)
public void handleJasperExportUnicode(@RequestParam(value = "type", defaultValue = "pdf") OutputType outputType, HttpServletRequest request, HttpServletResponse response) throws Exception
{
public void handleJasperExportUnicode(@RequestParam(value = "type", defaultValue = "pdf") OutputType outputType, HttpServletRequest request, HttpServletResponse response) throws Exception {
long start = System.currentTimeMillis();

JasperReport jasperReport = compileJasperReport("UnicodeReport.jrxml");

Map<String, Object> parameters = new HashMap<String, Object>();
//parameters.put("ReportTitle", "Customer Report");
parameters.put("ChineseText", "敏捷的棕色狐狸跃过那只懒狗。");
///parameters.put("Logo", "http://jasperreports.sourceforge.net/jasperreports.png");

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanCollectionDataSource(getCustomers()));
JRExporter exporter;
if (outputType == OutputType.xls)
{
if (outputType == OutputType.xls) {
exporter = new JRXlsExporter();
response.setContentType("application/xls");
response.setHeader("Content-Disposition", "attachment; filename=jasper.xls");
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
}
else
{
} else {
exporter = new JRPdfExporter();
response.setContentType("application/pdf");
//response.setHeader("Content-Disposition", "attachment; filename=example.pdf");
Expand All @@ -158,17 +138,15 @@ public void handleJasperExportUnicode(@RequestParam(value = "type", defaultValue
System.err.println("Filling time : " + (System.currentTimeMillis() - start));
}

private List<Customer> getCustomers()
{
private List<Customer> getCustomers() {
List<Customer> customers = new ArrayList<Customer>();
Customer c;
Random random = new Random(1000);
LoremIpsum loremIpsum = new LoremIpsum();
for (int i = 1; i < 100; i++)
{
for (int i = 1; i < 100; i++) {
c = new Customer(Long.valueOf(i));
c.setFirstName("John");
c.setLastName("Doe");
c.setFirstName("John约翰");
c.setLastName("Doe李四");
c.setBalance(random.nextDouble());
c.setPhone("425-555-1212");
c.setNotes(loremIpsum.getWords(10, random.nextInt(10)));
Expand Down
15 changes: 0 additions & 15 deletions src/main/resources/arphic/fonts.xml

This file was deleted.

File renamed without changes.
14 changes: 14 additions & 0 deletions src/main/resources/fonts/fonts.xml
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<fontFamilies>

<fontFamily name="Arial">
<normal>fonts/arialuni.ttf</normal>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.pdf">Arial, SansSerif</export>
</exportFonts>
</fontFamily>

</fontFamilies>
2 changes: 1 addition & 1 deletion src/main/resources/jasperreports_extension.properties
@@ -1,2 +1,2 @@
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.arphic=arphic/fonts.xml
net.sf.jasperreports.extension.simple.font.families.arialuni=fonts/fonts.xml
96 changes: 45 additions & 51 deletions src/main/webapp/jasper-designs/JasperDesign.jrxml
Expand Up @@ -5,16 +5,13 @@
name="FirstJasper" pageWidth="792" pageHeight="612" orientation="Landscape" columnWidth="752"
leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30"
uuid="98ddb265-196f-4e42-ac80-d0a78f16a57a">
<property name="ireport.zoom" value="1.5"/>
<property name="ireport.zoom" value="3.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="Arial_Normal" isDefault="true" fontName="Arial" fontSize="8" isBold="false" isItalic="false"
isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252"
isPdfEmbedded="false"/>
isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" isPdfEmbedded="false"/>
<style name="Arial_Bold" fontName="Arial" fontSize="8" isBold="true" isItalic="false" isUnderline="false"
isStrikeThrough="false" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Italic" fontName="Arial" fontSize="8" isBold="false" isItalic="true" isUnderline="false"
isStrikeThrough="false" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
isStrikeThrough="false" pdfFontName="Helvetica-Bold" isPdfEmbedded="false"/>
<parameter name="ReportTitle" class="java.lang.String"/>
<parameter name="Logo" class="java.lang.String"/>
<field name="CUSTOMER_ID" class="java.lang.Long">
Expand All @@ -36,96 +33,82 @@
<fieldDescription><![CDATA[notes]]></fieldDescription>
</field>
<title>
<band height="100" splitType="Stretch">
<band height="50" splitType="Stretch">
<elementGroup>
<line>
<reportElement uuid="4e6dcb11-35b7-4458-a001-206aee1b5237" x="0" y="0" width="752" height="1"/>
</line>
<image scaleImage="Clip">
<reportElement uuid="9c7b325d-76ec-418f-bb15-3bfdfab4b732" x="0" y="1" width="165" height="40"/>
<reportElement uuid="9c7b325d-76ec-418f-bb15-3bfdfab4b732" x="0" y="0" width="165" height="40"/>
<imageExpression><![CDATA[$P{Logo}]]></imageExpression>
</image>
</elementGroup>
<staticText>
<reportElement uuid="8c203a10-8719-4125-b90f-dd91ba0e2f6e" x="377" y="35" width="375" height="15"/>
<reportElement uuid="8c203a10-8719-4125-b90f-dd91ba0e2f6e" style="Arial_Bold" x="377" y="35" width="375"
height="15"/>
<textElement textAlignment="Right">
<font size="12"/>
</textElement>
<text><![CDATA[(c)2001-2006 by Tyrion Lannister]]></text>
</staticText>
<textField isBlankWhenNull="true">
<reportElement uuid="b8aa805b-6f6c-4892-b867-51b68278445d" x="377" y="1" width="375" height="35"/>
<textElement textAlignment="Right">
<font size="22"/>
<reportElement uuid="b8aa805b-6f6c-4892-b867-51b68278445d" x="377" y="0" width="375" height="35"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font fontName="Arial" size="22"/>
</textElement>
<textFieldExpression><![CDATA[$P{ReportTitle}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="45" splitType="Stretch">
<rectangle>
<reportElement uuid="754c4d92-391f-4bfd-ad84-dbe446cf3541" mode="Transparent" x="0" y="0" width="752"
height="40"/>
</rectangle>
<staticText>
<reportElement uuid="191a2cdd-e40a-4143-a5bb-7e7823a123b5" x="0" y="0" width="752" height="25"/>
<textElement textAlignment="Center">
<font size="18"/>
</textElement>
<text><![CDATA[Customer List]]></text>
</staticText>
<textField>
<reportElement uuid="e34e7fd8-c567-428e-8172-9ef96942ef14" x="0" y="25" width="752" height="15"/>
<textElement textAlignment="Center">
<font size="10"/>
</textElement>
<textFieldExpression><![CDATA["Message Here"]]></textFieldExpression>
</textField>
</band>
<band height="10" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="12" splitType="Stretch">
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="b41392fa-a5c9-4130-a3be-620c0d9677bf" style="Arial_Bold" mode="Opaque" x="0" y="0"
width="100" height="12" forecolor="#FFFFFF" backcolor="#333333"/>
width="100" height="12" forecolor="#000000" backcolor="#FFFFFF"/>
<textElement textAlignment="Left"/>
<text><![CDATA[Customer ID]]></text>
</staticText>
<staticText>
<reportElement uuid="01b8a44c-247b-4f1f-abfa-0d12fe01a122" style="Arial_Bold" mode="Opaque" x="100"
y="0" width="135" height="12" forecolor="#FFFFFF" backcolor="#333333"/>
y="0" width="135" height="12" forecolor="#000000" backcolor="#FFFFFF"/>
<textElement textAlignment="Left"/>
<text><![CDATA[First Name]]></text>
</staticText>
<staticText>
<reportElement uuid="aec39fb9-a51c-4cb9-b663-31fb1c44f17f" style="Arial_Bold" mode="Opaque" x="235"
y="0" width="189" height="12" forecolor="#FFFFFF" backcolor="#333333"/>
y="0" width="189" height="12" forecolor="#000000" backcolor="#FFFFFF"/>
<textElement textAlignment="Left"/>
<text><![CDATA[Last Name]]></text>
<text><![CDATA[Last Name 懒狗]]></text>
</staticText>
<staticText>
<reportElement uuid="45bd9941-470a-4a28-af96-ab3591f86d66" style="Arial_Bold" mode="Opaque" x="424"
y="0" width="76" height="12" forecolor="#FFFFFF" backcolor="#333333"/>
y="0" width="76" height="12" forecolor="#000000" backcolor="#FFFFFF"/>
<textElement textAlignment="Left"/>
<text><![CDATA[Balance]]></text>
</staticText>
<staticText>
<reportElement uuid="f43a69b5-f0cc-4b74-b25d-a5bea51402f0" style="Arial_Bold" mode="Opaque" x="500"
y="0" width="83" height="12" forecolor="#FFFFFF" backcolor="#333333"/>
y="0" width="83" height="12" forecolor="#000000" backcolor="#FFFFFF"/>
<textElement textAlignment="Left"/>
<text><![CDATA[Phone]]></text>
</staticText>
<staticText>
<reportElement uuid="6a03f3a4-bbde-47e1-a64d-62065d328df5" style="Arial_Bold" mode="Opaque" x="583"
y="0" width="169" height="12" forecolor="#FFFFFF" backcolor="#333333"/>
y="0" width="169" height="12" forecolor="#000000" backcolor="#FFFFFF"/>
<textElement textAlignment="Left"/>
<text><![CDATA[Notes]]></text>
</staticText>
<line>
<reportElement uuid="e90fff54-2ebc-4c50-b4af-8adf383e9801" x="0" y="14" width="752" height="1"
forecolor="#0000FF"/>
<graphicElement>
<pen lineWidth="0.5" lineColor="#0000FF"/>
</graphicElement>
</line>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<band height="16" splitType="Stretch">
<rectangle>
<reportElement uuid="53daeddb-b5c3-402b-98a6-dc8117c22edb" x="-1" y="4" width="1" height="1"
forecolor="#FF0000" backcolor="#FF0000">
Expand All @@ -134,40 +117,51 @@
</reportElement>
</rectangle>
<textField>
<reportElement uuid="33b0edad-7a82-47d2-b53a-ae7065f98be8" x="0" y="0" width="100" height="15"/>
<reportElement uuid="33b0edad-7a82-47d2-b53a-ae7065f98be8" x="0" y="1" width="100" height="15"/>
<textElement/>
<textFieldExpression><![CDATA[$F{CUSTOMER_ID}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement uuid="f170fd07-624f-424d-b56a-bdf26c8cc371" x="100" y="0" width="135" height="15"/>
<reportElement uuid="f170fd07-624f-424d-b56a-bdf26c8cc371" x="100" y="1" width="135" height="15"/>
<textElement>
<font fontName="Arial Unicode MS" isStrikeThrough="false"/>
<font fontName="Arial" isStrikeThrough="false"/>
</textElement>
<textFieldExpression><![CDATA[$F{FIRST_NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="3b4d016f-ed8e-4389-80a7-32dc0c0dbfaf" x="235" y="0" width="189" height="15"/>
<reportElement uuid="3b4d016f-ed8e-4389-80a7-32dc0c0dbfaf" x="235" y="1" width="189" height="15"/>
<textElement/>
<textFieldExpression><![CDATA[$F{LAST_NAME}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement uuid="cf9cf53c-3616-4301-80c1-b21a3050aea2" stretchType="RelativeToBandHeight" x="583"
y="0" width="169" height="15"/>
y="1" width="169" height="15"/>
<textElement/>
<textFieldExpression><![CDATA[$F{NOTES}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" pattern="€ #,##0.00" isBlankWhenNull="true">
<reportElement uuid="17993c1c-d071-46da-b810-63509302a60b" x="424" y="0" width="76" height="15"/>
<reportElement uuid="17993c1c-d071-46da-b810-63509302a60b" x="424" y="1" width="76" height="15"/>
<textElement>
<font fontName="Arial Unicode MS" isPdfEmbedded="false"/>
</textElement>
<textFieldExpression><![CDATA[$F{BALANCE}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="0e3e53b3-5216-4856-8818-a67d4656a57e" x="500" y="0" width="83" height="15"/>
<reportElement uuid="0e3e53b3-5216-4856-8818-a67d4656a57e" x="500" y="1" width="83" height="15"/>
<textElement/>
<textFieldExpression><![CDATA[$F{PHONE}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="20">
<textField>
<reportElement uuid="312141aa-2469-4880-a87e-59b71de58437" x="324" y="3" width="100" height="15"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="6"/>
</textElement>
<textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>

0 comments on commit cca4810

Please sign in to comment.