Jump to content
We've recently updated our Privacy Statement, available here ×

HTML markup doesn't work in PDF


vanger

Recommended Posts

I'm trying to make a test report wich can print styled text with html markup in PDF.

I'm downloaded iReport 3.5.2 for Win, sample report from here and Adobe Reader 9.1 from there.

 

Opened sample report in iReport, set PDF viewer to AcrobatReader, RTF viewer is MSWord, XLS's is MSExcel, then ran "preview". The textField with "Here is the list with the supported HTML tags :" works good in all  viewers except PDF. Also, the markup of this text field is "styled", not "html". When i changed it to "html" and add "<html>" and "<body>" tags report become invalid cause of "&" ">" and similar constructions.

 

Then i made my own report with single text field with markup setted to html and  PDF font setted to Times-Roman.

The only text field contains:

<html><body>No style<br/>
<b>Bold only</b><br/>
<i>Italic</i><br/>
<u>underline</u><br/>
<b><i>bold italic</b></i><br/>
<i><b>Italic bold</i></b><br/>
</body></html>

 

And this report also works good in every viewer except PDF Reader. In PDF viewer only <u> tag works when running from iReport!

 

But when i call report from my application (under jasperreport-3.5.2) using JRPdfExporter and JRExporterParameter.FONT_MAP contains all font family of "Times". And here tags <b> and <u> works in PDF reader, but report looks weird:

 

No style
> Bold only
> Italic
> underline
> bold italic
> Italic bold
>

 

Also jasperreport cannot normally parse html markup if some text appears before <html> tag, but there's no big deal with it.

 

I don't know how to summorize all of this  and what's really doesn't work.

And i wounder: does the nested tags (for example <b><i>) works in PDF? Help please!



Post Edited by vanger at 07/03/2009 09:06
Link to comment
Share on other sites

  • Replies 12
  • Created
  • Last Reply

Top Posters In This Topic

It would be awesome if someone who has good experience with the JasperReports fonts provided an example, along with a clearly detailed explanation.  I have been struggling with font problems now for a couple weeks, including the EXACT same problem you have, where only the underline HTML tags work within a PDF report.  I posted that question a few weeks ago, and Lucien explained the problem with this answer:

"You need to configure a bold PDF font for the text field.

You can either do this by using the FONT_MAP export parameter (as seen here), or font extension on newer JR versions."

 

You can't be any more clear than that as far as what we need to do.  However, how do we do it (using font extension on newer JR versions)?  :-)

 

I have also had problems with linebreaking, where the PDF output was truncating parts of the field when dealing with large Strings of output.  The answer I got for that was to look at the FAQ, and then the font example from any version of JasperReports 3.1.3 onward in the /demo/sample/fonts directory. 

http://jasperforge.org/uploads/publish/jasperreportswebsite/trunk/faq.html?group_id=252#FAQ25

 

It's all very helpful, and we have since upgraded from JasperReports and iReport version 3.0.0 to 3.1.3 just to tackle these problems.  However, the example font report does not explain a lot of the questions we still have.  It does not say in general how you map a PDF font to an AWT font.  It does not give a "best practices" approach for including additional fonts with a deployed application.  It does not say when to use embedded pdf fonts, or why.  There seems to be a big mystery surrounding the details of the new font support in JR 3.1.3.

 

Link to comment
Share on other sites

Hi,

 

I'll try to answer you both, as you experienced problems related to the use of fonts with PDF.

We are preparing a better documentation for fonts, but in the meantime, here is a set of rules:

 

1. When you plan to generate PDF, make sure you have TrueType font files that you can ship with your application, as resources within classpath. They can be open source fonts or you can buy some, if you like them better. Don't even think of generating good quality PDF documents, if you don't have the fonts.

 

2. Bundle your TTF files in packages (JARs) that we call font extensions. A font extension is shown in the /demo/fonts folder of the JR project, where you can see some TTF files and two configuration files: jasperreports_extension.properties and fonts.xml. The first one has two lines introducing the font extension to JR and the second, representing the mapping of the fonts. Inside your font extension you are free to give whatever names you want to your font families, but we encourage you to give the font family the same name as it appears in the normal TTF file.

 

3. Inside report templates, for the "fontName" attribute values, always use only font family names from font extensions that you ship as part of the classpath of your application. Never use font names from system available fonts, as these are highly likely to be missing on systems where your application will run. In short, use only what you bring with you.

 

4. When you create font extensions to wrap up your TTF files, make sure PDF embedding flags are turned on. This means you are always embeddding the fonts in the generated PDF files. This is the only way to make sure the PDF will look good on any system.

 

5. If you use well defined font extensions, as recommended above, you should not experience any issue with styled text or any other markup text, when exporting to PDF, as depending on the style, the correct TTF file will be used, thanks to the mappings made in the font extension file. This means that in the report template you only need to specify the font using the fontName attribute. No other PDF related attribute would be required, all of them being deprecated by now, by font extension use.

 

Given your current experience, if you think there are more rules to add to this list, please feel free to contribute and we'll make an FAQ out of it, for the benefit of all community,

 

Thank you,

Teodor

 

Link to comment
Share on other sites

Thank you for the support.  We have also purchased the JR Ultimate Guide 3rd Edition, and find it very helpful.

