Skip to content

Commit

Permalink
Example of custom font usage and documentation update.
Browse files Browse the repository at this point in the history
  • Loading branch information
jadsonbr committed Dec 20, 2023
1 parent 6ad17f8 commit 582e79c
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ reporting.

.. |TestsCI| image:: https://github.com/acesseonline/pyreportjasper/workflows/Tests/badge.svg?branch=master
.. _TestsCI: https://github.com/acesseonline/pyreportjasper/actions?query=workflow%3ATests
.. |pyversions| image:: https://img.shields.io/badge/python-3.8%20or%20higher-blue.svg
.. |javaversions| image:: https://img.shields.io/badge/java-9%20or%20higher-purple.svg
.. |jvm| image:: https://img.shields.io/badge/jvm-Open%20%7C%20Oracle%20%7C%20Corretto-purple.svg
.. |platform| image:: https://img.shields.io/badge/platform-linux%20%7C%20windows%20%7C%20mac-lightgrey
.. |pyversions| image:: https://img.shields.io/badge/python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11-blue.svg
.. |javaversions| image:: https://img.shields.io/badge/java-%3E=9%20and%20%3C=20-blue.svg
.. |jvm| image:: https://img.shields.io/badge/jvm-Open%20%7C%20Oracle%20%7C%20Corretto-blue.svg
.. |platform| image:: https://img.shields.io/badge/platform-Linux%20%7C%20Windows%20%7C%20Mac-lightgrey
.. _GNU GENERAL PUBLIC LICENSE: https://github.com/acesseonline/pyreportjasper/blob/master/LICENSE
.. _Python Docs: https://pyreportjasper.readthedocs.io/en/latest/
.. |license| image:: https://img.shields.io/badge/License-GPLv3-blue.svg
Expand Down
4 changes: 4 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ Class Report
Export the report in ``pdf`` format

.. py:classmethod:: export_html()
Export the report in ``html`` format

.. py:classmethod:: export_rtf()
Export the report in ``rtf`` format
Expand Down
42 changes: 42 additions & 0 deletions example_custom_font/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Adding font

To use a font, simply install it on the operating system and use it in your report. If that doesn't work, you have the option to add a .jar file with the fonts you want to use along with the library. I've shown below how to do it.


## Step 1 - Understanding the structure

In the project repository, there's a folder with the base structure to perform the procedure. After cloning the repository, inside the folder where you cloned it, navigate to the **example_custom_font** directory

```
..
+-- example_custom_font
| +-- src
| +-- fonts
| +-- myfontcustom
| +-- CaskaydiaCoveNerdFont-Bold.ttf
| +-- CaskaydiaCoveNerdFont-BoldItalic.ttf
| +-- CaskaydiaCoveNerdFont-Italic.ttf
| +-- CaskaydiaCoveNerdFont-Regular.ttf
| +-- fontsfamily1703017000639.xml
| +-- jasperreports_extension.properties
```

## Step 2 - Changes
1) Within the **fonts** folder, change the name of the folder `myfontcustom` to the name you want to use for the font in the report.

2) Copy the `.ttf` font files to the folder from the previous step. Specifically, the font files for `Regular`, `Bold`, `BoldItalic`, and `Italic`. Modify the corresponding file names within the `fontsfamily1703017000639.xml` file in their respective tags.

3) Using your preferred editor, open the file `fontsfamily1703017000639.xml`. Replace wherever you find the name `myfontcustom` with the same name you gave in step 1 of this section.


## Step 3 - Generating `.jar` file.

Compress the `fonts` folder along with the file `jasperreports_extension.properties`. Then, change the extension from `.zip` to `.jar` and name the `.jar` file with the same name as in `step 1` of the previous section, `Step 2 - Changes`.

## Step 4 - Using the generated `.jar` with the library.

Save the generated `.jar` in a folder of your choice, and in the `resource` parameter of the library, specify the folder where the `.jar` is located. Assign the font name in the report (`.jrxml`) and make good use of it.

