A simple report with only a png in it.
Png's dpi is 96, which looks pretty sharp. However, every time i export the report, be it to .docx or to pdf, only an awfully blurry image appears.
i've tried setting
net.sf.jasperreports.image.dpi
to 300, and to 96
both in ireports and directly on the report's jrxml as a property.
Nothing works.
exception
I've lost many days googling this matter but still no answers.
Update 1
I've been able to trace the cause of this strange behavior to itext. It seems that it has to do something with it.
Update 2
Here's the jrxml code
<?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="carta_policia" pageWidth="612" pageHeight="792" columnWidth="572" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fbda9a68-7549-438c-a8ad-b3aedaf0b2d4">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<property name="ireport.background.image" value="C:\Users\thou\workspace\cujillo\WebContent\resources\reports\fondo.png"/>
<property name="ireport.background.image.properties" value="false,true,0.25,0,0,0,0,0,0"/>
<property name="net.sf.jasperreports.image.dpi" value="96"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:/Users/thou/workspace/cujillo/WebContent/resources/reports/"]]></defaultValueExpression>
</parameter>
<parameter name="R_RADICADO" class="java.lang.String"/>
<parameter name="SUBREPORT_DATA_SOURCE" class="net.sf.jasperreports.engine.JRDataSource"/>
<parameter name="R_ASUNTO" class="java.lang.String"/>
<parameter name="R_LOCALIDAD" class="java.lang.String"/>
<parameter name="R_IMAGE_RENDERER" class="net.sf.jasperreports.engine.JRRenderable" isForPrompting="false"/>
<parameter name="R_PRINT_BACKGROUND" class="java.lang.String"/>
<background>
<band height="752"/>
</background>
<title>
<band height="371" splitType="Stretch">
<image scaleImage="RealSize">
<reportElement uuid="dbadb500-4011-415b-bd98-4236532783c4" x="234" y="147" width="75" height="63"/>
<imageExpression><![CDATA["C:\\Users\\thou\\workspace\\cujillo\\WebContent\\resources\\reports\\alcaldia mayor.png"]]></imageExpression>
</image>
<image scaleImage="Clip">
<reportElement uuid="ecf7dbe3-4369-41a8-ba49-db98ba5ef478" x="309" y="151" width="75" height="63"/>
<imageExpression><![CDATA["C:\\Users\\thou\\workspace\\cujillo\\WebContent\\resources\\reports\\alcaldia mayor.jpg"]]></imageExpression>
</image>
</band>
</title>
<pageHeader>
<band splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band splitType="Stretch"/>
</columnHeader>
<detail>
<band height="104" splitType="Stretch"/>
</detail>
<columnFooter>
<band splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="11" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="209" splitType="Stretch"/>
</summary>
</jasperReport>
Although we've raised the DPI setting in our presentations, I've implemented a custom exporter, which exports charts as powerpoint shapes and not as raster images.
At least for .docx it's feasible to implement something similar.
Before I've started the shape export classes, I've tried to use EMF (1,2) as an image format, but this is quite limited, e.g. it doesn't embed fonts.
I haven't searched further in the direction, but maybe you can transform an intermediate SVG image to something (raster/vector) which can be embedded into .docx/.pdf.
Have you checked the images inside .docx-Zip-file? ... are the plain images also blurry or just when they are displayed by Word/LibreOffice. And of course the images should change after you changed the DPI setting - if not, try to put a jasperreports.properties in the classpath
Related
I'm using Java and Jasper to generate reports in .xlsx format.
And I was successful and I have components in my application which create actual reports with data.
recently I was trying to add another component and the report generation was successful , but the generated .xslx file wont open with MS Excel or any other.
the code parts are below.
ArrayList<ReportBean> reportBean = new ArrayList<ReportBean>();
response.setHeader("Content-Disposition", "attachment;filename=BudgetaryQuote.xlsx");
response.setContentType("application/xlsx");
OutputStream outStream = response.getOutputStream();
Report report = new Report(template, "xlsx", reportBean, outStream);
//Report(String template, String format, ArrayList dataList,OutputStream outputStream)
report.generateReport();
in generateReport() it has ,
InputStream inputStream = new FileInputStream(new File(this.template));
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(this.dataList);
JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
if(this.format.equalsIgnoreCase(XLSX)) {
JRXlsxExporter Xlsxexporter = new JRXlsxExporter();
Xlsxexporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
Xlsxexporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
Xlsxexporter.exportReport();
}
the .jrxml file (It's pretty much blank)
<?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="timeSheetDetailedReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4ffef809-ea2d-4533-a0b9-bdd2d055afbe">
<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="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch"/>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
There is an error like in the image.
"Excel found unreadable content in filename.xls. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes."
How can I check what is the wrong with the file ?
I just solved the problem. But it's not clear.
my code had the below code ,
ArrayList<ReportBean> reportBean = new ArrayList<ReportBean>();
Report report = new Report(template, "xlsx", reportBean, outStream);
report.generateReport();
The problem was the ArrayList reportBean was empty. The I add an dummy ReportBean to the list and the problem was solved.
But I have no idea how it solved the problem and why
I have a Java class with two attribute id (integer) and name (string). I have created a list of objects and I want to use the JasperReports to make a pdf file.
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(conceptsList,true);
Map jasperParameters = new HashMap();
jasperParameters.put("Concepts", ds);
String input = "C:/report.jasper";
JasperPrint jp = JasperFillManager.fillReport(input, jasperParameters, new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jp, "C:/report.pdf");
In iReport Designer I have one parameter (Concepts) and set net.sf.jasperreports.engine.data.JRBeanCollectionDataSource as a parameter class. I also define two fields id and name and put them in the details band. but my pdf file does not show anything. I think somewhere I have to define datasource for id and name but I cannot find anything. It would be great if anyone can help me.
<?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="report22" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="313d6b25-bcea-43a0-80db-2c7733499ca2">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="Concepts" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="id" class="java.lang.Integer"/>
<field name="name" class="java.lang.String"/>
<title>
<band height="50" splitType="Stretch"/>
</title>
<detail>
<band height="200" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="b6c2dc20-ebaa-4dfe-8d42-b8318bf05d33"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="fbc83087-e178-4210-a9f0-a8e1d86a5f71"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
You can send the object data as a datasource to JRXML. Additionally, if the List should also be sent as a datasource to JRXML.
First design the iReport to accept Object as datasource:
1) Add the project "src" in class-path in iReport.
2) Specify the object package.className in JRXML report datasource.
3) Map the TextFields with the Object parameters.
Now in source code, use "JRBeanCollectionDataSource" Class for sending the List of Object in "JasperFillManager.fillReport()", for Connection.
Hope this helps.
I have a problem with JasperReports API.
I add html component into my report (via iReport designer 5.0.4). Compilation to jasper file runs without errors.
When I try create the report I get null from JasperPrint object (method createReport(getReportAsByteArray(), parameters, dataSource); returns null instead of JasperPrint object.
getReportAsByteArray() - returns jasper report file from my database).
The Html component (in iReport) is connected with string parameter, I put right string parameter into parameters HashMap<>. I already have tried write html text in html content exp. (html component properties).
It didn't work.
Earlier everything was fine, when the jasper file was without html component I could create my report and export to PDF.
Now I can see the resulting report (with html component) in my iReport preview (and it works), but when I fill my report via my java bean - it doesn't work.
(the jasper file with only one object - html component - didn't work too).
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="raport_test_1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5a918503-735f-4812-bbd1-f4017cca4b0f">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="TEXT_CONTENT" class="java.lang.String"/>
<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">
<componentElement>
<reportElement uuid="e9907391-3e2f-45c9-af15-b7a60f7a3312" x="0" y="0" width="555" height="125"/>
<hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="RealSize" horizontalAlign="Left" verticalAlign="Middle">
<hc:htmlContentExpression><![CDATA[$P{TEXT_CONTENT}]]></hc:htmlContentExpression>
</hc:html>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
And the method, which should return byte array of report pdf file
Map<String, Object> parameters = new HashMap<>();
parameters.put("TEXT_CONTENT","sample text<br/>");
List<Data> list= getList();
if (list == null) {
list = new ArrayList<>();
}
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
JasperPrint printableReport = createReport(getReportFileAsByteArray(), parameters, dataSource);
if (printableReport != null) {
try {
return JasperExportManager.exportReportToPdf(printableReport);
} catch (JRException ex) {
//
}
}
return null;
Thanks for any advices!
hi i am very new to Xml parsing
i want to change following attribute values frequently ...........
columnCount, width and height
After that i need to rewrite xml file with modified data
in following xml file by using java(sax ,Dom or jaxB parser) please any one can give some suggestion on it...............
=======================================================================================
<?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="Hello_subreport1_subreport1" language="groovy" columnCount="2" printOrder="Horizontal" pageWidth="520" pageHeight="802" columnWidth="260" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="ac19d62f-eac8-428e-8e0a-9011534189ed">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="subjectName" class="java.lang.String">
<fieldDescription><![CDATA[subjectName]]></fieldDescription>
</field>
<field name="subjectID" class="java.lang.Integer">
<fieldDescription><![CDATA[subjectID]]></fieldDescription>
</field>
<field name="maxMarks" class="java.lang.Integer">
<fieldDescription><![CDATA[maxMarks]]></fieldDescription>
</field>
<field name="redMarks" class="java.lang.Float">
<fieldDescription><![CDATA[redMarks]]></fieldDescription>
</field>
<field name="passMarks" class="java.lang.Integer">
<fieldDescription><![CDATA[passMarks]]></fieldDescription>
</field>
<field name="marks" class="java.lang.Float">
<fieldDescription><![CDATA[marks]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="52">
<textField isStretchWithOverflow="true">
<reportElement uuid="5f7665fb-9218-4434-a9e5-5eff306499b3" x="0" y="33" width="100" height="20"/>
<box>
<pen lineWidth="0.0"/>
<topPen lineWidth="0.0"/>
<leftPen lineWidth="0.0"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="0.0"/>
</box>
<textElement>
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{marks}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="6b999cb1-600e-4634-be8f-7ac99e225f49" x="0" y="13" width="100" height="20"/>
<box>
<topPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{subjectName}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
========================================================================
If you want to modify the document them use a DOM parser. This will transform the xml file into a datastructure where you can find the attributes and change their values. Have a look at jdom or dom4j, they are really easy to use.
A sax parser is a good choice if you only want to read the document. That parser just creates events while parsing the document.
Answering to your comment: I do not get a NPE but the rootNode.getChild("detail")) return null. That is because the element is associated with a namespace. Replace the last line in your sample code with
System.out.println(rootNode.getChild("detail", rootNode.getNamespace()));
That works.
there are tons of documents on web detailing editing of an XML. Explore them and then try something out. If you are stuck then post it here.
some ref: http://www.w3schools.com/dom/default.asp
How to modify XML data in Dom parser
http://www.mkyong.com/java/how-to-modify-xml-file-in-java-dom-parser/
http://www.drdobbs.com/jvm/easy-dom-parsing-in-java/231002580
Maybe you could use avc-binding-dom, which binds DOM nodes to your custom Java interface via annotations:
import org.w3c.dom.Node;
import net.avcompris.binding.annotation.XPath;
import net.avcompris.binding.dom.impl.DefaultDomBinder;
#Namespaces("xmlns:jr=http://jasperreports.sourceforge.net/jasperreports")
#XPath("/jr:jasperReport")
interface MyJasperReport {
#XPath("#columnCount")
int getColumnCount();
void setColumnCount(int columnCount);
#XPath("#pageWidth")
int getPageWidth();
void setPageWidth(int pageWidth);
#XPath("jr:property[#name = 'ireport.zoom']/#value")
String getZoom();
void setZoom(String zoom);
}
Node node = ... // You have to load the XML file into a DOM node.
MyJasperReport jr = new DefaultDomBinder().bind(node, MyJasperReport.class);
jr.setColumnCount(4); // previously: 2
jr.setPageWidth(1024); // previously: 520
jr.setZoom("1.45"); // previously: "1.0"
... // then save the DOM node into a XML file.
I have written this code to show a simple report using iReport 4.7.1.
private void printReport() {
try {
JRTableModelDataSource dataSource = new JRTableModelDataSource(
new JTable().getModel());
String reportSource = "C:\\Reports\\report1.jrxml";
Map<String, Object> params = new HashMap<String, Object>();
params.put("Name", "SriLanka");
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
e.printStackTrace();
}
}
Here are my libraries:
commons-beanutils-1.8.0.jar
commons-collections-2.1.1.jar
commons-digester-2.1.jar
commons-logging-1.1.1.jar
jasperrreports-4.7.1.jar
jasperreports-applet-4.7.1.jar
jasperreports-javaflow-4.7.1.jar
groovy-all-1.7.5.jar
and NetBeans output shows this warning
Oct 08, 2012 2:57:31 PM net.sf.jasperreports.engine.component.ComponentsEnvironment findBundles
WARNING: Found two components for namespace http://jasperreports.sourceforge.net/jasperreports/components
and here is 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="report1" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="BlankPage" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5174594e-de3c-4b09-932b-c6665bf6a34b">
<parameter name="Name" class="java.lang.String" isForPrompting="false"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement uuid="fd02a8bc-548b-41c1-aed5-2a7c2e257704" x="210" y="23" width="157" height="36"/>
<textElement>
<font size="18"/>
</textElement>
<textFieldExpression><![CDATA[$P{Name}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch"/>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
The report opens but doesn't show any text.
To solve this problem you should:
check your datasource, it can be empty;
add textFields elements with fields expressions (<textFieldExpression><![CDATA[$F{field}]]></textFieldExpression>) to the Detail band to show records in report.
Anyway your parameter Name should be displayed in the Title band (in generated report). You can pass even the JREmptyDataSource to check this.
For showing data you can chose one of this ways:
1. Using Detail band
It is classical, "old school" method for showing data stored in records from the data source. The JR engine iterates through the data source for rendering this section. With help of textField elements we can show the fields values.
The sample: look at $jasperreports$\demo\samples\jasper sample from JR distribution package.
2. Using Crosstab component
The Crosstab component used for summarizing data into grid which has two dimensions.
The sample: look at $jasperreports$\demo\samples\crosstabs sample.
3. Using List component
The List component iterates on set of its records (defined with help of subDataset property).
The sample: look at Using the Built-in List Component sample.
4. Using Table component
The Table is using for showing data with tabular structure. As a List component has its own subDataset.
The sample: look at Using the Built-in Table Component sample.
4. Using Subreport component
The Subreport is using for building complex reports such as compilation of different reports in one. This component often operates with subqueries.
The sample: look at Subreports sample.
You can read details in JasperReports Ultimate Guide