HTML Markup in PDF

I have a jasper report that has a Textfield flagged as Markup HTML. I understand from previous topics that it supports formatting tags but not the layout type of tags.

I really need a bullited list of sorts but would be satisfied with just having a <br> tag work.

I have tried both <br> and <br/> in both upper and lower case and when sending it to a PDF it will not give me a line break. I have tried replacing it with a new line character. The only thing that gives me a new line is the <li> tag. It doesn't give me the bullet mark but does give me a new line. I have tried removing all of the bullited list tags and using the various <br> tags but I do not get a new line.
I also have converted the <strong> tags to <b> making sure I have the closing tag and when sending it to a pdf I do not see any bold. Do I have to use style tags?

The HTML is stored in the database from a front end application using FCK Editor. It generates the HTML and I could use a StringBuffer to read through the HTML and replace it with simpler HTML or something that the Jasper filler/exporter would be happier with.

Any insight on rules and how the HTML should be for the Markup HTML setting to work would be greatly appreciated.

Thanks
Cheri
dac4457's picture
129
Joined: May 11 2007 - 6:20am
Last seen: 16 years 4 months ago

18 Answers:

One other question to ask about the Markup HTML field. Do I need to provide the <HTML>, <HEAD>, <BODY> tags?
I believe I tried this in the past and it didn't make a difference.
One other note when I use the Markup HTML property I don't set the isStyledText to true. I leave this at false.
Thanks
Cheri
dac4457's picture
129
Joined: May 11 2007 - 6:20am
Last seen: 16 years 4 months ago
Hi,

as I currently switch from JR 2.0.2 to JR 2.0.5 I had some troubles with replacing the old StyledText attribute against the new markup one.

I set all elements that had formerly set "isStyledText" to true to new setMarkup("html") as I just use html-syntax within report fields.

But as Teodor told me I should have uses setMarkup("styled") instead.

IReport just has "RTF" and "HTML" ... so I'm lucky to have my own plugin that does te actual work.
Just try to set the attribute within JRXML directly (via editor) and try again your sample.

My problem was solved with the tag "styled" instead "html"...

(I hope Teodor will explain the differences a bit more in detail)

hth
C-Box
C-Box's picture
23929
Joined: Jul 19 2006 - 5:58pm
Last seen: 3 weeks 3 days ago
I´m facing the same problem, do you fix it ?

When you say method setMarkup(), where you call this method ? In iReport or when you are generating the report in your application ?

Tnx
Rodrigo Kerkhoff
rKT's picture
rKT
70
Joined: Jul 4 2007 - 12:24am
Last seen: 7 years 10 months ago
The mentioned method setMarkup("styled") is direct JasperReports API....

so I have an own iReport-Plugin that can load many JRXMLs from one directory (and subdirs) and does some manipulations with the JasperDesigns, that are too much work to do it by hand.

So you can also edit your JRXML within EDITOR and change the Markup-Tag just to "styled".

hth
C-Box
C-Box's picture
23929
Joined: Jul 19 2006 - 5:58pm
Last seen: 3 weeks 3 days ago
Hello,
I went thru the forums to fing how to get the HTML markup work in PDF. I read that setting the markup="styled" in jrxml would fix it . But it did not fix it. Given below is my jrxml snippet

Code:
<br />
<textField isStretchWithOverflow="true" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" ><br />
<reportElement						<br />
	mode="Opaque"					<br />
	x="15"		<br />
	y="388"						<br />
	width="555"	<br />
	height="62"						backcolor="#C0C0C0"					key="textField"						positionType="Float"/><br />
<box></box><br />
<textElement markup="styled"><br />
      <font size="12"/>					</textElement><br />
