How to embed and display an SVG image in iReport - pdf

I am using iReport 4.5.0, and want to embed an SVG image to generate a PDF.
When using iReport's preview option to run the report, the image won't get rendered to the PDF (i.e. there's an empty space where the image is supposed to be).
I do not see any exceptions in the logs during execution, what's going on?
Here's the image tag code snippet:
<?xml version="1.0" encoding="UTF-8"?>
<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="reportSVGTest" language="groovy" pageWidth="595"
pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20"
topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="50">
<staticText>
<reportElement x="81" y="13" width="100" height="20"/>
<textElement/>
<text><![CDATA[Hello]]></text>
</staticText>
</band>
</title>
<detail>
<band height="443" splitType="Stretch">
<image hAlign="Center" vAlign="Middle" isUsingCache="true" isLazy="true">
<reportElement x="12" y="17" width="512" height="403"/>
<imageExpression class="net.sf.jasperreports.engine.JRRenderable">
<![CDATA[
net.sf.jasperreports.renderers.BatikRenderer.getInstance(
new java.io.File("C:\\Users\\akshayd\\Desktop\\polygons1.svg"))
]]>
</imageExpression>
</image>
</band>
</detail>
</jasperReport>

I wanted to embed a SVG text in my report, and this topic helped me succeed.
I am using Jaspersoft Studio with full support for JasperReports 5.5.0.
Here is how i did :
Create a image element
Choose Custom expression and let expression empty
Modify JRXML source: add a tag imageExpression in the tag image
<image>
<reportElement x="0" y="60" width="50" height="50" uuid="c0d0856e-0bb1-4634-b8b0- 6f7ace0e3ff3"/>
<imageExpression><![CDATA[net.sf.jasperreports.renderers.BatikRenderer.getInstanceFromText("<svg:svg xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.0\" width=\"88\" height=\"21\" id=\"svg\"><svg:g id=\"layer1\"><svg:path id=\"path1\" d=\"M 61.799149,2.0342178 L 67.119461,2.0342178 L 67.119461,19.213905 L 61.799149,19.213905 L 61.799149,2.0342178 z\" style=\"text-anchor:start;fill:#FF0000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;\" /></svg:g> </svg:svg>")]]></imageExpression>
</image>
So, i use the method getInstanceFromText of BatikRenderer.
Do not forget to protect double quotes in your SVG text.

Related

How to do form block letter printing?

I want to print in block letters in a jasper print. E.g. I have a Bank Format and I want the name to come in the boxes. How does one do that?
Name: [F][I][R][S][T][ ][N][A][M][E]
Option1 - Print cell by cell with a ${FIRST_NAME}.charAt(x) - which is unsustainable
Option2 - Add spaces between characters but this changes based on font size
Option3 - Add padding spaces to the end of the text. Stretch the text to the full width available and characters get distributed equally - how does one do this?
This is normal achieved using a monospaced font.
If you like to achieve it with other fonts or the spacing is very large you can use a subreport (that is called every time you need the fixed space)
Example
Main report (I'm using parameter to test)
Call the subreport with a datasource that contains each letter, I'm using $P{testText}.split("") , since char[] is not allowed in the JRBeanArrayDataSource.
Note: split will give empty first String in java7 but not in java8
<?xml version="1.0" encoding="UTF-8"?>
<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="MainFixedSpace" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d2bcb5ab-c751-4f39-8753-561b8a6ac629">
<parameter name="testText" class="java.lang.String">
<defaultValueExpression><![CDATA["Hello world"]]></defaultValueExpression>
</parameter>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:\\jdd\\projects\\StackTrace\\jasper\\"]]></defaultValueExpression>
</parameter>
<title>
<band height="25" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="555" height="25" uuid="76f53ca9-da1f-46c8-bb3b-aca0dc43d2d3"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanArrayDataSource($P{testText}.split(""))]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "MainFixedSpace_subreport.jasper"]]></subreportExpression>
</subreport>
</band>
</title>
</jasperReport>
Subreport
Setup the the fixed space column count in my case 20 columns on each row (you need to adapted it to your case) and set printOrder="Horizontal" The _THIS field will let you access the letter.
<?xml version="1.0" encoding="UTF-8"?>
<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="MainFixedSpace_subreport" columnCount="20" printOrder="Horizontal" pageWidth="555" pageHeight="802" columnWidth="27" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="77ba37de-32e1-4ec6-8496-58d716d0340d">
<field name="_THIS" class="java.lang.String"/>
<detail>
<band height="25" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="27" height="25" uuid="ffb27000-41ba-419f-8836-b24dbb0dbb25"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Output

