Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an external URL hyperlink with JasperReports

How do you include a hyperlink (URL) in a PDF that links to an external site?

Using a simple string like "http://www.stackoverflow.com", a link is automatically generated. But, how can I use a URL like <a href="http://www.stackoverflow.com">Click here</a>?

If I use this HTML string, Jaspers create a link but also shows the code.

Using JasperReports 4.0.4 and iReport 4.5.1.

like image 848
Ricardo Avatar asked Jun 28 '12 22:06

Ricardo


People also ask

How do I create a hyperlink in Jasper studio?

To set anchor, bookmark, or hyperlink properties, go to the Hyperlink tab in the Properties view. Use Anchor and Bookmark to create an anchor inside the current document; it is only available for elements that support hyperlinks (textfield, image, and chart).

Where do I put JasperReports properties?

jasper. properties is looked up by jasper reports in the classpath, so it can be directly in the WEB-INF/classes folder, or in the root folder of any of the jars in WEB-INF/lib.

How do I use JasperReports font?

The best way to use a font in JasperReports Library is to use a font extension. Jaspersoft fonts can be configured in Studio and Server and displayed in the PDF view. Fonts can be referred from an URL and from the file system using the Preferences dialog window from the Jaspersoft Studio.


2 Answers

To make a textField a hyperlink to an external URL, you need to add the attribute hyperlinkType="Reference" to the element, and add a <hyperlinkReferenceExpression> tag within it. The reference expression is where you put the URL.

For example:

<textField hyperlinkType="Reference" hyperlinkTarget="Blank">
    <reportElement x="5" y="5" width="200" height="15"/>
    <textElement/>
    <textFieldExpression class="java.lang.String"><![CDATA["Click Here!"]]></textFieldExpression>
    <hyperlinkReferenceExpression><![CDATA["http://www.google.com"]]></hyperlinkReferenceExpression>
</textField>

The hyperlinkTarget attribute behaves in the same way as the target attribute in HTML.

Note that only textFields, images, and charts can be hyperlinked in this way.

like image 104
GenericJon Avatar answered Sep 17 '22 22:09

GenericJon


For some reasons, the example given didn't work. I used wayback machine and found the following snippet that worked:

<textField hyperlinkType="Reference">
  <reportElement x="5" y="95" width="300" height="15"/>
  <textFieldExpression class="java.lang.String">"  >> Click here to go to www.google.com"</textFieldExpression>
  <hyperlinkReferenceExpression>"http://www.google.com
</hyperlinkReferenceExpression>
    </textField>
like image 24
Alan Apter Avatar answered Sep 19 '22 22:09

Alan Apter