当我打印报告注释时,JasperExportManager.exportReportToPdf(JasperPrint)
报告会在Jar文件中很好地显示。否则,报表将卡在此功能中,并且报表查看器不会出现,甚至不会出现任何错误。在Netbeans中,它工作正常。我正在JDK8u25(32bits)中工作。这是我的报告打印代码:
public void PrepareReport(ResultSet rs) {
try{
String reportName ="/com/mmts/reports/challan.jasper";
InputStream st = getClass().getResourceAsStream(reportName);
JasperReport jr= (JasperReport) JRLoader.loadObject(st);
JasperPrint jp =
JasperFillManager.fillReport(jr,new HashMap<>(),new JRResultSetDataSource(rs));
JRViewer jv = new JRViewer(jp);
JFrame jf = new JFrame();
jf.getContentPane().add(jv);
JasperExportManager.exportReportToPdf(jp);
jf.validate();
jf.setVisible(true);
jf.setTitle("Challan");
jf.setSize(new Dimension(1200,600));
jf.setLocation(20,20);
jf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}catch(JRException ex){
JOptionPane.showMessageDialog(null, ex);
}
}
这是我的打包控制台输出:
ant -f D:\\Metro\\MMTS\\NetBeansProjects\\MMTS -Djar.archive.disabled=true -Dnative.bundling.type=image build-native
Detected JavaFX Ant API version 1.3
init:
deps-clean:
Updating property file: D:\Metro\MMTS\NetBeansProjects\MMTS\build\built-clean.properties
Deleting directory D:\Metro\MMTS\NetBeansProjects\MMTS\build
clean:
deps-jar:
Created dir: D:\Metro\MMTS\NetBeansProjects\MMTS\build
Updating property file: D:\Metro\MMTS\NetBeansProjects\MMTS\build\built-jar.properties
Created dir: D:\Metro\MMTS\NetBeansProjects\MMTS\build\classes
Created dir: D:\Metro\MMTS\NetBeansProjects\MMTS\build\empty
Created dir: D:\Metro\MMTS\NetBeansProjects\MMTS\build\generated-sources\ap-source-output
Compiling 24 source files to D:\Metro\MMTS\NetBeansProjects\MMTS\build\classes
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Copying 5 files to D:\Metro\MMTS\NetBeansProjects\MMTS\build\classes
Copied 2 empty directories to 1 empty directory under D:\Metro\MMTS\NetBeansProjects\MMTS\build\classes
compile:
Created dir: D:\Metro\MMTS\NetBeansProjects\MMTS\dist
Copying 33 files to D:\Metro\MMTS\NetBeansProjects\MMTS\dist\lib
Launching <fx:jar> task from C:\Program Files (x86)\Java\jdk1.8.0_25\jre\..\lib\ant-javafx.jar
Launching <fx:deploy> task from C:\Program Files (x86)\Java\jdk1.8.0_25\jre\..\lib\ant-javafx.jar
Note: To create native bundles the <fx:deploy> task may require external tools. See JavaFX 2.2+ documentation for details.
Launching <fx:deploy> in native packager mode...
No base JDK. Package will use system JRE.
Creating app bundle: MMTS in D:\Metro\MMTS\NetBeansProjects\MMTS\dist\bundles
Result application bundle: D:\Metro\MMTS\NetBeansProjects\MMTS\dist\bundles
Deleting: D:\Metro\MMTS\NetBeansProjects\MMTS\dist\MMTS.html
Deleting: D:\Metro\MMTS\NetBeansProjects\MMTS\dist\MMTS.jnlp
Deleting: D:\Metro\MMTS\NetBeansProjects\MMTS\dist\MMTS_browser.jnlp
build-native:
BUILD SUCCESSFUL (total time: 17 seconds)
对我来说,将生成的报告保存为pdf formatt是必须的。请帮忙。
最佳答案
好吧...现在我有了解决方案。
问题出在itextpdf和lowagie jars的版本中。
早些时候,我把罐子包括如下:
(1)itextpdf-2.1.7.jar
(2)com.springsource.com.lowagie.text-2.0.8.jar
现在我将它们更改为以下内容:
(1)itextpdf-5.5.3.jar
(2)com.lowagie.text-2.1.7
由于这个原因,我可以在报表查看器中查看我的报表,然后打印到打印机或保存为pdf
现在的最终代码如下:
public void PrepareReport(ResultSet rs){
try{
String reportName ="/com/mmts/reports/challan.jasper";
InputStream st = getClass().getResourceAsStream(reportName);
JasperReport jr= (JasperReport) JRLoader.loadObject(st);
Map parameters = new HashMap();
JasperPrint jp = JasperFillManager.fillReport(jr,parameters,new JRResultSetDataSource(rs));
JasperExportManager.exportReportToPdf(jp);
JasperViewer jv=new JasperViewer(jp,false);
jv.setTitle("Challan");
jv.setVisible(true);
}catch(JRException ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}
谢谢