How to change font in PDF generated by iReport?

9

I created a report using iReport and changed the text font to a custom font here.

The report is viewed using the iReport viewer itself and works smoothly with the font I placed, but when generating the report in PDF the font I placed is changed to the system default.

I've already placed the font in the path of iReport, and the custom font is installed in Windows. I'm using iReport 5.1.

    
asked by anonymous 22.01.2014 / 12:01

1 answer

8

I ran a test to reproduce your case as I explain in the following topics:

Including a font in iReport

First I downloaded any font, in this case called Royal Chicken .

Then,IaccessedtheiReportconfigurationscreen,specificallythefontstab:

By clicking on Install Font , I put the path of the file .ttf , according to the image:

NotethataveryimportantstepistomarkthefontasininaPDF.Seetheoptionbelow:

I do not know why this occurs, but without checking this option, Jasper Reports does not generate the PDF with the correct font, even if the "embed" field option is not enabled. In theory, it should try to put the font without embedding it, but that does not happen.

By completing the wizard , the font appears installed as the image:

Andthenyoucanselectitfromthelist,maketheimage:

After following these steps, I was able to see the properly generated PDF report within iReport .

Exporting the font for use outside iReport

On the Font Configuration screen, click Export as extension to create a .jar file containing the font.

Within the generated file, if the Embed this font in the PDF Document option has been activated, there must be an XML file (within a folder named fonts ), with content similar to this :

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>

   <fontFamily name="Royal Chicken">
       <normal><![CDATA[fonts/Royal Chicken.ttf]]></normal>
       <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
   </fontFamily>

</fontFamilies>

Configuring a Test Project

After performing these steps, I created a project in my Eclipse to test the report export.

This is pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>br.com.starcode</groupId>
  <artifactId>jasper-font-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
    <dependency>
        <groupId>net.sf.jasperreports</groupId>
        <artifactId>jasperreports</artifactId>
        <version>5.1.2</version>
    </dependency>
    <dependency>
        <groupId>fonte</groupId>
        <artifactId>fonte</artifactId>
        <version>0</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/fonte.jar</systemPath>
    </dependency>
  </dependencies>
</project>

Then, make up the above configuration, put the exported file fonte.jar in the lib folder that is at the root of the project.

I still put the report (XML) font in /src/main/resources/relatorio.jrxml .

I also created a Bean class, serving as a dummy entity to populate the report.

Finally, I executed the following method to generate the PDF with the new source:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;


public class Teste {

    public static void main(String[] args) throws JRException, FileNotFoundException {

        //lista com dados do relatório
        List<Bean> beans = new ArrayList<Bean>();

        //adicionar dados fictícios

        //encapsula lista num JRDataSource
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(beans);

        //compila relatório
        JasperReport report = JasperCompileManager.compileReport(Teste.class.getResourceAsStream("relatorio.jrxml"));

        //cria a "impressora" do jasperreports
        Map<String, Object> mapaParametros = new HashMap<String, Object>();
        JasperPrint jrprint = JasperFillManager.fillReport(report, mapaParametros, jrDataSource); 

        //cria um exportador para PDF
        JRExporter exporter = new JRPdfExporter();

        //define a saída para um arquivo
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream("teste.pdf"));
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jrprint);

        //exporta
        exporter.exportReport();

    }

}

The result can be seen in the following image:

Conclusion

If you had already followed the steps above, perhaps you just missed the option to embed the font in the PDF. Otherwise, review the steps and you'll be able to do this.

    
22.01.2014 / 19:47