How to create a report with fields in between paragraph text?

My report is something like below.
In the shown report, text in black color is static and the text in RED are fields to be replaced with some value dynamically by java.
In the above picture, line 1 and 2 I have no problem.
But for the paragraph I am not sure what to use? Should I use static-text box or Text field? If I use part static-text box and part dynamic, its becoming clumsy and difficult to maintain the line spacing.
So please advise how to design the following report in jasper studio 5.6.
Use a textField and then String concatenation on your text, when you need to number format use the NumberFormat API or the DecimalFormat API
If you need to break line, set bold text you can use html to achive this by setting markup="html" on the textElement
Example
<?xml version="1.0" encoding="UTF-8"?>
<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="test" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0ef33a9d-fd2b-46cc-9ec4-c6906db8097b">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="testNumber" class="java.lang.Double" isForPrompting="false">
<defaultValueExpression><![CDATA[new Double(10000.23445)]]></defaultValueExpression>
</parameter>
<title>
<band height="57" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="373" height="57" uuid="2f795f8a-43fa-4b89-a173-76854a13ba48"/>
<textElement markup="html"/>
<textFieldExpression><![CDATA["Just use a textField and string concat the other text a " + java.text.NumberFormat.getNumberInstance().format($P{testNumber}) + " as you see you can even format it and remeber if you set as html you can <br/> break line and <b>bold</b> text"]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
Output

JS TileMap iOS 8 and swift, incomprehensible archive?

I've bridged JS TileMap for my spritekit game. I've put a tmx file in my project and I'm trying to init a JSTileMap object
let map = JSTileMap(fileNamed: "level 1-1.tmx")
i get this error
2014-11-15 17:43:03.428 MyGame[5726:1901295] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x3c, 0x3f, 0x78, 0x6d, 0x6c, 0x20, 0x76, 0x65)'
*** First throw call stack:
(
Could it be that it's not finding the file? I noticed if i change the filename to something nonexistent i get the same error..
Here is the contents of my tmx file
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="1000" height="800" tilewidth="1" tileheight="1">
<tileset firstgid="1" name="enemies" tilewidth="80" tileheight="75">
<tile id="0">
<properties>
<property name="name" value="EnemyA"/>
</properties>
<image width="66" height="75" source="../Atlases/iPhone/enemyA.atlas/enemy#2x.png"/>
</tile>
<tile id="1">
<image width="65" height="67" source="../Atlases/iPhone/enemyB.atlas/enemy#2x.png"/>
</tile>
<tile id="2">
<properties>
<property name="name" value="EnemyC"/>
</properties>
<image width="60" height="60" source="../Atlases/iPhone/enemyC.atlas/enemy#2x.png"/>
</tile>
<tile id="3">
<image width="61" height="65" source="../Atlases/iPhone/enemyD.atlas/enemy#2x.png"/>
</tile>
<tile id="4">
<image width="74" height="58" source="../Atlases/iPhone/enemyE.atlas/enemy#2x.png"/>
</tile>
<tile id="5">
<properties>
<property name="name" value="EnemyF"/>
</properties>
<image width="80" height="73" source="../Atlases/iPhone/enemyF.atlas/enemy#2x.png"/>
</tile>
</tileset>
<objectgroup color="#000000" name="Object Layer 1">
<object name="EnemyA" type="enemy" gid="1" x="137" y="489"/>
<object name="EnemyC" type="enemy" gid="6" x="468" y="454"/>
<object name="EnemyB" type="enemy" gid="3" x="310" y="321"/>
</objectgroup>
</map>
i've tried editing it down to just and it still throws that error. I'm making sure to include libz.dylib
what am i doing wrong?
I was using the wrong init method
it should be
let map = JSTileMap(named: "level 1-1.tmx")
Try to remove #2 from your image files source.. Then run it again. I think you cannot use #2 in tiled. Also remove spaces from you tmx name.

How to hide page header band on last page

I think I am having a problem with evaluation time. In this example, you see the variable $P{REPORT_PARAMETERS_MAP}.get("LastPageNumber") reflecting the last page correctly; it updates correctly. But when I use it for the "printWhenExpression" of a detail band field (experiment) or for the page header band (my goal) it does not work.
I am using "page" evaluation time throughout which seems to work for the variable itself. But the printWhenExpressions are not working using this variable.
Here is my xml file :
<?xml version="1.0" encoding="UTF-8"?>
<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="report3" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<queryString>
<![CDATA[select * from users]]>
</queryString>
<field name="userName" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<pageHeader>
<band height="23" splitType="Stretch">
<textField evaluationTime="Page">
<reportElement x="120" y="0" width="238" height="20">
<printWhenExpression><![CDATA[!$V{PAGE_NUMBER}.equals($P{REPORT_PARAMETERS_MAP}
.get("LastPageNumber"))]]></printWhenExpression>
</reportElement>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["please don't print on last page - page header"]]></textFieldExpression>
</textField>
<textField evaluationTime="Page">
<reportElement x="380" y="0" width="175" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["last page variable is: " + $V{PAGE_NUMBER}.equals($P{REPORT_PARAMETERS_MAP}
.get("LastPageNumber"))]]></textFieldExpression>
</textField>
<textField>
<reportElement x="2" y="2" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["Page header"]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="21" splitType="Stretch">
<textField evaluationTime="Page">
<reportElement x="381" y="0" width="174" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["last page variable is: " + $V{PAGE_NUMBER}.equals($P{REPORT_PARAMETERS_MAP}
.get("LastPageNumber"))]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{userName}]]></textFieldExpression>
</textField>
<textField evaluationTime="Page">
<reportElement x="119" y="0" width="191" height="20">
<printWhenExpression><![CDATA[!$V{PAGE_NUMBER}.equals($P{REPORT_PARAMETERS_MAP}
.get("LastPageNumber"))]]></printWhenExpression>
</reportElement>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["please don't print on last page - detail"]]></textFieldExpression>
</textField>
</band>
</detail>
<lastPageFooter>
<band height="50">
<line>
<reportElement x="188" y="21" width="100" height="1">
<printWhenExpression><![CDATA[new Boolean($P{REPORT_PARAMETERS_MAP}.put("LastPageNumber",
$V{PAGE_NUMBER}))]]></printWhenExpression>
</reportElement>
</line>
<textField evaluationTime="Page">
<reportElement x="380" y="2" width="174" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA["last page variable is: " + $V{PAGE_NUMBER}.equals($P{REPORT_PARAMETERS_MAP}
.get("LastPageNumber"))]]></textFieldExpression>
</textField>
</band>
</lastPageFooter>
</jasperReport>
My solution was to make two reports (one for the last page) and then join them (jasperPrints) using the array list option for he jasper exporter:
List jasperPrintList = new ArrayList();
JasperPrint jasperPrint1 =
JasperFillManager.fillReport(
jasperReport1,
parameters,
c
);
JasperPrint jasperPrint2 =
JasperFillManager.fillReport(
jasperReport2,
parameters,
c
);
jasperPrintList.add(jasperPrint1);
jasperPrintList.add(jasperPrint2);
JRPdfExporter exporter = new JRPdfExporter();
ByteArrayOutputStream byte_report = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "test_report_name");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byte_report);
exporter.exportReport();

