Jasper Report: how to configure JasperViewer so that it can export in only one format?

Our client loves the Jasper viewer, but we have a problem. It exports data in several different formats (PDF, Excel, CSV, HTML, etc.), but our client wants to export to PDF.

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

+3
source share
3 answers

I found a solution that, in my opinion, was just awful, but worked on my case.

Good: after reading the source code for the JasperViewer class, I found a protected field named viewer in this class.

, , , :

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();
            } 

, , , Jasper Reports 3.7.1. , , - , .

+5

SaveContributor, PDF ? E. g. JRPdfSaveContributor.

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

viewReportsBean.xml \apache-tomcat-7.0.12\webapps\jasperserver\WEB-INF\flow

,

<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> 

, , ! ...

<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

Source: https://habr.com/ru/post/1740017/


All Articles