package com.fincatto.nfe310.danfe;

import com.fincatto.nfe310.classes.nota.NFNotaProcessada;
import net.sf.jasperreports.engine.DefaultJasperReportsContext;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRPropertiesUtil;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

public abstract class NFDanfeReport {

    public static byte[] imprimirDanfe(NFNotaProcessada nota) throws IOException, ParserConfigurationException, SAXException, JRException {
        try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("danfe/danfeR3.jasper")) {
            final JRPropertiesUtil jrPropertiesUtil = JRPropertiesUtil.getInstance(DefaultJasperReportsContext.getInstance());
            jrPropertiesUtil.setProperty("net.sf.jasperreports.xpath.executer.factory", "net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory");
            return JasperExportManager.exportReportToPdf(JasperFillManager.fillReport(in, null, new JRXmlDataSource(convertStringXMl2DOM(nota.toString()), "/nfeProc/NFe/infNFe/det")));
        }
    }

    private static Document convertStringXMl2DOM(String nota) throws ParserConfigurationException, IOException, SAXException {
        try (StringReader stringReader = new StringReader(nota)) {
            InputSource inputSource = new InputSource();
            inputSource.setCharacterStream(stringReader);
            return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource);
        }
    }
}

@@ -87,16 +87,17 @@
 essada;%0A
+%0A
 import n
@@ -355,32 +355,80 @@
 perFillManager;%0A
+import net.sf.jasperreports.engine.JasperPrint;%0A
 import net.sf.ja
@@ -460,32 +460,33 @@
 RXmlDataSource;%0A
+%0A
 import org.w3c.d
@@ -660,32 +660,33 @@
 ationException;%0A
+%0A
 import java.io.I
@@ -934,28 +934,261 @@
 ption %7B%0A    
-    
+%09return JasperExportManager.exportReportToPdf(createJasperPrint(nota));%0A    %7D%0A    %0A    public static JasperPrint createJasperPrint(NFNotaProcessada nota) throws IOException, ParserConfigurationException, SAXException, JRException %7B%0A    %09
 try (InputSt
@@ -1596,46 +1596,8 @@
 urn 
-JasperExportManager.exportReportToPdf(
 Jasp
@@ -1717,17 +1717,16 @@
 e/det%22))
-)
 ;%0A      
@@ -1746,13 +1746,15 @@
   pr
-ivate
+otected
  sta

Ajuste para expor o JasperPrint para quem desejar imprimir diretamente ou gerar o PDF de outra forma