3
respostas

Jasper gera local, mas não em homologação

Fiz assim e não roda no servidor, somente local também

package br.com.ghnetsoft.comprasfood.relatorio.service;

import static br.com.ghnetsoft.principal.util.DataUtil.DD_MM_YYYY;
import static br.com.ghnetsoft.principal.util.DataUtil.converterLocalDateParaString;
import static br.com.ghnetsoft.principal.util.DataUtil.converterLocalDateTimeJava;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.com.ghnetsoft.comprasfood.model.requisicao.Requisicao;
import br.com.ghnetsoft.comprasfood.model.requisicaoitem.RequisicaoItem;
import br.com.ghnetsoft.comprasfood.relatorio.dto.RequisicaoItemDTO;
import br.com.ghnetsoft.comprasfood.repository.requisicao.RequisicaoRepository;
import br.com.ghnetsoft.principal.dto.ArquivoDTO;
import br.com.ghnetsoft.principal.exception.GeralException;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

@Service
public class RequisicaoService {

    @Autowired
    private RequisicaoRepository repository;
    private BigDecimal total;

    public ArquivoDTO imprimir(Long idRequisicao) {
        try {
            Optional<Requisicao> requisicaoExiste = repository.findById(idRequisicao);
            if (requisicaoExiste.isPresent()) {
                total = new BigDecimal("0");
                JRDataSource dataSource = new JRBeanCollectionDataSource(inserirLista(requisicaoExiste));
                Map<String, Object> parametros = new HashedMap<String, Object>();
                parametros.put("lojaOrigem", requisicaoExiste.get().getLojaOrigem().getNome());
                parametros.put("lojaDestino", requisicaoExiste.get().getLojaDestino().getNome());
                parametros.put("funcionario", requisicaoExiste.get().getFuncionario().getNome());
                parametros.put("dataEntrega", converterLocalDateParaString(
                        converterLocalDateTimeJava(requisicaoExiste.get().getDataEntrega()), DD_MM_YYYY));
                parametros.put("natureza", requisicaoExiste.get().getObservacao());
                parametros.put("numeroRequisicao", requisicaoExiste.get().getNumero());
                parametros.put("status", requisicaoExiste.get().getStatus().getDescricao());
                parametros.put("total", total);
                byte[] bytes = JasperRunManager.runReportToPdf(
                        this.getClass().getClassLoader().getResourceAsStream("relatorio/requisicao_compra.jasper"),
                        parametros, dataSource);
                return ArquivoDTO.builder().contentType("application/pdf").arquivo(bytes).nome("teste").build();
            }
        } catch (JRException e) {
            e.printStackTrace();
            throw new GeralException("Erro ao gerar relatório de requisição de compras !");
        }
        return ArquivoDTO.builder().build();
    }

    private Collection<RequisicaoItemDTO> inserirLista(Optional<Requisicao> requisicaoExiste) {
        Collection<RequisicaoItemDTO> itens = new ArrayList<>();
        for (RequisicaoItem item : requisicaoExiste.get().getRequisicoesItens()) {
            itens.add(RequisicaoItemDTO.builder().insumo(item.getInsumo().getNome()).quantidade(item.getQuantidade())
                    .quantidadeAprovada(item.getQuantidadeAprovada()).total(item.getTotal())
                    .ultimoCusto(item.getUltimoCusto()).unidade(item.getUnidade().getNome()).build());
            total = total.add(item.getTotal());
        }
        return itens;
    }
}
3 respostas

log

2021-01-13 19:35:01.519 ERROR 1 --- [nio-8600-exec-2] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/modulo-relatorio-api] threw exception [Handler dispatch failed; nested exception is java.lang.InternalError: java.lang.reflect.InvocationTargetException] with root cause
java.lang.NullPointerException: null
    at java.desktop/sun.awt.FontConfiguration.getVersion(Unknown Source) ~[na:na]
    at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(Unknown Source) ~[na:na]
    at java.desktop/sun.awt.FontConfiguration.init(Unknown Source) ~[na:na]
    at java.desktop/sun.awt.X11FontManager.createFontConfiguration(Unknown Source) ~[na:na]
    at java.desktop/sun.font.SunFontManager$2.run(Unknown Source) ~[na:na]
    at java.base/java.security.AccessController.doPrivileged(Native Method) ~[na:na]
    at java.desktop/sun.font.SunFontManager.<init>(Unknown Source) ~[na:na]
    at java.desktop/sun.awt.FcFontManager.<init>(Unknown Source) ~[na:na]
    at java.desktop/sun.awt.X11FontManager.<init>(Unknown Source) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:na]
    at java.desktop/sun.font.FontManagerFactory$1.run(Unknown Source) ~[na:na]
    at java.base/java.security.AccessController.doPrivileged(Native Method) ~[na:na]
    at java.desktop/sun.font.FontManagerFactory.getInstance(Unknown Source) ~[na:na]
    at java.desktop/java.awt.Font.<init>(Unknown Source) ~[na:na]
    at java.desktop/java.awt.Font.createFont(Unknown Source) ~[na:na]
    at net.sf.jasperreports.engine.fonts.AwtFontManager.getAwtFont(AwtFontManager.java:89) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fonts.SimpleFontFace.loadFont(SimpleFontFace.java:181) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fonts.SimpleFontFace.setTtf(SimpleFontFace.java:162) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontFace(SimpleFontExtensionHelper.java:390) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontFamily(SimpleFontExtensionHelper.java:311) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.parseFontExtensions(SimpleFontExtensionHelper.java:259) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontExtensions(SimpleFontExtensionHelper.java:230) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.loadFontExtensions(SimpleFontExtensionHelper.java:187) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.ensureFontExtensions(FontExtensionsRegistry.java:93) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.getExtensions(FontExtensionsRegistry.java:57) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:134) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.util.JRStyledTextParser.<clinit>(JRStyledTextParser.java:86) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:116) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:79) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]
    at net.sf.jasperreports.engine.fill.JRFiller.createBandReportFiller(JRFiller.java:251) ~[jasperreports-6.16.0.jar!/:6.16.0-48579d909b7943b64690c65c71e07e0b80981928]

Parece um bug no openjdk usado na imagem docker: https://github.com/AdoptOpenJDK/openjdk-docker/issues/75

Aparentemente o Alpine linux deve estar faltando essas fontes.

E como faz para instalar as fontes ?