rafaspara2017 Posted June 11, 2021 Share Posted June 11, 2021 Bom dia a todos, com o JasperStudio 6.17.0 (incorporado no Eclipse) criei meu relatório de vendas, dentro das configurações deste relatório passei uma consulta, que faz a consulta diretamente ao banco de dados MySQL, e estou recebendo os dados corretamente com essa consulta. O que eu não sou capaz de fazer é converter o arquivo jrxml que foi gerado em um arquivo pdf. Como faço essa conversão? Eu não quero exibir este relatório e eu não quero baixá-lo também, eu só quero que esta conversão para gerar um arquivo pdf no mesmo local onde o arquivo jrxml está. Como fazer essa conversão? Link to comment Share on other sites More sharing options...
Solution rafaspara2017 Posted June 29, 2021 Author Solution Share Posted June 29, 2021 create a class that will only be responsible for converting from jrxml to pdf like this one below:package br.com.fjsistemas.relatorios;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.sql.SQLException;import javax.sql.DataSource;import org.springframework.stereotype.Component;import br.com.fjsistemas.repository.VendaRepository;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JasperCompileManager;import net.sf.jasperreports.engine.JasperExportManager;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.jasperreports.engine.JasperPrint;import net.sf.jasperreports.engine.JasperReport;@Componentpublic class ConverteJrxmlToPdf { @SuppressWarnings("unused") private VendaRepository vendaRepository; private DataSource dataSource; public ConverteJrxmlToPdf(VendaRepository vendaRepository, DataSource dataSource) { this.vendaRepository = vendaRepository; this.dataSource = dataSource; } public byte[] conversor() throws JRException, SQLException { InputStream reportJrxml = this.getClass().getResourceAsStream("/RelatorioVendas.jrxml"); JasperReport document = JasperCompileManager.compileReport(reportJrxml); JasperPrint print = JasperFillManager.fillReport(document, null, dataSource.getConnection()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JasperExportManager.exportReportToPdfStream(print, baos); byte[] relatorio = baos.toByteArray(); return relatorio; }}[/code]and in the view class, call the method via a button exportarRelatorio.addThemeVariants(ButtonVariant.LUMO_PRIMARY); exportarRelatorio.getStyle().set("margin-top", "37px"); exportarRelatorio.setWidth("180px"); FileDownloadWrapper buttonWrapper = new FileDownloadWrapper(new StreamResource("RelatorioVenda.pdf", () -> { try { return new ByteArrayInputStream(pdf.conversor()); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }));[/code] Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now