Despite all this help, we are still having problems getting HTML markup (the <b> bold tag)  to work within a textField.  We have done the following per your advice above and the Simple Font Extension Tutorial in the guide:

 

  1. Upgraded to Jasper Reports 3.1.3
  2. Created a font jar containing:
    1. 8 .ttf True Type font files (2 font families - Arial and Courier New)
    2. jasperreports_extension.properties
    3. jasperfonts.xml
  3. Used these properties on the textField:
    1. isStyledText="true"
    2. markup="html"
    3. fontName="Courier New"
    4. pdfFontName="cour.ttf"
    5. isPdfEmbedded="true"

Attached are the extension.properties and jasperfonts.xml files from our fonts jar.  Are we missing something else?

Link to comment
Share on other sites

Hi,

 

You no longer need the pdfFontName and isPdfEmbedded attributes at text field level, because you already specified them in the font extension config file. Only the fontName is needed.

Also, the markup attribute deprecates the formed isStyledText attribute. isStyledText="true" is equvalent to markup="styled", but the markup attribute takes precedence, if present.

Can you upload your JRXML file and maybe the PDF you get using it?

 

Thank you,

Teodor


Link to comment
Share on other sites

teodord
Wrote:
 

Hi,

 

You no longer need the pdfFontName and isPdfEmbedded attributes at text field level, because you already specified them in the font extension config file. Only the fontName is needed.

Also, the markup attribute deprecates the formed isStyledText attribute. isStyledText="true" is equvalent to markup="styled", but the markup attribute takes precedence, if present.

Can you upload your JRXML file and maybe the PDF you get using it?

 

Thank you,

Teodor


 

Okay, I modified the JRXML file, removing all the text field attributes except markup="styled" and fontName="Courier New", and now it works!  So the textField properties look like this:

<textElement markup="styled">

    <font fontName="Courier New"/>

</textElement>

 

Thank you for the help.

Link to comment
Share on other sites

  • 1 month later...

Hello

 

I am facing similar problem with fonts. On windows machine (developer) I am using fonts that are not available in Linux machine (production environment).

I am using iReport 3.0.0. I suppose that presented solution with font extension is not applicable for this version of JasperReport, am I right ?

I was also trying to place TTF font in java classpath. Unfortunately they were only available as pdf fonts (PDF Font Name) but not as report font (Font Name). How to add external font to be available in Font Name combobox ?

How to solve problem with font issue (different fonts on windows and linux) ? Should Font Name and PDF Font Name be assigned to the same external font which is available on linux and windows ?

 

 

Regards

Lukasz

Link to comment
Share on other sites

Hi,

 

I suggest you upgrade to a version of iR/JR supporting font extensions.

Otherwise you just need to install the fonts in the Linux system. Or, write a little Java code to register those fonts with the JVM when your application runs the reports. Upgrading JR would be the easiest thing to do.

 

I hope this helps.
Teodor

 

Link to comment
Share on other sites

  • 2 weeks later...

Am I to understand that we have to specify embedded pdf fonts as font extensions in order for a report to work?

In my case I have a client executing a report but the report runs on a server so it means that I have to bundle font extensions into a jar and download them to the client machine so that they can be provided to the viewer that runs on the clinet machine? The pdf embedded fonts are specified as normal fonts (ttf) on a server in the classpath of the code executing on the server but still get the message that the fonts cannot be found.

 

Link to comment
Share on other sites

Hi, Tom

 

Please take it slowly. Explain whay kind of client applicationare you talking about. Is it a Swing desktop application?

Or is simply the browser connecting to a Web app. Also explain us how is your client application viewing the reports? In PDF format? Is the PDF file delivered by the server application, or is the client application who actually performs the export?

 

Thank you,

Teodor

 

Link to comment
Share on other sites

  • 1 month later...

I'm also having the same problem with the fonts - but I think this thread as come up with a solution to that problem.

What I'm interested in is the tags that are supported when converting from html to PDF e.g. is printing images supported?

If I include an image in the html then japser ignores and prints out the text e.g.

 

HTML input:

<html><body><p>aaa</p><p><img 

src="http://localhost:8080/image?name=logo.jpg"/></p><p>bbb</p></body></html>

 

Output is just the text without the image

 

here is the text field def:

 

 

 

<band height="125" splitType="Stretch">

<textField>

<reportElement x="0" y="0" width="555" height="125"/>

<textElement lineSpacing="Single" markup="html">

<font fontName="Courier New"/>

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$P{e}.getHTMLBody()]]></textFieldExpression>

</textField>

</band>

 

Are there any good examples on how to print HTML to PDF?

 

 

 



Post Edited by nedge at 11/24/2009 12:26
Link to comment
Share on other sites

  • 3 months later...
  • 1 month later...

Was this specifc concern ever addressed (i.e. generating a report on one machine and rendering it on another)?  Our shop has the same concern: One machine 'generates' the reports and they are viewed on a different machine which may or may not have the appropriate fonts.  You (Teodor) asked for a description of the system:  Mine is a browser connecting to the web server.  The web server generates the report (using jasper reports) and the browser on a differnt machine 'renders' the content sent back to it.  So how does packaging font extensions on the server help in this situation? 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...