Export custom HTML template

后端 未结 1 1297
温柔的废话
温柔的废话 2021-01-21 04:58

Background

Exporting to HTML generates the following:


    
        
        

        
相关标签:
1条回答
  • 2021-01-21 05:54

    The HTML_HEADER parameter of JRHtmlExporterParameter was replaced with:

    HtmlExporterConfiguration.getHtmlHeader()

    For example:

    public byte[] exportHtml(final JasperPrint print) {
        final Exporter exporter = new HtmlExporter();
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
    
        exporter.setConfiguration(createHtmlConfiguration());
        exporter.setExporterOutput(new SimpleHtmlExporterOutput(out));
        exporter.setExporterInput(new SimpleExporterInput(print));
        exporter.exportReport();
    
        return out.toByteArray();
    }
    
    private HtmlExporterConfiguration createHtmlConfiguration()
            throws IOException {
        SimpleHtmlExporterConfiguration shec
                = new SimpleHtmlExporterConfiguration();
    
        shec.setHtmlHeader(getHtmlHeader());
        shec.setHtmlFooter(getHtmlFooter());
    
        return shec;
    }
    
    private String getHtmlHeader() {
        StringBuffer sb = new StringBuffer();
        sb.append("<html>");
        sb.append("<head>");
        sb.append("  <title></title>");
        sb.append("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>");
        sb.append("  <style type=\"text/css\">");
        sb.append("    a {text-decoration: none}");
        sb.append("  </style>");
        sb.append("</head>");
        sb.append("<body text=\"#000000\" link=\"#000000\" alink=\"#000000\" vlink=\"#000000\">");
        sb.append("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
        sb.append("<tr><td align=\"left\">");
    
        return sb.toString();
    }
    
    private String getHtmlFooter() {
      // Close the opening tags from getHtmlHeader()...
    }
    

    Even better would be to use external resources (such as a database, file, or web page) for the HTML content, rather than hard-coded strings.

    0 讨论(0)
提交回复
热议问题