2

I'm currently editing code created by a colleague which uses Jasper Reports to create a PDF file with certain elements of which one is a bunch of html code. This html consists of text and images (both linked and in base64 format).

Almost everything works perfectly, but I have issues with the html. Most importantly the images do not show up and secondly not all html tags get processed.

Is this even possible to do with Jasper Reports?

Current code in the template where the html is supposed to go:

<textField>
    <reportElement positionType="Float" x="100" y="73" width="611" height="307" uuid="d3037b2c-6592-4d7b-b703-d64f736ca4be"/>
    <textElement markup="html"/>
    <textFieldExpression><![CDATA[$F{content_1}]]></textFieldExpression>
</textField>

Data to be processed by the template:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<compass>
    <messages>
        <message>
            <attachmentCount>0</attachmentCount>
            <attachmentString></attachmentString>
            <contactPerson>compassmanager manager</contactPerson>
            <contactPersonDepartment>Bike</contactPersonDepartment>
            <content>&lt;p&gt;&lt;b&gt;123&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;
                     img src="http://globalgamejam.org/sites/default
                     /files/styles/game_sidebar__normal
                     /public/game/featured_image/promo_5.png
                     ?itok=9dymM8JD" style="width: 135px;height:                          
                     98px;"/&gt;&lt;b&gt;&lt;br/&gt;&lt;
                     /b&gt;&lt;/p&gt;&lt;p&gt;&lt;i&gt;
                     456&lt;/i&gt;&lt;/p&gt;&lt;p&gt;&lt;
                     u&gt;789&lt;/u&gt;&lt;/p&gt;&lt;p&gt;&lt;
                     img src="data:image/jpeg;base64,/9j/
                     4AAQSkZJRgABAgAAAQABAAD......
                     sxwW6jrcshiI96diGk3OT0joAQBR0nmptgn//2Q=="
                     style="width: 25%;"/&gt;&lt;u&gt;&lt;br/&gt;&lt;
                     /u&gt;&lt;/p&gt;&lt;p&gt;&lt;strike&gt;
                     012&lt;/strike&gt;&lt;/p&gt;</content>
            <countries>BE</countries>
            <departments>Sales</departments>
            <duedate>16/11/2015</duedate>
            <messageType>ACTION</messageType>
            <title>Testerij</title>
        </message>
    </messages>
    <publishdate>16/11/2015</publishdate>
    <title>Compass 47.7</title>
</compass>

Solution

If found the html-component in Jasper Studio. There's a small discussion between me and colleagues whether or not it has been there from the beginning, but that doesn't really matter for the current situation. As Petter Friberg said below it's available from at least version 6.0 and upwards and we've been using that since we started working with Jasper Reports. Probably overlooked it.

This works for my html and the linked images in it, but doesn't yet process the base64 info. I'm currently fixing this by making sure the images get uploaded when pasted or dragged into the textfield and automatically get added as a linked image.

I'm currently switching from TextAngular to the CKEditor/ng-ckeditor combo because the lather has plugins for this purpose that work quite well.

Still some work to do before I'm finished, but this is the way to go.

Dennie
  • 743
  • 2
  • 13
  • 30
  • 1
    FYI: I have added an [answer here](http://stackoverflow.com/a/41027997/5292302) on how to use base64 image's in the html component – Petter Friberg Dec 07 '16 at 22:12

1 Answers1

1

With textField NO, markup="html" will not work on tags like table, img, style="border:1px" ecc.

Why? Because a text field displays text. Formatted text, es. <b>String</b> will work.

One way would be to check the jasper report htmlcomponent <hc:html/> this will render an image of your html or an url

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Okay, thanks for the explanation! Back to the drawing board for another solution then. – Dennie Nov 17 '15 at 15:01
  • 1
    Ok I found the html component (is available in jasper report 6.0 api), it can render an image of html, do not know if this solve your problem... check it out... – Petter Friberg Nov 19 '15 at 10:52
  • I also found it. I was planning to update my post but didn't find the time yet. Thanks anyway! – Dennie Nov 19 '15 at 10:57
  • It is possible to update your answer or post it ? We're facing the same problem with HTML image... Thank you ! – Guillaume Dec 07 '16 at 16:24
  • @PetterFriberg Do you have exemple with pure HTML and image in base 64 in same HTML tag? – Guillaume Dec 07 '16 at 16:31
  • @PetterFriberg let me know, we are losing some hair right now :'( – Guillaume Dec 07 '16 at 16:36
  • @Guillaume need to test this with the html component if you just like to use base64 data have a look at this answer http://stackoverflow.com/questions/33571520/how-can-i-use-base64-as-image-source-in-a-jasper-report-template/33572720#33572720 – Petter Friberg Dec 07 '16 at 16:36
  • @PetterFriberg no no, the HTML comes from database and can be only text, only image or both mixed, but HTML for sure. – Guillaume Dec 07 '16 at 16:38
  • maybe it's best you pass a new question, with your example create a [mcve], if I have time to night I will check it out.. Note: html component has its problem since it renders as image... you will have sizing problems. – Petter Friberg Dec 07 '16 at 16:40
  • http://stackoverflow.com/questions/41023179/export-html-text-embedded-images-with-jasperreports-htmlcomponent – Guillaume Dec 07 '16 at 17:00