2 Replies Latest reply on Feb 3, 2009 3:58 AM by Sascha Janz

    a4j:mediaoutput  pdf not rendered

    Sascha Janz Master

      i want to display a pdf inside of my jsf page.

      i use a4j:mediaoutput for rendering. i tested my code with a jpg an everything was ok. but if i use pdf i only got an image like a broken link,although the pdf is written to the outputstream. i wrote a copy on filesystem which is ok.

      here is the code

      <?xml version="1.0" encoding="ISO-8859-1"?>
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:rich="http://richfaces.org/rich"
       xmlns:a4j="http://richfaces.org/a4j"
       xmlns:s="http://jboss.com/products/seam/taglib">
      
      <h:form id="testpdf" >
      <h:outputLabel value="TEST!!!!" for="" />
      <a4j:mediaOutput element="pdf" cacheable="false"
       createContent="#{PDF.loadPDF}" mimeType="application/pdf" > </a4j:mediaOutput>
       </h:form>
      </ui:composition>
      


      java bean

      
      
      import java.io.BufferedInputStream;
      import java.io.DataInputStream;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.OutputStream;
      import java.nio.ByteBuffer;
      import java.nio.channels.FileChannel;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.log.Log;
      import org.jboss.seam.log.Logging;
      
      
      
      @Name("PDF")
      @Scope(ScopeType.SESSION)
      
      public class PDFBean {
      
       private static final Log log = Logging.getLog(PDFBean.class.getName());
      
      
       public void loadPDFFehler(OutputStream out, Object data) throws IOException{
       log.debug("Calling loadPDF.........................................................................................");
       try {
       /*if (null == data) {
       log.debug("Data is null................................................");
       return;
       }*/
       //if (data instanceof PDFDoc) {
       // PDFDoc pdfdata = (PDFDoc) data;
       //File aFile = new File("D:\\Bild0710.jpg");
       File aFile = new File("D:\\test.pdf");
       log.debug("reading " + aFile.getAbsolutePath());
       FileInputStream inFile = null;
       try {
       inFile = new FileInputStream(aFile);
       } catch (FileNotFoundException e) {
       e.printStackTrace(System.err);
       }
       FileChannel inChannel = inFile.getChannel();
       final int PRIMECOUNT = 6;
       ByteBuffer buf = ByteBuffer.allocate(8 * PRIMECOUNT);
       long[] primes = new long[PRIMECOUNT];
       try {
       while (inChannel.read(buf) != -1) {
       ((ByteBuffer) (buf.flip())).asLongBuffer().get(primes);
       out.write(buf.array());
       buf.clear();
       }
       inFile.close();
       } catch (IOException e) {
       e.printStackTrace(System.err);
       }
       //}
       } catch (Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
      
       log.debug("end loadPDF...................................................................................");
      
      
       }
      
       public void loadPDF(OutputStream out, Object data) throws IOException{
      
       FileInputStream fis = new FileInputStream("d:\\test.pdf");
       FileOutputStream fout = new FileOutputStream("d:\\test1.pdf");
       BufferedInputStream bis = new BufferedInputStream(fis);
       DataInputStream dis = new DataInputStream(bis);
      
       int counter = 0;
       boolean end =false;
       byte[] buffer =new byte[1024*10124];
       while (!end) {
       try{
      
       int bytes = dis.read(buffer);
       System.out.println("bytes read=" + bytes );
       if ( bytes > 0 ) {
       out.write(buffer,0,bytes);
       fout.write(buffer,0,bytes);
       }
       else {
       end = true;
       out.flush();
       out.close();
       fout.close();
       }
      
       }catch (Exception ex) {
       ex.printStackTrace();
       end = true;
       }
       }
       dis.close();
       bis.close();
       fis.close();
       }
      
      
      
      }
      


      what am i doing wrong?

        • 1. Re: a4j:mediaoutput  pdf not rendered
          Sergey Smirnov Master

          a4j:mediaOutput element="pdf" .....

          means:

          <pdf src="......">


          There is no such tag in the html, right?

          In general, if you want to insert something inside html page that has a different content type, you need to use < object > or/and < embed > tags.

          Read more on google: http://tinyurl.com/html-pdf-object




          • 2. Re: a4j:mediaoutput  pdf not rendered
            Sascha Janz Master

            thank you for your reply

            so i changed code to

            <?xml version="1.0" encoding="ISO-8859-1"?>
            <ui:composition xmlns="http://www.w3.org/1999/xhtml"
             xmlns:ui="http://java.sun.com/jsf/facelets"
             xmlns:h="http://java.sun.com/jsf/html"
             xmlns:f="http://java.sun.com/jsf/core"
             xmlns:rich="http://richfaces.org/rich"
             xmlns:a4j="http://richfaces.org/a4j"
             xmlns:s="http://jboss.com/products/seam/taglib">
            
            <h:form id="testpdf" >
            <h:outputLabel value="TEST!!!!" for="" />
            <object data="test.pdf" style="width:500px; height:800px;" type="application/pdf"></object>
            <a4j:mediaOutput element="object" cacheable="false"
             createContent="#{PDF.loadPDF}" mimeType="application/pdf" uriAttribute="pdf" > </a4j:mediaOutput>
             </h:form>
            </ui:composition>
            


            but createcontent is not called...why?