我可以通过在 prpt 中预先配置(dbconntion + 查询和传递参数)并从 java 调用它来生成真实的报告。它工作正常。
问题:我想使用report.setQuery("dummyQuery","SELECT NAME,ID FROM test.person");//some query
动态生成报告。这可能吗?
当我将 report.setQuery 与相同的查询一起使用时,我的报告会生成一个空白 pdf。我想我应该配置一个 HQL 数据源来实现这一点。
预配置 PRPT - 我正在显示 2 个值,名称和 ID。
我可以通过传递参数动态生成这个pdf。
请大家帮助通过 setQuery 或“如何使用 setQuery”动态生成此 pdf。
我真的很想学习生成 pentaho pdf 报告。
package com.report;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.pentaho.reporting.engine.classic.core.ClassicEngineBoot;
import org.pentaho.reporting.engine.classic.core.MasterReport;
import org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.PdfReportUtil;
import org.pentaho.reporting.libraries.resourceloader.Resource;
import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
/**
* Servlet implementation class Generate
*/
@WebServlet("/Generate")
public class Generate extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Generate() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try{
response.reset();
response.setContentType("application/pdf");
ClassicEngineBoot.getInstance().start();
ResourceManager manager = new ResourceManager();
manager.registerDefaults();
String PrptPath ="C:\\Users\\3692902\\Desktop\\pentahoTest.prpt";
//generate report through pre-configured prpt (db connection + query)
Resource res = manager.createDirectly(new File(PrptPath), MasterReport.class);
MasterReport report = (MasterReport) res.getResource();
report.getParameterValues().put("IDValue",101);
//generate report through query?????
//report.setQuery("SELECT ID FROM test.person");
PdfReportUtil.createPDF(report,response.getOutputStream());
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}