I have a problem that Helvetica, which I am not using in any of my JRXML fields, is being used as the default font in Jasper PDF reports.
I am using iReports 4.0.2.
The first variant is to set the default font via setting net.sf.jasperreports.default.font.name and/or net.sf.jasperreports.default.pdf.font.name properties.
For example, it were my defaults settings in iReport (dialog Options -> JasperReports Properties):
With this settings (without setting default style in the template) I have the Helvetica font in my pdf generated by iReport.
After that I've changed settings in iReport like this:
And now I have Courier font in my pdf file (generated by iReport):
With JasperReports API you can set this properties with help of JRProperties.setProperty(java.lang.String key, boolean value) method.
The sample:
JRProperties.setProperty("net.sf.jasperreports.default.pdf.font.name", defaultPDFFont);
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
Another variant is to add default style in report's template.
The example:
<jasperReport .. topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="Base" isDefault="true" hAlign="Center" vAlign="Middle" fontSize="10" pdfFontName="Courier" pdfEncoding="UTF8" isPdfEmbedded="false"/>
<queryString>
More information about working with fonts you can read here.
I use NetBeans and on one of my projects i wanted to use Garamond as my default font for my pdfs so what i did was:
Tools -> options
Under IReport, click the Fonts Tab
Install a Font
Click on the "Export as Extension Button" which exports it as a jar file.
Add to class path.
Run report, save it, then view it.
Related
We're using Jaspersoft iReport Designer to create bilingual PDF outputs—each file contains both English and French text.
For accessibility reasons, we'd like to tag each block of text with its appropriate language in the resulting PDF. See PDF19: Specifying the language for a passage or phrase with the Lang entry in PDF documents for what we're trying to do.
Modifying the PDF files manually is not an option since we email them directly to our users.
Does Jaspersoft support this?
No JasperReports version<=6.7.0 do not support this, there is no available property in Configuration Reference to set Lang property to single textElement.
You have 2 options:
Post elaborate the pdf with for example iText as Dave Jarvis suggested. You can either try to change the dictionary or recreate the pdf adding this additional info. These methods are both fairly complex and naturally runtime will increase since you will need to read/recreate the pdf.
Modify source code of JasperReport to add support. Directly modify the JRPdfExporter, JRPdfExporterTagHelper or add your new exporter (to keep original library intact)
In this answer I will show you how you can modify the original lib adding additional tags (adding a LANG entry in the dictionary)
Background
The example in PDF19: Specifying the language for a passage or phrase with the Lang entry in PDF documents, show this PDF Object tree using iText RUPS.
I will assume that it is sufficient to add /Lang in our output related to the specific text that is not in default language of pdf. Note: If you need to add other entry as well the technique remains the same you just need to modify the below code example.
Source code modification
Add a new property net.sf.jasperreports.export.pdf.tag.lang if this
is present on reportElement in a type text field add a /Lang entry
with its value to dictionary.
Modification to JRPdfExporterTagHelper.java
Add static property identifier to follow code style
public static final String PROPERTY_TAG_LANG = JRPdfExporter.PDF_EXPORTER_PROPERTIES_PREFIX + "tag.lang";
Modify startText(boolean isHyperLink) and startText(String text, boolean isHyperlink), only first method is shown in this example (principal is same in second), we need to change method signature adding JRPrintText so that we can retrive properties.
protected void startText(JRPrintText text, boolean isHyperlink)
{
if (isTagged)
{
PdfStructureElement textTag = new PdfStructureElement(tagStack.peek(), isHyperlink ? PdfName.LINK : PdfName.TEXT);
if (text.hasProperties()&&text.getPropertiesMap().containsProperty(PROPERTY_TAG_LANG)){
textTag.put(PdfName.LANG, new PdfString(text.getPropertiesMap().getProperty(PROPERTY_TAG_LANG)));
}
pdfContentByte.beginMarkedContentSequence(textTag);
}
}
Since we change method signature we now need to modify JRPdfExporter.java so that we can recompile
Modify exportText(JRPrintText text)
...
if (glyphRendererAddActualText && textRenderer instanceof PdfGlyphRenderer)
{
tagHelper.startText(text,styledText.getText(), text.getLinkType() != null);
}
else
{
tagHelper.startText(text,text.getLinkType() != null);
}
...
You could remove the boolean text.getLinkType() != null, since we are actually passing the text object now, but I wanted to keep similar code for simplicity of example
Example
jrxml
<?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="TaggedPdf" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1be2df3d-cbc1-467c-8729-1ed569eb8a0d">
<property name="net.sf.jasperreports.export.pdf.tagged" value="true"/>
<property name="net.sf.jasperreports.export.pdf.tag.language" value="EN-US"/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="67" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="240" height="30" uuid="0722eadc-3fd6-4c4d-811c-64fbd18e0af5"/>
<textElement verticalAlignment="Middle"/>
<text><![CDATA[Hello world]]></text>
</staticText>
<staticText>
<reportElement x="0" y="30" width="240" height="30" uuid="5080190e-e9fd-4df6-b0f6-f1be3c109805">
<property name="net.sf.jasperreports.export.pdf.tag.lang" value="FR"/>
</reportElement>
<textElement verticalAlignment="Middle"/>
<text><![CDATA[Bonjour monde]]></text>
</staticText>
</band>
</title>
</jasperReport>
Exported to pdf with modifications above and visualized with iText RUPS
Is this enough according to PDF19: Specifying the language for a passage or phrase with the Lang entry in PDF documents
Verify that the language of a passage, phrase, or word that differs from the language of the surrounding text is correctly specified by a /Lang entry on an enclosing tag or container:
As far as I can see, yes but I'm not an expert in this matter, in any case if you need to add other tags the procedure is the same.
I try to format a Date in Jasper Reports and it works with Windows but not with Linux. With Linux the resulting text is truncated.
Code:
JRXML:
<parameter name="timestamp" class="java.util.Date"/>
[...]
<textField>
<reportElement x="0" y="0" width="50" height="16" uuid="0007846a-26f1-457a-a198-67a2f7c8417c">
<property name="local_mesure_unitwidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="local_mesure_unitx" value="pixel"/>
<property name="com.jaspersoft.studio.unit.x" value="px"/>
<property name="local_mesure_unity" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="px"/>
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box padding="2"/>
<textElement textAlignment="Left" verticalAlignment="Top">
<font size="8" pdfFontName="Helvetica" pdfEncoding="Cp1250" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[DATEFORMAT($P{timestamp},"dd.MM HH:mm")]]></textFieldExpression>
</textField>
Maven dependencies:
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-functions</artifactId>
<version>5.6.0</version>
</dependency>
Java:
private byte[] createPdf() {
try {
InputStream is = getClass().getResourceAsStream("MyReport.jasper");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("timestamp", new Date());
JRDataSource jrDataSource = new JRBeanCollectionDataSource(new Vector(), false);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrDataSource);
byte[] pdf = JasperExportManager.exportReportToPdf(jasperPrint);
return pdf;
} catch (JRException e) {
throw new RuntimeException("Could not create PDF.", e);
}
}
Results:
Result with Windows:
Result with Linux:
PDF properties:
Both generated PDF files have the same font properties in Acrobat Reader for Windows:
As you can see, the font is not embedded. (Second font "Helvetica" disappears if I add dependency jasperreports-fonts and remove attributes pdfFontName, pdfEncodingand isPdfEmbedded).
Research:
I read:
PDF generated with jasperreport not showing well on Linux but yes on Mac, could the os be related?
http://community.jaspersoft.com/questions/527138/pdf-text-truncated-linux-fine-windows
http://community.jaspersoft.com/questions/803503/why-text-filed-value-will-be-truncated-if-application-running-linux
and the solution seems to be to embed the font, but it doesn't work.
I use font "Helvetica" which is one of the default fonts and that's the reason, see Wikipedia:
These fonts, or suitable substitute fonts with the same metrics, must always be available in all PDF readers and so need not be embedded in a PDF
and https://stackoverflow.com/a/27345103/5277820:
If you use these fonts in iText, iText will ignore the embedded parameter, because it is safe to assume that Adobe Reader and other viewers can render these fonts correctly.
Question:
Why have the same font different widths with Windows and Linux? Or why is the text truncating and/or line wrapping different?
To calculate the font-metrics correctly, the font needs to be available to the java virtual macchine.
See this historical question: Font is not available to the JVM with Jasper Reports, that show various solution to the old error launched
However the correct solution with jasper-reports is to use font-extensions
If you use the distributed jasperreports-font.jar it contains these fonts:
DejaVu SansDejaVu SerifDejaVu Sans Mono
You need to use one of these in font name example fontName="DejaVu Sans", there is no automatic mapping to other fonts, the jar physically contains only these .ttf and no other (open jar and verify for different version of jasper-reports).
Hence only font installed on the pc or included in font-extension are available for the JVM.
If you need other fonts, the best solution is to generate your own font-extension including a valid .ttf font this can be done from within the IDE.
Never assume a particular font is available on a client system – gone are the days when there were only a couple of desktop operating systems on the market, available in a handful of western languages, with well-known lists of fonts that were stable for decades.
It's now much easier to create new fonts, users access content in their own language (with language-specific fonts), the industry launches new form factors every few years (with new vanity fonts), Unicode.org releases new specifications (that require overhauling existing fonts) and as a result the actual fonts present on different systems vary widely.
OSX and Windows still function somewhat in the old "fixed font list" mode, but newer entrants don't.
For PDFs, that means you have to embed fonts in the documents.
For the web, that means flexible site design that does not depend on exact pixel letter dimensions.
Trying to use an old legacy font like Helvetica or Arial is a pathologic case: either the client won't have it installed at all, and will alias it to something with different glyph metrics, or it will have an ancient version, that is lacking all the codepoints added since ASCII went out of favour, and won't be able to render modern international text. Arial is a bit more likely to work due to long windows dominance, but that's eroding quickly.
I am trying to export report to Excel (XLSX format) using jasper report, but get problems with stretching when text field is overflow.
I get just empty cells when value of textfield is bigger than textfield size.
I tried to use these parameters, but it isn't helping me:
<property name="net.sf.jasperreports.export.xls.collapse.row.span" value="true"/>
<property name="net.sf.jasperreports.print.keep.full.text" value="true"/>
<property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
<property name="net.sf.jasperreports.export.xls.wrap.text" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
Here is refer to screenshot:
http://clip2net.com/s/39cbljj
In HTML it looks ok:
http://c2n.me/39cyhRf
Does anybody know how to fix this?
Thanks.
you should do the following to get it working:
Set isStretchWithOverflow="true" for all TextField elements on Detail
band (it will fix cutting of long text
values)
Set stretchType="RelativeToBandHeight" for all TextField elements on Detail band (It will fix the issue with empty cells - all TextFields will be rendered with the same height)
set the property <property name="net.sf.jasperreports.export.xls.auto.fit.row" value="true"/>
on each TextField which may have long value. The point is that property net.sf.jasperreports.export.xls.auto.fit.row works on Element level only.Also please note that this property won't work for merged cells.
It works perfect in HTML, but during export in EXCEL format I get problem.
I looked stretch sample from jasper reports demo/samples folder and found out solution. I set horizontal alighment textfield parameter to "justify" and it will work in Excel and look perfect. Seems that without this parameter stretch does not work during export to Excel.
Thanks for the help, user1390785 !
I just got same issue and i solved it.
its simple. just open your report properties and set page height to 999999. convert again and see its just solved
I am using the JasperReports 4.0.5. Trying to suppress group footer and headers while exporting to CSV.
I've tried the using of
net.sf.jasperreports.export.{format}.exclude.origin.{suffix}.{arbitrary_name} property.
It didn't work.
Any suggestions?
TU
For hiding only the groupHeader band you should add this two properties (both lines) to the jrxml file, for example:
<property name="net.sf.jasperreports.export.csv.exclude.origin.band.1" value="groupHeader"/>
<property name="net.sf.jasperreports.export.csv.exclude.origin.group.1" value="GroupExpenses"/>
For hiding both groupHeader and groupFooter bands you should add all this properties to jrxml file:
<property name="net.sf.jasperreports.export.csv.exclude.origin.band.1" value="groupHeader"/>
<property name="net.sf.jasperreports.export.csv.exclude.origin.group.1" value="GroupExpenses"/>
<property name="net.sf.jasperreports.export.csv.exclude.origin.band.2" value="groupFooter"/>
<property name="net.sf.jasperreports.export.csv.exclude.origin.group.2" value="GroupExpenses"/>
In both samples the group name is GroupExpenses.
<group name="GroupExpenses">
For more details look at How can I suppress page headers and footers when exporting to XLS? topic.
When JasperReports outputs a file in the Excel xls format, the columns when viewed are only the fixed width determined in the jasper XML.
This is a problem when the data in the columns is of varying widths, causing some columns to wrap.
I've had a look at the API and the DTD and there doesn't seem to be a way of setting the column widths to auto adjust.
i.e.instead of displaying as
emailAdress
#email.com
display as
emailAdress#email.com
Is it possible to do this at all?
This question is similar to Jasper report column width
We can achieve the effect of auto column's width with help of net.sf.jasperreports.export.xls.auto.fit.column property. If we set the value as true the auto width will be enabled. The default value of this property is false.
The working sample
We can use the csv datasource and simple report designed in Jaspersoft Studio to check the result.
The report's design will be simple - only one textField in Detail band.
Datasource
It is very simple - only one column email. The first row contains just a column's name. We can told the datasource adapter to skip this first line.
email
emailAdress#email.com
short#have.org
a#b.net
Report's template
The width of textField will be too small to show the full text.
To "turn on" the auto width we should set the net.sf.jasperreports.export.xls.auto.fit.column property.
The jrxml will be:
<?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_auto_width_excel" pageWidth="100" pageHeight="842" columnWidth="100" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="c899fa54-7405-4371-b34f-429f5959b593">
<queryString language="csv">
<![CDATA[]]>
</queryString>
<field name="email" class="java.lang.String"/>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="04d0735d-a1b6-4a8f-b252-b8772d7c5abd">
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
</reportElement>
<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Output result
The generated xls file without using property looks like this (opened in MS Excel):
The generated xls file with net.sf.jasperreports.export.xls.auto.fit.column property looks like this (opened in MS Excel):
Notes
In both cases the xls files were generated in Jaspersoft Studio.
The Advanced Excel Features article contains more information about tricks of exporting report to MS Excel format