0

I'm creating a jasper report template from a page where I can upload some files as attachment. The requirement is to generate a report from the page and add a URL for the file into the generated report. If the user clicks the URL he can download the file.

When we upload a file to our system we generate a unique filename for the uploaded file.

Example: If the uploaded file was "Jaspersoft-Studio-User-Guide.pdf" the file will have a new name: "03ff52a3-c165-4f71-9d27-eedac395a41b"

I need a hyperlink which works similar to the browser file download so by accessing the file with URL: "www.test.com/03ff52a3-c165-4f71-9d27-eedac395a41b" The downloaded file name will be "Jaspersoft-Studio-User-Guide.pdf" instead of "03ff52a3-c165-4f71-9d27-eedac395a41b"

Is this possible?

I tried to use:

<textField isStretchWithOverflow="true" isBlankWhenNull="true" hyperlinkType="RemotePage" hyperlinkTarget="Blank">
    <reportElement x="0" y="0" width="345" height="20" isRemoveLineWhenBlank="true" forecolor="#5A82C3" uuid="c2621a64-81fe-4847-9f25-48292d0beb46"/>
    <textElement verticalAlignment="Middle" markup="html">
        <font fontName="GT-Pressura"/>
    </textElement>
    <textFieldExpression><![CDATA[$F{filename} +"<style size='4'>" +$F{url}+ "</style>"]]></textFieldExpression>
    <anchorNameExpression><![CDATA[$F{filename}]]></anchorNameExpression>
    <hyperlinkReferenceExpression><![CDATA[$F{url}]]></hyperlinkReferenceExpression>
    <hyperlinkTooltipExpression><![CDATA[$F{filename}]]></hyperlinkTooltipExpression>
</textField>

and with html as it is used in the browser:

<textField>
    <reportElement x="290" y="90" width="254" height="20" uuid="5124e8e6-831b-49f8-9f4a-b920f6fb2277"/>
    <textElement markup="html"/>
    <textFieldExpression><![CDATA["<a  href='localhost:8080/bb7162d3-83e5-4b7a-b65b-eaa5930924d1'  download='Jaspersoft-Studio-User-Guide.pdf'> Jaspersoft-Studio-User-Guide.pdf </a>"]]></textFieldExpression>
</textField>

I expect from the link to works as a browser link so the name should be whatever I specify in the download field.

Is this possible to achieve with hyperlink or with text field with markup="html" or I have to create a filter which changes the downloaded file name?

Alex K
  • 22,315
  • 19
  • 108
  • 236
bazsoja
  • 85
  • 1
  • 14
  • Related, maybe even duplicate [How can I add a button in jasper report?](https://stackoverflow.com/q/34673513/5292302), can you specify some more, what do you see with your current solution?, what is the problem? – Petter Friberg Jun 20 '19 at 09:39
  • Unfortunately it doe snot solves my problem. My problem is that if I access the link (actually it is a file download link) **www.test.com/03ff52a3-c165-4f71-9d27-eedac395a41b** I am redirected to a page where I can download the file with name: **03ff52a3-c165-4f71-9d27-eedac395a41b**. I have to achieve that when I access the page **www.test.com/03ff52a3-c165-4f71-9d27-eedac395a41b** to download the file with name **Jaspersoft-Studio-User-Guide.pdf** – bazsoja Jun 20 '19 at 13:48
  • I'm not sure if understands it correctly, but it sounds like a backend problem it needs to set `Content-Disposition` see for example https://stackoverflow.com/a/3102276/5292302, hence it's not much you can in jasper-report, you need to fix the backend. – Petter Friberg Jun 20 '19 at 13:59
  • If I access the URL from the web page with this link: ` Jaspersoft-Studio-User-Guide.pdf ` the file is downloaded correctly. I fixed the issue by writing a new resource just for serving these attachments but it feels incorrect that I can access the file from the web page with the URL but I can't from the PDF. – bazsoja Jun 20 '19 at 14:06
  • The [download attribute](https://www.w3schools.com/tags/att_a_download.asp) is a new attribute ("HTML5") I'm not surprised it's not supported well, I think your fix is the way to go, fix backend...(also in some old browser like IE12 it would not work) – Petter Friberg Jun 20 '19 at 14:14
  • Thanks for your help. I will try to pass through this solution in the review. I will leave this question open since the question is about creating file download link in jasper reports. Hopefully in the feature there will be some better solution. – bazsoja Jun 20 '19 at 14:27

0 Answers0