Jaspereport QRCODE html 預覽 二維碼不顯示問題解

主要 參考:https://stackoverflow.com/questions/6942373/export-to-single-html-with-embedded-images-using-jasper-report

環境:springboot2.0   Jaspereport 6.8.0  ,

在之前的增加二維碼顯示的依賴包

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.8.1</version>
</dependency>
<!--addnew -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.2</version>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
    <groupId>net.sf.barcode4j</groupId>
    <artifactId>barcode4j</artifactId>
    <version>2.1</version>
</dependency>

<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-bridge</artifactId>
    <version>1.11</version>
</dependency>
<dependency>
    <groupId>cn.lesper</groupId>
    <artifactId>iTextAsian</artifactId>
    <version>3.0</version>
</dependency>

在使用HtmlExporter生成在線預覽的時候不顯示二維碼 不顯示的原因可能

返回的img的src的找不到,應用在啓動的時候沒用註冊

net.sf.jasperreports.j2ee.servlets.ImageServlet

監聽,在沒用註冊此監聽的情況下,註冊監聽的方法待驗證,

使用img src 流的方式在頁面上顯示QRCODE解決在頁面不顯示二維碼的問題

關鍵代碼片段:

預覽:

   public void previewReport(String modelId, HttpServletResponse response) throws Exception {
      /*  //exportByJson(response,modelId,ReportUtil.PDF_TYPE,"",null);
        testjDBC(modelId,response);*/
         //jxrmxl的ReportModel 
        //生成HTML 預覽用這種模式   
        ReportModel reportModel = reportModelService.findById(modelId).get();
        //添加導出記錄
        //創建 JasperReport 對象
            JasperReport jasperReport = createJasperReport(reportModel);
            JREmptyDataSource jrEmptyDataSource = new JREmptyDataSource();
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), jrEmptyDataSource);
           ReportUtil.exportHtml(response, jasperPrint);
        //ReportUtil.exportPdf(response,"test1",jasperPrint);
    }
  /**
     * 創建 JasperReport 對象
     *
     * @param reportModel
     * @return
     * @throws Exception
     */
    @Override
    public JasperReport createJasperReport(ReportModel reportModel) throws Exception {
        JasperReport jasperReport;
        if (null == reportModel.getFileByte()) {
            throw new Exception("文件不存在!");
        }
        /*File reportFile = new File(reportModel.getFilePath());
        if (!reportFile.exists())
            throw new Exception("模板文件不存在");*/
        if (reportModel.getType() == 1) {
            //編譯,並得到jasperReport對象,相當於.jasper文件
            jasperReport = JasperCompileManager.compileReport(new ByteArrayInputStream(reportModel.getFileByte()));
        } else {
            //直接加載編譯後的模板文件
            jasperReport = (JasperReport) JRLoader.loadObject(new ByteArrayInputStream(reportModel.getFileByte()));
        }
        return jasperReport;
    }

 

 

 /**
     * 以html的形式打印報表
     *
     * @param response
     * @param jasperPrint 打印對象
     * @throws Exception
     */
    public static void exportHtml(HttpServletResponse response, JasperPrint jasperPrint) throws Exception {
        response.setContentType("text/html");//以頁面的形式打開
        /*JRHtmlExporter jrHtmlExporter = new JRHtmlExporter();
        jrHtmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false);//解決錯亂顯示img標籤問題
        jrHtmlExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
        jrHtmlExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        jrHtmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        jrHtmlExporter.exportReport();*/
        Map<String, String> images = new HashMap<>();
        HtmlExporter exporter=new HtmlExporter();
        String[] propertyNames = jasperPrint.getPropertyNames();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
       // exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getOutputStream()));
        SimpleHtmlReportConfiguration simpleHtmlReportConfiguration  = new SimpleHtmlReportConfiguration();
        //將svg轉換成img(png)格式
        simpleHtmlReportConfiguration.setConvertSvgToImage(true);
        SimpleHtmlExporterConfiguration reportExportConfiguration =new SimpleHtmlExporterConfiguration();
        exporter.setConfiguration(reportExportConfiguration );
        exporter.setConfiguration(simpleHtmlReportConfiguration);
        //ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ServletOutputStream outputStream = response.getOutputStream();
        SimpleHtmlExporterOutput simpleHtmlExporterOutput = new SimpleHtmlExporterOutput(response.getOutputStream());
        //生成imgsrc爲base64格式的資源
        simpleHtmlExporterOutput.setImageHandler(new HtmlResourceHandler() {
            @Override
            public void handleResource(String id, byte[] data) {
                System.err.println("id" + id);
                images.put(id, "data:image/png;base64," + Base64.encodeBytes(data));
            }

            @Override
            public String getResourcePath(String id) {
                return images.get(id);
            }
        });
        exporter.setExporterOutput(simpleHtmlExporterOutput);
        exporter.exportReport();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章