web service to print PDF

this openbravo tutorial will share how to create a web service in Openbravo ERP, that put a PDF stream as the output. Many functionality achived by using this scenario, such as: display a link in email alert notification, once clicked then give you PDF prinout.PDF, just like another stream output such a xml, html, xls, txt, etc, are defined in mime type. You can follow steps below to create:

  1. create a generic web service object in openbravo, just like ordinary openbravo web service, please follow openbravo modularity rule to create it.
  2. inside doGet meythod, write a code that put PDF output stream. your previously designed report saved as jrxml, will be rendered as JasperReport object, then you need to export this JasperReport object into PDF.
  3. put exported PDF to OutputStream on HttpServletResponse.

lest checkout this code snippet:

package org.openbravo.howto.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Properties;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRXlsExporter;

import org.openbravo.base.provider.OBConfigFileProvider;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.database.ConnectionProviderImpl;
import org.openbravo.model.common.plm.Product;
import org.openbravo.service.web.WebService;

public class SalesOrderPrint implements WebService {
@Override
 public void doGet(String path, HttpServletRequest request,
 HttpServletResponse response) throws Exception {
 
 //make sure product parameter exists, if not then exit
 final String productID = request.getParameter("product");
 if (productID==null || productID.isEmpty()){
 throw new IllegalArgumentException("parameter product is required");
 }
 
 //make sure product parameter exists in OB product master data, if not then exit
 final Product product = OBDal.getInstance().get(Product.class, productID);
 if (product==null){
 throw new IllegalArgumentException("product with id: "+ productID + " does not exist");
 }
 
 //get attachment path
 OBConfigFileProvider obConfigFileProvider = OBConfigFileProvider.getInstance();
 String webINFLocation=obConfigFileProvider.getServletContext().getRealPath("")+obConfigFileProvider.getClassPathLocation();
 String obConfigFileLocation = webINFLocation+"/Openbravo.properties";
 File obConfigFile = new File(obConfigFileLocation);
 InputStream obConfigInputStream = new FileInputStream(obConfigFile);
 Properties obConfigProperties = new Properties();
 obConfigProperties.load(obConfigInputStream);
 String attachPath = obConfigProperties.getProperty("attach.path");
 
 //get report file path
 String strReportFileLocation = webINFLocation+"/classes/org/openbravo/howto/ad_report/OrderPrint.jrxml";
 
 
 
 //build report parameter
 HashMap<String, Object> parameters = new HashMap<String, Object>();
 parameters.put("product", productID);
 
 //build report connection
 final ConnectionProvider conn = new ConnectionProviderImpl(obConfigFileLocation);
 Connection con = conn.getConnection();
 
 //compile report
 JasperReport jasperReport=JasperCompileManager.compileReport(strReportFileLocation);
 JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport, parameters, con);
 
 //render report output
 printPagePDF(jasperPrint, response, attachPath);
 
 }

@Override
 public void doPost(String path, HttpServletRequest request,
 HttpServletResponse response) throws Exception {
 
 }

@Override
 public void doDelete(String path, HttpServletRequest request,
 HttpServletResponse response) throws Exception {
 
 }