<textFieldExpression   class="java.lang.String"><br />
<![CDATA[$F{heading}]]></textFieldExpression><br />
</textField><br />
</td></tr></tbody></table><br />
<br />
And I followed the MarkUp example in my java code. Given below is my Java code<br />
<br />
<table align="center" border="0" cellpadding="3" cellspacing="1" width="90%"><tbody><tr><td><b>Code:</b></td></tr><tr><td><pre><br />
public class ReportServlet extends BaseServlet {<br />
	/** * */<br />
	private static final long serialVersionUID = 1L;<br />
<br />
<br />
	@Override<br />
	public void doGet(HttpServletRequest request, HttpServletResponse response)<br />
			throws ServletException, IOException {<br />
<br />
		// URI - WebContext/rpt/{templateName}/{treeName}/{timePeriodId}/... ?<br />
		// or parameters?<br />
<br />
		String qs[] = request.getRequestURI().split("/"«»);<br />
		String templateName = "/MainReport.jasper";<br />
		String treeName = "datatree";<br />
		int timePeriodId = 0;<br />
<br />
		if (qs.length == 6) { // TODO: FIX THIS<br />
			templateName = "/" + qs[3] + ".jasper";<br />
			treeName = qs[4];<br />
			timePeriodId = Integer.parseInt(qs[5]);<br />
		}<br />
		<br />
		UserSession session = UserSession.login(request, "rnarala", "rnd26575",<br />
				"de"«»);<br />
		ServletOutputStream sos = null;<br />
		<br />
		try {<br />
			byte[] bArray = GenerateReport(timePeriodId, session, treeName,<br />
					templateName);<br />
			response.setContentLength(bArray.length);<br />
			response.setHeader("Content-Disposition",<br />
					"inline; filename=report.pdf"«»);<br />
			response.setContentType("application/pdf"«»);<br />
			sos = response.getOutputStream();<br />
			sos.write(bArray);<br />
			sos.flush();<br />
		} catch (Exception re) {<br />
			re.printStackTrace();<br />
			response.getOutputStream().println(re.getMessage());<br />
		} finally {<br />
			try {<br />
				if (sos != null) {<br />
					sos.flush();<br />
					sos.close();<br />
				}<br />
			} catch (Exception e1) {<br />
				e1.printStackTrace();<br />
			}<br />
		}<br />
	}<br />
<br />
	/**<br />
	 * @param timePeriodId<br />
	 *            The id of time period.<br />
	 * @param session<br />
	 *            The user session that is required to build the ReportTree<br />
	 * @param treeName<br />
	 *            The data tree name.<br />
	 * @param templateName<br />
	 *            The JasperReport template name.<br />
	 * @return PDF byte array The byte array of the generated PDF<br />
	 */<br />
	public byte[] GenerateReport(int timePeriodId, UserSession session,<br />
			String treeName, String templateName) {<br />
<br />
		InputStream is = null;<br />
		ByteArrayOutputStream baos = null;<br />
		try {<br />
			baos = new ByteArrayOutputStream();<br />
			String r = getClass().getResource("/MainReport.jrxml"«»).getPath();<br />
			File reports = new File(r.substring(0, r.lastIndexOf('/')));<br />
			for (File f : reports.listFiles()) {<br />
				if (f.getName().endsWith("jrxml"«»)) {<br />
					System.err.println(f);<br />
					JasperCompileManager.compileReportToFile(f.getPath(), f<br />
							.getPath().replaceFirst("jrxml", "jasper"«»));<br />
				}<br />
			}<br />
<br />
			ReportTree tree = session.getReportTree(timePeriodId, treeName);<br />
			List<ReportTreeNode> rootList = new ArrayList<ReportTreeNode>();<br />
<br />
			rootList.add(tree.root);<br />
<br />
			JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(<br />
					rootList, true);<br />
<br />
			is = this.getClass().getResourceAsStream(templateName);<br />
<br />
			JasperPrint jasperPrint = JasperFillManager.fillReport(is,<br />
					new HashMap<Object, Object>(), ds);<br />
<br />
			JRPdfExporter exporter = new JRPdfExporter();<br />
<br />
			Map<Object, Object> fontMap = new HashMap<Object, Object>();<br />
			fontMap.put(new FontKey("Arial", true, false), new PdfFont(<br />
					"Helvetica-Bold", "Cp1252", false));<br />
			fontMap.put(new FontKey("Arial", false, true), new PdfFont(<br />
					"Helvetica-Oblique", "Cp1252", false));<br />
			fontMap.put(new FontKey("Arial", true, true), new PdfFont(<br />
					"Helvetica-BoldOblique", "Cp1252", false));<br />
			fontMap.put(new FontKey("Comic Sans MS", false, false),<br />
					new PdfFont("COMICBD.TTF", "Cp1252", true));<br />
			<br />
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);<br />
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);<br />
			exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);	<br />
			exporter.exportReport();<br />
