1. 程式人生 > >ireport 匯出pdf檔案 字型問題

ireport 匯出pdf檔案 字型問題

最近專案使用ireport5.6.0匯出pdf檔案,使用ireport5.6.0工具設計pdf模板後,字型不生效。在網上查了很多資料,大體看了下。解決辦法.
在專案根目錄建立目錄net.sf.jasperreports.fonts
如圖:![這裡寫圖片描述](https://img-blog.csdn.net/20180912103322720?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pkYmt3dQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

jasperreports-fonts.xml

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies> <fontFamily name="Pictonic" visible="false"> <normal> <ttf>net/sf/jasperreports/fonts/pictonic/pictonic.ttf</ttf> <svg>net/sf/jasperreports/fonts/pictonic/pictonic.svg</svg> <eot>net/sf/jasperreports/fonts/pictonic/pictonic.eot</eot
>
<woff>net/sf/jasperreports/fonts/pictonic/pictonic.woff</woff> </normal> <pdfEncoding>Identity-H</pdfEncoding> <pdfEmbedded>true</pdfEmbedded> </fontFamily> <!-- 華文宋體 --> <fontFamily name="&#21326;&#25991;&#23435;&#20307;"
>
<normal> <ttf>net/sf/jasperreports/fonts/stsong/STSONG.TTF</ttf> </normal> <bold> <ttf>net/sf/jasperreports/fonts/stsong/STSONG.TTF</ttf> </bold> <pdfEncoding>Identity-H</pdfEncoding> <pdfEmbedded>true</pdfEmbedded> </fontFamily> <fontFamily name="宋體"> <normal>net/sf/jasperreports/fonts/dejavu/simsun.ttf</normal> <bold>net/sf/jasperreports/fonts/dejavu/simsun.ttf</bold> <italic>net/sf/jasperreports/fonts/dejavu/simsun.ttf</italic> <boldItalic>net/sf/jasperreports/fonts/dejavu/simsun.ttf</boldItalic> <pdfEncoding>Identity-H</pdfEncoding> <pdfEmbedded>true</pdfEmbedded> </fontFamily> <fontFamily name="黑體" > <normal>net/sf/jasperreports/fonts/simhei.ttf</normal> <bold>net/sf/jasperreports/fonts/simhei.ttf</bold> <italic>net/sf/jasperreports/fonts/simhei.ttf</italic> <boldItalic>net/sf/jasperreports/fonts/simhei.ttf</boldItalic> <pdfEncoding>Identity-H</pdfEncoding> <pdfEmbedded>true</pdfEmbedded> </fontFamily> </fontFamilies>
涉及pdf匯出程式碼:
/**
     * 
     * <b>Description: 匯出pdf檔案</b><br> 
     * @param request
     * @param response
     * @param jrxmlSourcePath  jrxml檔案路徑
     * @param parameterMap     模板中引數
     * @param list             模板中資料來源
     * @param fileName         檔名
     * <b>Author: </b>  
     * <br><b>Date: 2018年9月4日 下午2:07:29 </b>
     */
    public static void exportPdf(HttpServletRequest request, HttpServletResponse response, String jrxmlSourcePath, Map<String, Object> parameterMap, List<?> list, String fileName)
    {
        ServletOutputStream os = null;

        try
        {
            os = response.getOutputStream();
            JasperDesign jasperDesign = JRXmlLoader.load(request.getSession().getServletContext().getRealPath(jrxmlSourcePath));
            // 編譯report
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            // 讀取資料
            JRDataSource dataSource = new JRBeanCollectionDataSource(list);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap, dataSource);
            JRPdfExporter exporter = new JRPdfExporter();
            // 寫出內容
//          exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
            // 輸出流
//          exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, os);

            exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(os));

            // 設定輸出
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
            response.setContentType("application/pdf");
            response.setHeader("Content-type", "text/html;charset=UTF-8");

            // 匯出報表
            exporter.exportReport();

            os.flush();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(null != os) 
                    os.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

    }