@Override
 public void doPut(String path, HttpServletRequest request,
 HttpServletResponse response) throws Exception {
 
 }
 
 private void printPagePDF(JasperPrint jasperPrint, HttpServletResponse response, String reportOutputFolder) throws JRException, IOException{
 
 //define report output full path
 String strReportDestination = reportOutputFolder+java.io.File.separator+UUID.randomUUID().toString()+"OrderPrint.pdf";
 
 //export to PDF
 JasperExportManager.exportReportToPdfFile(jasperPrint, strReportDestination);
 
 //set HTTP response to PDF
 response.setHeader("Content-disposition", "inline; filename=Yearly Sales Order Report.pdf");
 
 /*load PDF to Output Stream
 * 1. load generated PDF to File object
 * 2. create OutputStream object based on HTTP response
 * 3. create buffer
 * 4. read PDF, load into buffer then OutputStream
 * 5. once done, your browser should display PDF page
 * 6. destroy OutputStream and InputStream to avoid memory leak
 * 7. delete generated PDF to avoid undesired disk usage*/
 File reportOutputFile = new File(strReportDestination);
 OutputStream reportOutputStream = response.getOutputStream();
 byte[] reportOutputBuffer = new byte[8192];
 InputStream reportInputStream = new FileInputStream(reportOutputFile);
 int c = 0;
 while ((c=reportInputStream.read(reportOutputBuffer, 0, reportOutputBuffer.length))>0){
 reportOutputStream.write(reportOutputBuffer, 0, c);
 reportOutputStream.flush();
 }
 
 //destroy I/O object
 reportOutputStream.close();
 reportInputStream.close();
 reportOutputFile.delete();
 
 }
 
 private void printPageHTML(JasperPrint jasperPrint, HttpServletResponse response, String reportOutputFolder) throws JRException, IOException{
 
 //define report output full path 
 String uuid = java.io.File.separator+UUID.randomUUID().toString();
 String strReportDestination = reportOutputFolder+uuid+"OrderPrint.html";
 String strReportLocation = reportOutputFolder+uuid+"OrderPrint.html_files";
 
 //export to HTML
 JasperExportManager.exportReportToHtmlFile(jasperPrint, strReportDestination);
 
 //set HTTP response to HTML
 response.setContentType("text/html");
 response.setCharacterEncoding("utf-8");
 
 //TODO output stream not capable yet to export image inside HTML file folder
 //load HTML to Output Stream
 File reportOutputFile = new File(strReportDestination);
 OutputStream reportOutputStream = response.getOutputStream();
 byte[] reportOutputBuffer = new byte[8192];
 InputStream reportInputStream = new FileInputStream(reportOutputFile);
 int c = 0;
 while ((c=reportInputStream.read(reportOutputBuffer, 0, reportOutputBuffer.length))>0){
 reportOutputStream.write(reportOutputBuffer, 0, c);
 reportOutputStream.flush();
 }
 
 //destroy I/O object
 reportOutputStream.close();
 reportInputStream.close();
 reportOutputFile.delete();
 File reportFolder = new File(strReportLocation);
 String[] entries = reportFolder.list();
 for (String files: entries){
 File currentFile=new File(reportFolder.getPath(),files);
 currentFile.delete();
 }
 reportFolder.delete();
 
 }
 
 private void printPageXLS(JasperPrint jasperPrint, HttpServletResponse response, String reportOutputFolder) throws JRException, IOException{
 
 //define report output full path
 String strReportDestination = reportOutputFolder+java.io.File.separator+UUID.randomUUID().toString()+"OrderPrint.xls";
 
 //convert to XLS
 //TODO JRXlsExporter throw exception: class not found: org.apache.poi.ss.usermodel.Font
 JRXlsExporter exporter = new JRXlsExporter();
 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
 exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strReportDestination);
 exporter.exportReport();
 
 //set HTTP response to XLS
 response.setHeader("Content-disposition", "inline; filename=Yearly Sales Order Report.xls");
 
 //load XLS into output steam
 File reportOutputFile = new File(strReportDestination);
 OutputStream reportOutputStream = response.getOutputStream();
 byte[] reportOutputBuffer = new byte[8192];
 InputStream reportInputStream = new FileInputStream(reportOutputFile);
 int c = 0;
 while ((c=reportInputStream.read(reportOutputBuffer, 0, reportOutputBuffer.length))>0){
 reportOutputStream.write(reportOutputBuffer, 0, c);
 reportOutputStream.flush();
 }
 
 //destroy I/O object
 reportOutputStream.close();
 reportInputStream.close();
 reportOutputFile.delete();
 
 }
}

also, please check our configuration file: attachment:org.openbravo.howto-provider-config.xml

after all steps executed, you can call the web service http://localhost:8081/openbravo/ws/org.openbravo.howto.getSalesOrderPrint?product=9E3A94833BBD468C865C297449CC6AED

laviola, now you get a PDF file through this web service call.

From openbravo tutorial

happy openbravo coding – openbravo tutorial – a brickbook company.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s