<br />
		} catch (JRException jre) {<br />
			jre.printStackTrace();<br />
			throw new RuntimeException(<br />
					"Exception Filling the template with data", jre);<br />
		} catch (Exception e) {<br />
			e.printStackTrace();<br />
			throw new RuntimeException("Exception in GenerateReport", e);<br />
		} finally {<br />
			try {<br />
				if (is != null) {<br />
					is.close();<br />
				}<br />
			} catch (Exception e1) {<br />
				e1.printStackTrace();<br />
			}<br />
		}<br />
		return baos.toByteArray();<br />
	}<br />
}<br />
</td></tr></tbody></table><br />
<br />
please let me know if I am missing something. ANy help in this regard is appreciated.<br />
<br />
regards,<br />
Ravi
rnarala's picture
521
Joined: Apr 30 2008 - 3:33am
Last seen: 15 years 4 months ago
What's the content of the field:

$F{heading}

??

C-Box
C-Box's picture
23929
Joined: Jul 19 2006 - 5:58pm
Last seen: 3 weeks 3 days ago
it is html content

<h1>This is a header text</h1>
rnarala's picture
521
Joined: Apr 30 2008 - 3:33am
Last seen: 15 years 4 months ago
Uii... have you already checked the StyledText Sample provided with full JasperReports Download package????

There are just a few html tags supported by StyledText in JR. <h1> is not one of them. :-(
Just take a look at it.
When I remember right we just use <b> for bold, <i> for italic, <u> for underline and some <font> tags (size, color, family) to render html-styled content. (I think <li> is also supported, but has a bug when line breaks)

hth
C-Box
C-Box's picture
23929
Joined: Jul 19 2006 - 5:58pm
Last seen: 3 weeks 3 days ago
even the italics does not work. I tried it with
<i>This is a italics text<i>

it rendered a plain text
rnarala's picture
521
Joined: Apr 30 2008 - 3:33am
Last seen: 15 years 4 months ago
Hi,

You should start with some sample text fields taken from the /demo/samples/markup sample provided with the project.
If you see it working there, then try to do only slight modifications to it and understand when it no longer works.

Why? Because everything you have tried so far has been wrong for one reason or another.
First you have tried H1 tags in styled text (not supported, as indicated in the /demo/samples/styledtext).
Next, you have tried italic text, but in PDF, where special PDF font settings have to be made. This is also shown in those samples and you would have seen them if you would have paid more attention to them.

I hope this helps.
Teodor
teodord's picture
53198
Joined: Jun 30 2006 - 9:00am
Last seen: 1 day 1 hour ago
First: Do what Teodor told you! Check the sample (as I also adviced)!!! You'll understand more details than trial & error!

and next...

it should be:
<i>This is a italics text</i>


and before you call the exporter you should use this to map the system fonts to the PDF fonts:
Code:
<br />
Map<FontKey,PdfFont> fontMap = new HashMap<FontKey,PdfFont>();<br />
      // Arial is Helvetica<br />
      fontMap.put(new FontKey("Arial", true, false), new PdfFont("Helvetica-Bold", "Cp1252", false));<br />
      fontMap.put(new FontKey("Arial", false, true), new PdfFont("Helvetica-Oblique", "Cp1252", false));<br />
      fontMap.put(new FontKey("Arial", true, true), new PdfFont("Helvetica-BoldOblique", "Cp1252", false));<br />
      // Times bleibt Times<br />
      fontMap.put(new FontKey("Times New Roman", true, false), new PdfFont("Times-Bold", "Cp1252", false));<br />
      fontMap.put(new FontKey("Times New Roman", false, true), new PdfFont("Times-Italic", "Cp1252", false));<br />
      fontMap.put(new FontKey("Times New Roman", true, true), new PdfFont("Times-BoldItalic", "Cp1252", false));<br />
      // Comic !??!<br />
      fontMap.put(new FontKey("Comic Sans MS", false, false), new PdfFont("COMICBD.TTF", "Cp1252", true));<br />
      exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);</td></tr></tbody></table><br />
