Распечатка счетов в pdf по событию генерации счета
Материал из BiTel WiKi
Данный скрипт, будучи установленным на событие генерации счета, сохраняет его в соответствующий pdf.
v 1.0 by Ezhik452
import bitel.billing.server.util.*; import bitel.billing.server.bill.bean.*; import bitel.common.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import java.util.Map; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.apache.avalon.framework.logger.ConsoleLogger; import org.apache.avalon.framework.logger.Logger; import org.apache.fop.apps.Driver; import org.apache.fop.apps.FOPException; import org.apache.fop.apps.Options; import org.apache.fop.apps.XSLTInputHandler; import org.xml.sax.InputSource; public void onEvent( event, setup, con, conSlave ) { int mid = 4; //id модуля бухгалтерии ModuleSetup moduleSetup = setup.getModuleSetup( mid ); BillManager pbm = new BillManager( setup, con, mid, moduleSetup ); Bill bill = pbm.getBill(event.getBillId()); String xslPath = "/webroot/xsl/bill_all.xsl"; //путь до xsl шаблона, по которому генерируется счет ByteArrayOutputStream outputStream = null; OutputStream pdfOutputStream = null; Map env = System.getenv(); String rootPath = (String) env.get("BGBILLING_SERVER_DIR"); try { org.apache.fop.configuration.Configuration.put("fontBaseDir", "jar:file:" + rootPath + "/lib/fop.jar!/conf/"); URL config = new URL("jar:file:" + rootPath + "/lib/fop.jar!/conf/userconfig.xml"); InputStream input = config.openStream(); new Options(input); } catch (MalformedURLException e) { print(e.getMessage()); } catch (IOException e) { print(e.getMessage()); } catch (FOPException e) { print(e.getMessage()); } Logger logger = new ConsoleLogger(); Driver driver = new Driver(); driver.setLogger(logger); try { Exception e = new Exception(); driver.setRenderer(Driver.RENDER_PDF); driver.dumpError(e); driver.setErrorDump(true); XSLTInputHandler inputHandler = new XSLTInputHandler(new InputSource(new ByteArrayInputStream(pbm.getXMLData(event.getBillId()))), new InputSource(new FileInputStream(rootPath + xslPath))); outputStream = new ByteArrayOutputStream(); driver.setOutputStream(outputStream); inputHandler.run(driver); pdfOutputStream = new FileOutputStream ("c:/pdftest/test_" + event.getBillId() + ".pdf"); //путь до pdf-файла outputStream.writeTo(pdfOutputStream); print(e.getMessage()); } catch (Exception e){ print(e.getMessage()); } finally { if(outputStream != null) try{ outputStream.close(); } catch (IOException e){ print(e.getMessage()); } if(pdfOutputStream != null) try { pdfOutputStream.close(); } catch (IOException e) { print(e.getMessage()); } } }