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

HTML Markup in PDF


dac4457

Recommended Posts

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

Link to comment
Share on other sites

  • Replies 18
  • Created
  • Last Reply

Top Posters In This Topic

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 weeks later...

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 1 month later...

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:

<textField isStretchWithOverflow="true" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
mode="Opaque"
x="15"
y="388"
width="555"
height="62" backcolor="#C0C0C0" key="textField" positionType="Float"/>
<box></box>
<textElement markup="styled">
<font size="12"/> </textElement>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{heading}]]></textFieldExpression>
</textField>

 

And I followed the MarkUp example in my java code. Given below is my Java code

 

Code:
[code]
public class ReportServlet extends BaseServlet {
/** * */
private static final long serialVersionUID = 1L;


@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// URI - WebContext/rpt/{templateName}/{treeName}/{timePeriodId}/... ?
// or parameters?

String qs[] = request.getRequestURI().split("/"«»);
String templateName = "/MainReport.jasper";
String treeName = "datatree";
int timePeriodId = 0;

if (qs.length == 6) { // TODO: FIX THIS
templateName = "/" + qs[3] + ".jasper";
treeName = qs[4];
timePeriodId = Integer.parseInt(qs[5]);
}

UserSession session = UserSession.login(request, "rnarala", "rnd26575",
"de"«»);
ServletOutputStream sos = null;

try {
byte[] bArray = GenerateReport(timePeriodId, session, treeName,
templateName);
response.setContentLength(bArray.length);
response.setHeader("Content-Disposition",
"inline; filename=report.pdf"«»);
response.setContentType("application/pdf"«»);
sos = response.getOutputStream();
sos.write(bArray);
sos.flush();
} catch (Exception re) {
re.printStackTrace();
response.getOutputStream().println(re.getMessage());
} finally {
try {
if (sos != null) {
sos.flush();
sos.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}

/**
* @param timePeriodId
* The id of time period.
* @param session
* The user session that is required to build the ReportTree
* @param treeName
* The data tree name.
* @param templateName
* The JasperReport template name.
* @return PDF byte array The byte array of the generated PDF
*/
public byte[] GenerateReport(int timePeriodId, UserSession session,
String treeName, String templateName) {

InputStream is = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
String r = getClass().getResource("/MainReport.jrxml"«»).getPath();
File reports = new File(r.substring(0, r.lastIndexOf('/')));
for (File f : reports.listFiles()) {
if (f.getName().endsWith("jrxml"«»)) {
System.err.println(f);
JasperCompileManager.compileReportToFile(f.getPath(), f
.getPath().replaceFirst("jrxml", "jasper"«»));
}
}

ReportTree tree = session.getReportTree(timePeriodId, treeName);
List<ReportTreeNode> rootList = new ArrayList<ReportTreeNode>();

rootList.add(tree.root);

JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
rootList, true);

is = this.getClass().getResourceAsStream(templateName);

JasperPrint jasperPrint = JasperFillManager.fillReport(is,
new HashMap<Object, Object>(), ds);

JRPdfExporter exporter = new JRPdfExporter();

Map<Object, Object> fontMap = new HashMap<Object, Object>();
fontMap.put(new FontKey("Arial", true, false), new PdfFont(
"Helvetica-Bold", "Cp1252", false));
fontMap.put(new FontKey("Arial", false, true), new PdfFont(
"Helvetica-Oblique", "Cp1252", false));
fontMap.put(new FontKey("Arial", true, true), new PdfFont(
"Helvetica-BoldOblique", "Cp1252", false));
fontMap.put(new FontKey("Comic Sans MS", false, false),
new PdfFont("COMICBD.TTF", "Cp1252", true));

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);
exporter.exportReport();

} catch (JRException jre) {
jre.printStackTrace();
throw new RuntimeException(
"Exception Filling the template with data", jre);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Exception in GenerateReport", e);
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
return baos.toByteArray();
}
}

 

please let me know if I am missing something. ANy help in this regard is appreciated.

 

regards,

Ravi

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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:

Map<FontKey,PdfFont> fontMap = new HashMap<FontKey,PdfFont>();
// Arial is Helvetica
fontMap.put(new FontKey("Arial", true, false), new PdfFont("Helvetica-Bold", "Cp1252", false));
fontMap.put(new FontKey("Arial", false, true), new PdfFont("Helvetica-Oblique", "Cp1252", false));
fontMap.put(new FontKey("Arial", true, true), new PdfFont("Helvetica-BoldOblique", "Cp1252", false));
// Times bleibt Times
fontMap.put(new FontKey("Times New Roman", true, false), new PdfFont("Times-Bold", "Cp1252", false));
fontMap.put(new FontKey("Times New Roman", false, true), new PdfFont("Times-Italic", "Cp1252", false));
fontMap.put(new FontKey("Times New Roman", true, true), new PdfFont("Times-BoldItalic", "Cp1252", false));
// Comic !??!
fontMap.put(new FontKey("Comic Sans MS", false, false), new PdfFont("COMICBD.TTF", "Cp1252", true));
exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);

 

hth

C-Box

Link to comment
Share on other sites

  • 1 month later...
  • 11 months later...

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 3 months later...

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>"

Post Edited by PlexPro at 10/22/2009 19:34
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...