net.sf.jasper-reports.engine.JR异常运行时异常: 在lib文件夹中添加xercesImpl-2.10.0.jar和jasperreports-5.6.0.jar文件后,在Android Studio中创建SAX解析器时出错。如何从实时数据生成Jasper报告。
答案 0 :(得分:0)
复制以下类,只需注意包名称
if($request->lineasdeinvestigacion){
$otro_item = count($request->lineasdeinvestigacion) - 1;
echo '<p>"other" is the item: '.$otro_item.'</p>';
}else{
echo '<p>Nothing was selected in the checkboxes list</p>';
}
//dd($request->lineasdeinvestigacion);
//Validating input data
$this->validate($request,[
'lineasdeinvestigacion' => 'nullable|max:3',
'fortalecer_otro' => 'required_if:lineasdeinvestigacion.'.$otro_item.',otro|max:255',
],[
'lineasdeinvestigacion.max' => 'No puede elegir más de :max opciones.',
]);
再看下面的代码,FactureHeader和Article是myown数据类,您需要将其替换为我们的数据类:
package com.sunflower.visitor.report;
import java.io.Closeable;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.oss.pdfreporter.engine.JRDataSource;
import org.oss.pdfreporter.engine.JREmptyDataSource;
import org.oss.pdfreporter.engine.JRException;
import org.oss.pdfreporter.engine.JRExporterParameter;
import org.oss.pdfreporter.engine.JasperCompileManager;
import org.oss.pdfreporter.engine.JasperExportManager;
import org.oss.pdfreporter.engine.JasperFillManager;
import org.oss.pdfreporter.engine.JasperPrint;
import org.oss.pdfreporter.engine.JasperReport;
import org.oss.pdfreporter.engine.data.JRXmlDataSource;
import org.oss.pdfreporter.engine.data.JsonDataSource;
import org.oss.pdfreporter.engine.design.JasperDesign;
import org.oss.pdfreporter.engine.export.JRPdfExporterParameter;
import org.oss.pdfreporter.engine.type.RunDirectionEnum;
import org.oss.pdfreporter.engine.util.JRLoader;
import org.oss.pdfreporter.engine.util.JRXmlUtils;
import org.oss.pdfreporter.engine.xml.JRXmlLoader;
import org.oss.pdfreporter.registry.ApiRegistry;
import org.oss.pdfreporter.repo.FileResourceLoader;
import org.oss.pdfreporter.repo.RepositoryManager;
import org.oss.pdfreporter.repo.SubreportUtil;
import org.oss.pdfreporter.sql.IConnection;
import org.oss.pdfreporter.sql.factory.ISqlFactory;
import org.oss.pdfreporter.uses.org.w3c.dom.Document;
public class PdfReporter {
private final String mPdfOutputFolder;
private final String mPdfOutputName;
private final String mJrxmlFilePath;
private Map<JRExporterParameter, Object> mExportParameters = new HashMap();
private Map<String, Object> mFillParameters = new HashMap();
private String mSubreportName;
private String mSubreportLocation;
private boolean mXmlReport;
private boolean mSqlReport;
private boolean mJsonReport;
private String mXmlDataFile;
private String mXmlXPath;
private String mSqlPath;
private String mSqlUsername;
private String mSqlPassword;
private String mJsonDataFile;
private String mJsonExpression;
public PdfReporter(String jrxmlFilePath, String outputFolder, String outputPdfName) {
this.mPdfOutputFolder = outputFolder;
this.mPdfOutputName = outputPdfName;
this.mJrxmlFilePath = jrxmlFilePath;
}
public void setExportParameters(Map<JRExporterParameter, Object> exporterParameters) {
this.mExportParameters.putAll(exporterParameters);
}
public void setFillParameters(Map<String, Object> fillParameters) {
this.mFillParameters.putAll(fillParameters);
}
public static RepositoryManager getRepositoryManager() {
return RepositoryManager.getInstance();
}
private String exportWithoutDataSource() throws Exception {
return this.exportFromXml((String)null, (String)null);
}
public PdfReporter setXmlSource(String xmlDataFile, String xmlXpath) {
if(!this.mSqlReport && !this.mJsonReport) {
this.mXmlReport = true;
this.mXmlDataFile = xmlDataFile;
this.mXmlXPath = xmlXpath;
return this;
} else {
throw new RuntimeException("Can\'t change report type, data source already set");
}
}
public PdfReporter setSqlSource(String databasePath, String username, String password) {
if(!this.mXmlReport && !this.mJsonReport) {
this.mSqlReport = true;
this.mSqlPath = databasePath;
this.mSqlUsername = username;
this.mSqlPassword = password;
return this;
} else {
throw new RuntimeException("Can\'t change report type, data source already set");
}
}
public PdfReporter setJsonSource() {
return this.setJsonSource((String)null, (String)null);
}
public PdfReporter setJsonSource(String jsonDataFile) {
return this.setJsonSource(jsonDataFile, (String)null);
}
public PdfReporter setJsonSource(String jsonDataFile, String selectExpression) {
if(!this.mXmlReport && !this.mSqlReport) {
this.mJsonReport = true;
this.mJsonDataFile = jsonDataFile;
this.mJsonExpression = selectExpression;
return this;
} else {
throw new RuntimeException("Can\'t change report type, data source already set");
}
}
public String exportPdf() throws Exception {
return this.mXmlReport?this.exportFromXml(this.mXmlDataFile, this.mXmlXPath):(this.mSqlReport?this.exportSqlReport(this.mSqlPath, this.mSqlUsername, this.mSqlPassword):(this.mJsonReport?this.exportJsonReport():this.exportWithoutDataSource()));
}
private String exportFromXml(String xmlDataFile, String xmlXpath) throws Exception {
ApiRegistry.initSession();
String var5;
try {
JasperDesign design = this.loadReport(this.mJrxmlFilePath);
design.setColumnDirection(RunDirectionEnum.RTL);
JasperReport report = JasperCompileManager.compileReport(design);
var5 = this.exportReport(report, xmlDataFile, xmlXpath);
} finally {
ApiRegistry.dispose();
}
return var5;
}
private String exportSqlReport(String databasePath, String username, String password) throws Exception {
ApiRegistry.initSession();
IConnection sqlDataSource = null;
String var10;
try {
JasperDesign design = this.loadReport(this.mJrxmlFilePath);
JasperReport report = JasperCompileManager.compileReport(design);
ISqlFactory sqlFactory = ApiRegistry.getSqlFactory();
sqlDataSource = sqlFactory.newConnection(databasePath, username, password);
this.processSubreport();
JasperPrint printReport = JasperFillManager.fillReport(report, this.mFillParameters, sqlDataSource);
String pathToPdfFile = this.mPdfOutputFolder + "/" + this.mPdfOutputName + ".pdf";
JasperExportManager.exportReportToPdfFile(printReport, pathToPdfFile, this.mExportParameters);
var10 = pathToPdfFile;
} finally {
this.close(sqlDataSource);
ApiRegistry.dispose();
}
return var10;
}
private String exportReport(JasperReport compiledReport, String xmlDataFile, String xmlXpath) throws Exception {
ApiRegistry.initSession();
Object dataSource = null;
InputStream isXmlData = null;
String var8;
try {
if(xmlDataFile == null) {
dataSource = new JREmptyDataSource();
} else {
isXmlData = FileResourceLoader.getInputStream(xmlDataFile);
JRXmlDataSource printReport = new JRXmlDataSource(isXmlData, xmlXpath);
printReport.setDatePattern("yyyy-MM-dd");
dataSource = printReport;
}
this.processSubreport();
JasperPrint printReport1 = JasperFillManager.fillReport(compiledReport, this.mFillParameters, (JRDataSource)dataSource);
String pathToPdfFile = this.mPdfOutputFolder + "/" + printReport1.getName() + ".pdf";
JasperExportManager.exportReportToPdfFile(printReport1, pathToPdfFile, this.mExportParameters);
var8 = pathToPdfFile;
} finally {
this.close(isXmlData);
ApiRegistry.dispose();
}
return var8;
}
private String exportJsonReport() throws Exception {
ApiRegistry.initSession();
JsonDataSource jsonDataSource = null;
String var6;
try {
JasperDesign design = this.loadReport(this.mJrxmlFilePath);
JasperReport report = JasperCompileManager.compileReport(design);
JasperPrint printReport = null;
this.processSubreport();
if(this.mJsonDataFile == null) {
printReport = JasperFillManager.fillReport(report, this.mFillParameters);
} else {
InputStream pathToPdfFile = FileResourceLoader.getInputStream(this.mJsonDataFile);
jsonDataSource = new JsonDataSource(pathToPdfFile, this.mJsonExpression);
printReport = JasperFillManager.fillReport(report, this.mFillParameters, jsonDataSource);
}
String pathToPdfFile1 = this.mPdfOutputFolder + "/" + printReport.getName() + ".pdf";
JasperExportManager.exportReportToPdfFile(printReport, pathToPdfFile1, this.mExportParameters);
var6 = pathToPdfFile1;
} finally {
this.close(jsonDataSource);
ApiRegistry.dispose();
}
return var6;
}
public PdfReporter addSubreport(String subreportName, String location) throws JRException {
this.mSubreportName = subreportName;
this.mSubreportLocation = location;
return this;
}
private void processSubreport() throws JRException {
if(this.mSubreportLocation != null && this.mSubreportName != null) {
JasperReport subreport = SubreportUtil.loadSubreport(this.mSubreportLocation);
this.mFillParameters.put(this.mSubreportName, subreport);
}
}
public PdfReporter addEncryption(boolean is128bitKey, String userPassword, String ownerPassword, int permissions) {
this.mExportParameters.put(JRPdfExporterParameter.IS_ENCRYPTED, Boolean.TRUE);
this.mExportParameters.put(JRPdfExporterParameter.IS_128_BIT_KEY, is128bitKey?Boolean.TRUE:Boolean.FALSE);
this.mExportParameters.put(JRPdfExporterParameter.USER_PASSWORD, userPassword);
this.mExportParameters.put(JRPdfExporterParameter.OWNER_PASSWORD, ownerPassword);
this.mExportParameters.put(JRPdfExporterParameter.PERMISSIONS, Integer.valueOf(permissions));
return this;
}
public PdfReporter addJSONParams(String datePattern, String numberPattern, Locale jsonLocale, Locale country) {
this.mFillParameters.put("net.sf.jasperreports.json.date.pattern", datePattern);
this.mFillParameters.put("net.sf.jasperreports.json.number.pattern", numberPattern);
this.mFillParameters.put("JSON_LOCALE", jsonLocale);
this.mFillParameters.put("REPORT_LOCALE", country);
return this;
}
public PdfReporter addXMLParams(String datePattern, String numberPattern, Locale xmlLocale, Locale country) {
this.mFillParameters.put("XML_DATE_PATTERN", datePattern);
this.mFillParameters.put("XML_NUMBER_PATTERN", numberPattern);
this.mFillParameters.put("XML_LOCALE", xmlLocale);
this.mFillParameters.put("REPORT_LOCALE", country);
return this;
}
public PdfReporter addSubReportXMLDocument(String xmlDataFile) throws JRException {
Document document = JRXmlUtils.parse(JRLoader.getLocationInputStream(xmlDataFile));
this.mFillParameters.put("XML_DATA_DOCUMENT", document);
return this;
}
public PdfReporter newResourceBundle(String classPath, Locale locale) {
this.mFillParameters.put("REPORT_LOCALE", locale);
ResourceBundle resourceBundle = ResourceBundle.getBundle(classPath, locale);
this.mFillParameters.put("REPORT_RESOURCE_BUNDLE", resourceBundle);
return this;
}
public PdfReporter addFillParameter(String key, Object value) {
this.mFillParameters.put(key, value);
return this;
}
private JasperDesign loadReport(String reportFileName) throws Exception {
InputStream isReport = null;
JasperDesign var3;
try {
isReport = FileResourceLoader.getInputStream(reportFileName);
var3 = JRXmlLoader.load(isReport);
} finally {
this.close(isReport);
}
return var3;
}
private void close(Closeable stream) throws Exception {
if(stream != null) {
stream.close();
}
}
}
还有一个XMLStorage类来生成将数据转交给jrxml文件的xml文件,请注意,您必须将方法配置为数据:
public String exportFactures(Activity activity, FactureHeader factureHeader, List<Article> factureArticles) throws Exception {
PdfReporter exporter = getExporter("FactureReport.jrxml", "crosstabs", "extra-fonts");
Map<JRExporterParameter, Object> map = new HashMap<JRExporterParameter, Object>();
map.put(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.setExportParameters(map);
exporter.addXMLParams("YYYY/MM/dd", "#,##0", new Locale("ar", "EG"), new Locale("ar", "EG"));
return exporter.setXmlSource(XmlStorage.saveXmlFacturePdfReportArticle(activity, factureHeader, factureArticles), "/facture/article").exportPdf();
}
private PdfReporter getExporter(String jrxmlPath, String reportFolder, String extraFolder) {
//path to root folder with all reports
final String rootFolder = getRootFolder();
final String resourceFolder = getResourcesFolder();
RepositoryManager repo = PdfReporter.getRepositoryManager();
repo.reset();
repo.setDefaultResourceFolder(resourceFolder);
repo.setDefaulReportFolder(rootFolder + RepositoryManager.PATH_DELIMITER + "jrxml" + RepositoryManager.PATH_DELIMITER + reportFolder);
并确保已将jasper jar文件正确添加到项目中。请让我知道发生了什么,距离我上次在Android中使用Jasper已有很长时间了,也许我缺少一些东西。