<br />
hth<br />
C-Box
C-Box's picture
23929
Joined: Jul 19 2006 - 5:58pm
Last seen: 3 weeks 3 days ago

It seems that the html support when using markup="html" is extremely limited. Even text alignment does not work. Is this the case?

joeherbet's picture
139
Joined: Jul 23 2008 - 3:57am
Last seen: 9 years 9 months ago

Joe wrote:

It seems that the html support when using markup="html" is extremely limited. Even text alignment does not work. Is this the case?

You're right, text alignment is ignored when using markup="html".  See this thread for some details on this.

Regards,

Lucian

lucianc's picture
86885
Joined: Jul 17 2006 - 1:10am
Last seen: 2 hours 14 min ago

Hi ,

I am new to this.From where i can find the /demo/samples/markup in the project.

 

Thanks.

Madhavan

madhavanasokan's picture
Joined: Jun 12 2009 - 3:43am
Last seen: 14 years 3 months ago

Can anyone tell me why the HTML underline tag does work in my textField, but the HTML bold tag does not?

 

<u>MyText</u> works

<b>MyText</b> does not work

 

We are using JasperReports 3.0, iReport 3.0, JDK 1.5.  The properties of the textField are:

Font Name = Courier New

PDF Font Name = Courier

Markup = HTML

IsStyledText not checked

PDF Embedded not checked.

 

We are using the JasperRunManager.runReportToPDF() method to stream the report.

wjones14's picture
1013
Joined: Jul 19 2006 - 11:59pm
Last seen: 2 years 1 month ago

madhavanasokan
Wrote:

Hi ,

I am new to this.From where i can find the /demo/samples/markup in the project.

 

Thanks.

Madhavan

See the READ THIS FIRST! - Documentation and Samples Here! topic found on the top of the JR forum page.

Regards,

Lucian



Post Edited by lucianc at 07/01/2009 15:31
lucianc's picture
86885
Joined: Jul 17 2006 - 1:10am
Last seen: 2 hours 14 min ago

wjones14
Wrote:

Can anyone tell me why the HTML underline tag does work in my textField, but the HTML bold tag does not?

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.

Regards,

Lucian

lucianc's picture
86885
Joined: Jul 17 2006 - 1:10am
Last seen: 2 hours 14 min ago

Here is an example of a text box which is a combination of a parameter, static text, and a field, where I wanted the field to have distinct attributes, and the output is PDF.  The default PDF-font is Times-Roman, size 16 points, without other special characteristics.

 

Text Field Expression (see Code box below).

 

Here is the result:

10082 : Alpha Incorporated

Code:
Text Field Expression:
 
$P{customerId} +
" : <style isBold=\"true\" pdfFontName=\"Times-BoldItalic\">" +
$F{SoldToName} +
 "</style>"</td></tr></tbody></table><br><br>Post Edited by PlexPro at 10/22/2009 19:34
PlexPro's picture
566
Joined: May 11 2008 - 11:34pm
Last seen: 4 years 6 months ago
Feedback