开发者

Jasper Report: how to customize JasperViewer so that it could only export in one format?

our customer is loving the Jasper viewer, but we have a pr开发者_开发知识库oblem. It exports data to several different formats (PDF, Excel, CSV, HTML, etc.), but our customer only wants to export to PDF.

How can we customize the Jasper Viewer so that the only format which our users can choose to export data is PDF?


I've found a solution that, in my opinion is just terrible but worked on my case.

Well: reading the source code of the JasperViewer class, I found a protected field named viewer on that class.

So, all I had to do was write a code like this:

Field jrViewerField;
            try {
                jrViewerField = viewer.getClass().getDeclaredField("viewer");

                jrViewerField.setAccessible(true);
                JRViewer jrViewer = (JRViewer) jrViewerField.get(viewer);
                List<JRSaveContributor> savers = new ArrayList<JRSaveContributor>();
                for (JRSaveContributor sc : jrViewer.getSaveContributors()) {

                        savers.add(sc);

                }

                for (JRSaveContributor sc : savers) {
                    if (! sc.getClass().getName().toLowerCase().contains("pdf")) {
                        jrViewer.removeSaveContributor(sc);
                    }
                }


            } catch (Exception ex) {
              ex.printStackTrace();
            } 

It's not a beautiful solution, but at least it worked with the 3.7.1 version of Jasper Reports. It have NO WARRANTY that may work with another versions of the system, so I highly discourage anyone to use this solution, only if that is your last resource.


Why not set a SaveContributor that only allows PDFs? E. g. the JRPdfSaveContributor.

    JRViewer viewer = new JRViewer(jrPrint);
    viewer.setSaveContributors(new JRSaveContributor[] { new JRPdfSaveContributor(Locale.getDefault(), null) });


open up viewReportsBean.xml from \apache-tomcat-7.0.12\webapps\jasperserver\WEB-INF\flows

keep scrolling down to the end of the document you will see the following lines

<entry key="pdf" value-ref="pdfExporterConfiguration"/>
     <entry key="xls" value-ref="xlsExporterConfiguration"/> 
    <entry key="csv" value-ref="csvExporterConfiguration"/>
    <entry key="docx" value-ref="docxExporterConfiguration"/>
    <entry key="rtf" value-ref="rtfExporterConfiguration"/>
    <entry key="swf" value-ref="swfExporterConfiguration"/>
    <entry key="odt" value-ref="odtExporterConfiguration"/>
    <entry key="ods" value-ref="odsExporterConfiguration"/>
    <entry key="xlsx" value-ref="xlsxExporterConfiguration"/> 
    <!-- entry key="txt" value-ref="txtExporterConfiguration"/
</util:map> 

just add a comment tag to disable them and you're done !! happy coding...

<entry key="pdf" value-ref="pdfExporterConfiguration"/>
    <!-- <entry key="xls" value-ref="xlsExporterConfiguration"/> -->
    <!--<entry key="csv" value-ref="csvExporterConfiguration"/> -->
    <!--<entry key="docx" value-ref="docxExporterConfiguration"/> -->
    <!--<entry key="rtf" value-ref="rtfExporterConfiguration"/> -->
    <!--<entry key="swf" value-ref="swfExporterConfiguration"/> -->
    <!--<entry key="odt" value-ref="odtExporterConfiguration"/> -->
    <!--<entry key="ods" value-ref="odsExporterConfiguration"/> -->
    <!--<entry key="xlsx" value-ref="xlsxExporterConfiguration"/> -->
    <!-- entry key="txt" value-ref="txtExporterConfiguration"/-->
</util:map> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