How to add bullet point in TextField

I want to add bullet point to my static text and to a text field, i added style tag to the jrxml but it didn't work for me :
Here's the example :
TextField :
<textField isBlankWhenNull="true">
<reportElement uuid="cfd514bc-d1c5-4369-b10d-71042b046e37" x="11" y="0" width="400" height="12"/>
<textElement/>
<textFieldExpression><![CDATA[<style size="40">.</style>$F{LMSG}]]></textFieldExpression>
</textField>
StaticText:
<staticText>
<reportElement uuid="c1485aba-09a4-4c7b-9106-0893341f1368" x="44" y="107" width="309" height="15"/>
<textElement>
<font size="9"/>
</textElement>
<text><![CDATA[<style size="40">.</style>Je déduis cet avoir de ma commande]]></text>
</staticText>
You can use styled markup.
Try to use <li> tag.
The sample
The jrxml file:
<?xml version="1.0" encoding="UTF-8"?>
<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="bullet_sample" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="aa7ced41-689a-4b8d-94aa-ee79c243a7a8">
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement uuid="bf39def6-a3a7-4fa1-9e99-e488b3567974" x="159" y="31" width="100" height="20"/>
<textElement markup="styled"/>
<textFieldExpression><![CDATA["<li>Text with bullet</li>"]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
The result will be (via preview in iReport):
In case using field, the right expression will be:
<textField>
<reportElement x="171" y="0" width="100" height="20"/>
<textElement markup="styled"/>
<textFieldExpression><![CDATA["<li>" + $F{fieldName} + "</li>"]]></textFieldExpression>
</textField>
Note:
You can find more info about styled text in Style a text field in JasperReports post and in Styled Text sample
I used the <li> tag, but it introduced padding which did not look good. So, I used "\u2022" which gives a round symbol like
<textFieldExpression><![CDATA["\u2022 Line1\n\u2022 Line2"]]></textFieldExpression>
So, this looks much cleaner