## Reference

For further details, visit: [https://jasperreports.sourceforge.net/sample.reference/fonts/#fonts](https://jasperreports.sourceforge.net/sample.reference/fonts/#fonts)
11 changes: 11 additions & 0 deletions example_custom_font/src/fonts/fontsfamily1703017000639.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="myfontcustom">
<normal><![CDATA[fonts/myfontcustom/CaskaydiaCoveNerdFont-Regular.ttf]]></normal>
<bold><![CDATA[fonts/myfontcustom/CaskaydiaCoveNerdFont-Bold.ttf]]></bold>
<italic><![CDATA[fonts/myfontcustom/CaskaydiaCoveNerdFont-Italic.ttf]]></italic>
<boldItalic><![CDATA[fonts/myfontcustom/CaskaydiaCoveNerdFont-BoldItalic.ttf]]></boldItalic>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
<exportFonts/>
</fontFamily>
</fontFamilies>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions example_custom_font/src/jasperreports_extension.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.ireportfamily1703017000639=fonts/fontsfamily1703017000639.xml
45 changes: 45 additions & 0 deletions example_custom_font/test/custom_font.jrxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<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="CustomFont" pageWidth="595" pageHeight="200" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"
uuid="0eb3df49-8342-4b94-9567-9afe83ee4297">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="125" splitType="Stretch">
<staticText>
<reportElement x="127" y="30" width="100" height="30" uuid="a4cf64e9-d6c3-4ce9-9039-79800533550a"/>
<textElement textAlignment="Left" verticalAlignment="Middle" markup="html">
<font fontName="myfont" size="11.5"/>
</textElement>
<text><![CDATA[caskaydia - Regular]]></text>
</staticText>
<staticText>
<reportElement x="337" y="30" width="100" height="30" uuid="ea00d75c-4bc9-44cd-9385-45f9f5dacaf0"/>
<textElement textAlignment="Left" verticalAlignment="Middle" markup="html">
<font fontName="myfont" size="11.5" isBold="true"/>
</textElement>
<text><![CDATA[caskaydia - Bold]]></text>
</staticText>
<staticText>
<reportElement x="127" y="70" width="100" height="30" uuid="52ca47a2-6c40-4163-a3b2-26098001a2d4"/>
<textElement textAlignment="Left" verticalAlignment="Middle" markup="html">
<font fontName="myfont" size="11.5" isItalic="true"/>
</textElement>
<text><![CDATA[caskaydia - Italic]]></text>
</staticText>
<staticText>
<reportElement x="337" y="70" width="100" height="30" uuid="fb69e717-6483-4039-a012-4e3d6cd1404c"/>
<textElement textAlignment="Left" verticalAlignment="Middle" markup="html">
<font fontName="myfont" size="11.5" isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[caskaydia - Bold Italic]]></text>
</staticText>
</band>
</detail>
</jasperReport>
17 changes: 17 additions & 0 deletions example_custom_font/test/test_custom_font.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
import os
from pyreportjasper import PyReportJasper

def processing():
REPORTS_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)))
input_file = os.path.join(REPORTS_DIR, 'custom_font.jrxml')
output_file = os.path.join(REPORTS_DIR, 'output', 'custom_font')
pyreportjasper = PyReportJasper()
pyreportjasper.config(
input_file,
output_file,
output_formats=["pdf"]
)
pyreportjasper.process_report()

processing()
1 change: 1 addition & 0 deletions pyreportjasper/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, config: Config, input_file):
if self.config.jvm_classpath is None:
jpype.startJVM("-Djava.system.class.loader=org.update4j.DynamicClassLoader",
"-Dlog4j.configurationFile={}".format(os.path.join(self.LIB_PATH, 'log4j2.xml')),
"--illegal-access=warn",
"-Xmx{}".format(self.config.jvm_maxmem),
classpath=classpath)

Expand Down

0 comments on commit 582e79c

Please sign in to comment.