How to change PDF source generated by iReport?

Asked

Viewed 5,564 times

9

I created a report using iReport and changed the source of the text to a custom font here in the company.

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

I already put the source in path iReport, and the custom font is installed in Windows. I am using iReport 5.1.

1 answer

8


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

Including a source in iReport

First I downloaded some source, in this case, called Royal Chicken.

inserir a descrição da imagem aqui

Then I downloaded the iReport configuration screen, specifically the font tab:

inserir a descrição da imagem aqui

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

inserir a descrição da imagem aqui

Note that a very important step is to mark the font as being pop-up in a PDF. See option below:

inserir a descrição da imagem aqui

I don’t know why this happens, but without checking this option, Jasper Reports does not generate the PDF with the correct source, even if the "embed" option of the field is not enabled. In theory, he should try to put the source without embedding it, but that doesn’t happen.

Concluding the Wizard, the installed font conforms the image:

inserir a descrição da imagem aqui

And then it is possible to select it in the list, conforms the image:

inserir a descrição da imagem aqui

After following these steps, I could see the report correctly generated in PDF inside the iReport.

Exporting the source for use outside of iReport

On the font configuration screen, click Export as extension to create an archive .jar containing the source.

Inside the generated file, if the option Embed this font in the PDF Document was enabled, there must be an XML file (inside a folder called fonts), with a 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>

Setting up a test project

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

This is the 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, conforms to the above configuration, I put the exported file fonte.jar in the briefcase lib which is at the root of the project.

I also put the source (XML) of the report in /src/main/resources/relatorio.jrxml.

I also created a class Bean, serving as a fictitious entity to popular the report.

Finally, I ran the following method to generate 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:

inserir a descrição da imagem aqui

Completion

If you had already followed the above steps, you might have missed just checking the option to embed the font in the PDF. Otherwise, review the steps and you will be able to do this.

  • 2

    After a brief look in the dictionary it seemed better to "embed" than to "embark" as translation for "embed", what find?

  • Embed, embark or soak, that is the question...

  • If the will of the people is that it be "embed", then it will be "embed"...

  • Step by step is right, but the error persists. I will have to see if on another machine the problem does not occur. Thank you for the answer

  • @adelmo00 Ever tried to put another source? The file may have some problem.

  • already put. I downloaded others and the problem persists

  • 1

    It’s the source they gave me. There are some fonts that work and others that don’t

  • The source may be in a format incompatible with Jasper’s Apis. If you want one last try, you can try converting the font to another format and then to TTF again. There are some online converters such as this.

  • Resuscitating topic. I was able to put the font without errors, but now the following error occurs: Font 'Custom Sans Semibold' is not available to the JVM. See the Javadoc for more Details.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.