- From: Paulo Carvalho <pjcarvalho@gmail.com>
- Date: Wed, 17 Feb 2010 16:20:12 +0100
- To: www-xsl-fo@w3.org
- Cc: dave.pawson@gmail.com
- Message-ID: <8f151cff1002170720o42d8906w4e49fbf3584bd65e@mail.gmail.com>
Hi again I used a simple example on the Internet about how to use FOP in java ( http://javaboutique.internet.com/tutorials/FOP/) : - I Created a simple Java application with a class that takes an XML and converts it into a PDF (using a XSL) containing an image. The name of my class is Process.java and it has a method "process" - I Created a simple web service that just call this "process" method of that class. Here is the code of my Process.java class: public static String process(String xml, String xsl) { String sResult = null; try { ByteArrayOutputStream foOut = new ByteArrayOutputStream(); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); InputStream iss = Process.class.getClassLoader().getResourceAsStream(brique); copyFile(new BufferedInputStream(iss), bOut); SAXBuilder builder = new SAXBuilder(); Document document = builder.build(new ByteArrayInputStream(xml.getBytes())); TransformerFactory factory = TransformerFactory.newInstance(); InputStream iXsl = Process.class.getClassLoader().getResourceAsStream(xsl); StreamSource iSource = new StreamSource(iXsl); Transformer foTrans = factory.newTransformer(iSource); StreamSource strSourceXML = new StreamSource(new ByteArrayInputStream(xml.getBytes())); foTrans.transform(strSourceXML, new StreamResult(foOut)); foOut.flush(); ByteArrayOutputStream pdfOut = new ByteArrayOutputStream(); TransformerFactory tFactoryFO2PDF = TransformerFactory.newInstance(); Transformer pdfTrans = tFactoryFO2PDF.newTransformer(); FopFactory fopFactory = FopFactory.newInstance(); FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, pdfOut); Result res = new SAXResult(fop.getDefaultHandler()); StreamSource streamSourceXml = new StreamSource(new ByteArrayInputStream(foOut.toByteArray())); pdfTrans.transform(streamSourceXml, res); java.io.File file = new java.io.File("d:/res.pdf"); FileOutputStream foStream = new FileOutputStream(file); pdfOut.writeTo(foStream); } catch(Exception e) { e.printStackTrace(); } return sResult; } private static boolean copyFile(InputStream in, OutputStream out) { try { int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); } catch (IOException io) { return false; } return true; } The code of my web service is just: public static String process(String xml, String xsl) { String sResult = null; try { sResult = Process.process(xml, xsl); System.out.println("sss"); } catch(Exception e) { e.printStackTrace(); } return sResult; } The web service has the JAR of the Java application in his classpath. The content of the Jar file is the following one: Name Path briques.xsd logo.gif img\ Manifest.mf meta-inf\ Process.class tst saxon-licence.lic xsl2.xslt I call the web service with the following parameters: xml = "<?xml version='1.0' encoding='UTF-8'?>"+ "<Catalog>"+ "<Book>"+ "<Title>Mastering EJB</Title>"+ "<Author>Ed Roman</Author>"+ "<Price>$45.00</Price>"+ "</Book>"+ "<Book>"+ "<Title>Design Patterns</Title>"+ "<Author>Erich Gamma</Author>"+ "<Price>$50.00</Price>"+ "</Book>"+ "<Book>"+ "<Title>Effective Java</Title>"+ "<Author>Josch Bloch</Author>"+ "<Price>$30.00</Price>"+ "</Book>" + "</Catalog>"; xsl = "xsl2.xslt"; In the xsl2.xslt I have a part of code like this to insert the image on the pdf: ... <fo:block> <fo:external-graphic src="img/logo.gif"/> </fo:block> ... The XSL is found in the JAR because the PDF file is generated. But the following error still appearing and the image is not inserted on the PDF: [ERROR] Image not found: img/logo.gif What am I doing wrong? Thanks Regards On Wed, Feb 17, 2010 at 1:20 PM, Dave Pawson <dave.pawson@gmail.com> wrote: > On 17 February 2010 11:38, Paulo Carvalho <pjcarvalho@gmail.com> wrote: > > > => The XSL stylesheet comes from a Database. It is not on the file > system. > > My image file is on a JAR file which is on the WAR file on my web > service. > > File name and path of the image are correct on the JAR file. > > In which case you are on your own and outside the W3C recommendation? > > Try it first with the stylesheet available on disk, with the img directory > in the same directory as the stylesheet, image within ./img > then see if it works. > > that way is 'as expected' by the rec. > > btw, this thread is more suitable for the XSLT mailing list > at www.mulberrytech.com > > > > -- > Dave Pawson > XSLT XSL-FO FAQ. > Docbook FAQ. > http://www.dpawson.co.uk > -- Paulo Carvalho 1 rue du Chateau 57710 Aumetz France http://forum-informatico.forumeiros.com/index.htm http://ummundoecologico.blogspot.com
Attachments
- image/png attachment: 1.PNG
Received on Wednesday, 17 February 2010 15:20:49 UTC