How to print html content to a PDF with jasper reports? - html

I'm using Primefaces 5.3 in a web app along with the Primefaces <p:editor> which saves its data to a database table. I would like to export/print to PDF the value data from the database with the format being ready instead of printing out codes like
<br> or <ul> <li>, etc.
I'm using Jasper reports to create the report and then exporting to pdf, so I need to create "something" that can be printed with jasper reports that will take care of the html codes.

You can use the HtmlComponent <hc:html/>
Example
<componentElement>
<reportElement x="0" y="100" width="230" height="110" backcolor="#ADD8E6" uuid="332dd551-e8cd-4cb0-a11f-7325f481017b"/>
<hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="FillFrame" horizontalAlign="Left" verticalAlign="Top">
<hc:htmlContentExpression><![CDATA["Hello<br/>World"]]></hc:htmlContentExpression>
</hc:html>
</componentElement>
It will generate an image of your html.

Related

rendering xml (reversing pdftohtml)

I have a document (shown below), created with pdftohtml (linux package/command). It has enough information to render a page. For example, see the page's height & width attributes. Also some textboxes with coordinates. I am thinking how should I approach the task of rendering it back to a readable, pdf-like view. Appreciate any pointer.
<page number="112" position="absolute" top="0" left="0" height="992" width="756">
<text top="78" left="108" width="540" height="21" font="22">This configures networking routes such that you have unique IP addresses assigned</text>
<text top="98" left="108" width="118" height="21" font="22">to every service of </text>
<text top="101" left="226" width="135" height="15" font="25">type: LoadBalancer</text>
<text top="98" left="360" width="4" height="21" font="22">.</text>
<text top="132" left="108" width="127" height="28" font="33"><b>Configuring DNS</b></text>
</page>
The whole point is it should be a HTML view.
So for example I print this Page to a PDF (Top) and run the pdftohtml command I get the output (Middle) with lower browser HTML view.
Now I can print to PDF again, However there are penalties for round tripping PDF some links will work but not other links (like that package one), or some other objects NOTE the line starting "Also" may need co-ordinate re-adjustments. Most easily after correction you could use a single command line to Chrome/Edge --headless "Print-to-PDF in place of my manual reprint.

Convert mxGraphModel XML to PNG on server side

I'm trying to integrate mxGraph in a small project and I would like to convert XML-files (with a mxGraphModel conatiner in it) stored on the server to PNG and PDF (and serve them as download).
I have the XML files on the server ready, but i can't find a possibility to convert them on server side (in a JAVA surflet) to PNG.
Here a sample XML-File:
<mxGraphModel>
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="2" value="Hello" parent="1" vertex="1">
<mxGeometry x="20" y="20" width="80" height="30" as="geometry"/>
</mxCell>
<mxCell id="3" value="World" parent="1" vertex="1">
<mxGeometry x="200" y="150" width="80" height="30" as="geometry"/>
</mxCell>
<mxCell id="4" value="" parent="1" source="2" target="3" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
Is there a way to do this on the server side ?
Best Regards,
Jens
I recently did this using :
the Xml2Svg demo program included in the draw.io source code, it converts mxgraph xml to svg file. Be carreful simple shapes are converted well, but not complex draw.io custom shapes
Batik library to convert svg to a PNG file
PS : I also included this process into a mvn pdf:pdf custom Mojo with no success : the Batik library are in conflict between Batik and the pdf mojo
Didier

Style Tag & <br> tag don't work together in Jasper Report

Currently, I have a text field in my jasper report with multiple style tags for different parts of the text.
<textField>
<reportElement x="9" y="2" width="403" height="126"/>
<textElement markup="styled"/>
<textFieldExpression><![CDATA['Hi <style size="32">User</style>How <br> <style size="12">are</style> you? ']]></textFieldExpression>
</textField>
Is there a work around to work with styled markup as well as html markup?

How do I set the left margin for a html paragraph shown in a Jasperreports TextField

I am making a subreport using Jaspersoft Studio 6.2.1. The subreport only contains a TextField defined as follows:
<band height="105">
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="505" height="47" uuid="03b4eae9-6dd0-4fdb-b2c5-5d8a5ba3b71d"/>
<textElement textAlignment="Justified" markup="html">
<font fontName="Verdana" size="10"/>
<paragraph lineSpacing="1_1_2" leftIndent="0" spacingAfter="250"/>
</textElement>
<textFieldExpression><![CDATA["Some text"]]></textFieldExpression>
</textField>
</band>
It works properly, making the PDF view with whatever content I put. Although, it can render html tags like <b>, <ul> or <u>, it doesn't render the html paragrahp (<p>) tag as it should when I set a style for it. Below, there are two images showing what I'm trying to achive and the end result.
As can be seen, both paragraphs are shown without any margin. I know I can set a style for all paragraphs inside the textField element, but is there any way to achieve that any paragraph has its own style according to its style attribute?
What you are experiencing has to do with the fact that, internally, JasperReports uses just a javax.swing.JEditorPane for processing HTML text that, unfortunately, supports only HTML 3.2 even in more recent Java versions.
So you should not rely on inline styling when writing HTML markup.

How to prevent wrapping of a line of text in Jasperreports

I have a line of text which reads "International ID". But whenever I am to display in in my Jasperreport to PDF, it will sometimes be spread across two lines like this:
"International
ID". Is there a way to prevent wrapping in like in html I would use
<span style="white-space: nowrap">Long line with no breaks</span>
Give sufficient width to the field "International ID" so that it won't wrap up in any exported format. Also check this with different languages (If you are supporting multiple languages).
About the HTML text part, you can style textField elements using different markups like "Styled", "HTML", "RTF" etc. See Style a text field in JasperReports for details.
However, HTML markup supports very basic HTML tags like <p>, <br>, <color> etc. It won't support style="white-space: nowrap".
So you can either:
Increase the width of textField OR
Let the text cut-off by setting isStretchWithOverflow="false" (This is not recommended mostly)
My approach was this: (Excel nowrap in Jasper Report)
<reportElement key="staticText-5" mode="Opaque" x="0" y="70" width="100" height="15" backcolor="#FFFFFF">
<property name="net.sf.jasperreports.text.truncate.at.char" value="true"/>
<property name="net.sf.jasperreports.export.xls.wrap.text" value="false"/>
</reportElement>
Jasperreports appears to be honoring combined with <textElement markup="html"/>
Example:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.20.0.final using JasperReports Library version 6.1.1 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="nbsp" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="77c97c1b-90ba-40a8-a5c3-f88b8a48a585">
<title>
<band height="130">
<textField>
<reportElement x="69" y="23" width="141" height="30" uuid="cd0cc386-988e-422f-83ba-744ee7d32d7d"/>
<textFieldExpression><![CDATA["The quick brown fox jumps over the lazy dog"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="70" y="80" width="140" height="30" uuid="9b26682f-8ebd-4b40-87d3-f1c9827eedfd"/>
<textElement markup="html"/>
<textFieldExpression><![CDATA["The quick brown fox jumps over the lazy dog"]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>