Board index JavaServer Faces General media PDF document not displayed

media PDF document not displayed

Components, Ajax Framework, Utilities and More.


Posts: 95
I am creating PDF documents in the web layer using Itext 2.1.7.

I can down the generated PDF using <p:filedownload ... but I really want to display the PDF document in the middle of a <p:dialog

If I use media command shown below I get blank area when using crome and firefox browser crashes.
In the Glassfish log it notes that mimetype is not known.

<p:media value="#{arDoc.invoicePDF}" player="pdf"/>


In showcase there is a blank area where the PDF document shoul be rendered.

Is it necessary to configure mimetypes in glassfish to use <p:media ?


Thank

Chris
J2EE 6 Glassfish 3.1.2 Mojarra 2.1.26 (SNAPSHOT 20111206) primefaces 4.0
Netbeans 7.2
Browser Firefox 19.0.1 database postgresql-9.1.3


Posts: 5689
I searched the forum (via forum search in upper right corner of this page) for the following:

pdf

scrolled down the list of results, and found the forum topic below:

viewtopic.php?f=3&t=25150&p=79877&hilit=pdf#p79835

Why did I know about this forum topic, because I like to scan forum topics almost daily, just to see what other people are talking about, and many many times, I am inspired to add some feature to my JSF/PrimeFaces app after reading/scanning forum topics here.

You can learn a lot this way, and then there will be 'almost' no need to ever ask a question (that was already asked and/or answered, already). :)
Howard

PrimeFaces 4.0.x, Extensions 1.2.1, Push (Atmosphere 2.1.3)
TomEE 1.6.1 (Tomcat 7.0.53), MyFaces Core 2.2.2
JUEL 2.2.6 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache


Posts: 95
The previous thread recommends using p:media. I am attempting to use p:media.

Additionally the showcase example does not work correctly.

If p: media cannot handle pdf documents then perhaps the documentation should be amended to remove player="pdf" as a valid usage.

Searching on glassfish I have found that some article suggesting web.xml should be updated for the mime type and others stating that it is built into glasfish.
<mime-mapping>
     <extension>pdf</extension>
     <mime-type>application/pdf</mime-type>
</mime-mapping>



If you have media working what changes have you needed to make?

The bean method that creates the pdf is as below. Is there an error in this:
public StreamedContent getInvoicePDF ()throws IOException {
 logger.log(INFO, "getInvoicePDF called");
 String fname = "Invoice-"+this.invoice.getInvoiceNumber()+".pdf";
 
  try{
  ByteArrayOutputStream baos = this.invoicePdfStream();
 
  if(baos == null){
   throw new BacException("pdf not set");
  }
 
 byte[] outputFile = baos.toByteArray();
 invoice.setInvoicePdf(outputFile);
 logger.log(INFO, "outputFile {0}", outputFile.length);
 ByteArrayInputStream bais = new ByteArrayInputStream(invoice.getInvoicePdf());
 
 invoiceMedia = new DefaultStreamedContent(bais, "application/pdf",fname);
 logger.log(INFO, "invoiceMedia {0}", invoiceMedia);
 baos.flush();
 baos.close();
 bais.close();
 return invoiceMedia;
               
 }catch(DocumentException ex){
  String msg = errorForKey("slInvNoInvPdf");
  GenUtil.addErrorMessage(msg);
  throw new BacException("Could not create invoice pdf document reason: "+ex.getLocalizedMessage());
 }
 
}



Thanks
Last edited by chrishj on 29 Oct 2012, 00:59, edited 1 time in total.
J2EE 6 Glassfish 3.1.2 Mojarra 2.1.26 (SNAPSHOT 20111206) primefaces 4.0
Netbeans 7.2
Browser Firefox 19.0.1 database postgresql-9.1.3


Posts: 5689
andyba wrote:
I really recommend using http://pdfobject.com/ in particular their generator for scriptless markup.
Or you can use a p:media tag....

Maybe andyba might see this and respond to your question(s).
Howard

PrimeFaces 4.0.x, Extensions 1.2.1, Push (Atmosphere 2.1.3)
TomEE 1.6.1 (Tomcat 7.0.53), MyFaces Core 2.2.2
JUEL 2.2.6 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache


Posts: 95
I have tried to set the mime-type in web.xml this does not help the issue so it is not a glassfish problem.

<mime-mapping>
     <extension>pdf</extension>
     <mime-type>application/pdf</mime-type>
    </mime-mapping>
J2EE 6 Glassfish 3.1.2 Mojarra 2.1.26 (SNAPSHOT 20111206) primefaces 4.0
Netbeans 7.2
Browser Firefox 19.0.1 database postgresql-9.1.3


Posts: 2
I'm also trying to use pdf with DefaultStreamedContent and the media tag using primefaces 3.4.1

pdfStream = new DefaultStreamedContent(is, "application/pdf");

<p:media value="#{reportBackerBean.pdfStream}" width="100%" height="300px">

Since the default is based on the file extension (see http://cagataycivici.wordpress.com/2010 ... rimefaces/),
I can understand if it is not able to detect what player to use. However, player="pdf" seems no longer to be supported.
The attributes section of the media tag documentation indicates
attribute: player, possible values are "quicktime","windows","flash","real".
(http://www.primefaces.org/documentation.html version 3.3)

Based on examples I saw online, I saw tried this with the player="pdf" attribute:
<p:media value="#{reportBackerBean.pdfStream}" player="pdf" width="100%" height="300px">
but I see the following error:
"pdf is not a valid media player type"

Dropping this line into my getPdfStream method shows that there is no player "pdf"
Map<String, MediaPlayer> mp = MediaPlayerFactory.getPlayers();
mp =
[0] HashMap$Entry "real => org.primefaces.component.media.player.RealPlayer@3acd811"
[1] HashMap$Entry "flash => org.primefaces.component.media.player.FlashPlayer@489d45f9"
[2] HashMap$Entry "windows => org.primefaces.component.media.player.WindowsPlayer@4d5d03ad"
[3] HashMap$Entry "quicktime => org.primefaces.component.media.player.QuickTimePlayer@159c7a89"

This matches the source code I found at http://grepcode.com/file/repository.pri ... ctory.java
getPlayers() {
if(players == null) {
players = new HashMap<String, MediaPlayer>();
players.put(MediaPlayer.QUICKTIME, new QuickTimePlayer());
players.put(MediaPlayer.FLASH, new FlashPlayer());
players.put(MediaPlayer.WINDOWS, new WindowsPlayer());
players.put(MediaPlayer.REAL, new RealPlayer());
}

I found source for a different version of this MediaPlayerFactory.getPlayers() which included the line but this does not currently seem to be available in primefaces 3.4.1.
players.put(MediaPlayer.PDF, new PDFPlayer());
http://code.google.com/p/primefaces/sou ... ava?r=7840

Has anyone been able to get the media tag to work for pdf display using DefaultStreamedContent?


primefaces 3.4.1
glassfish 3.1.2
firefox 16.0.2
Dean
Software Engineer
PrimeFaces 3.4.1
JSF 2.1
Glassfish Server 3.1.2 (development)
Firefox 22


Posts: 5689
@witcradg, very interesting post/response, and thanks for communicating your findings/research and what you learned.

Since PDF is no longer supported with p:media, then use another approach/workaround instead of waiting or hoping for this issue to be fixed/resolved.

PrimeFaces LightBox component should be sufficient to display your PDF in an iFrame. As the showcase example for Lightbox demonstrates, after enduser is finished viewing the document, then click somewhere/anywhere on parent page, and the lightbox/iframe closes or is hidden.

This is almost motivating me to display my PDF content/reports via p:lightBox, but I'm quite satisfied using p:fileDownload to display PDF content/reports in a separate browser tab/window, since the PDF files span entire width of the viewport (or computer monitor), and the same PDF files are printed to printers, attached to emails, or faxed to customers/employees.

Endusers are 'trained' to close browser tab/window that allows them to 'view' the PDF prior to printing emailing or faxing the PDF.
Howard

PrimeFaces 4.0.x, Extensions 1.2.1, Push (Atmosphere 2.1.3)
TomEE 1.6.1 (Tomcat 7.0.53), MyFaces Core 2.2.2
JUEL 2.2.6 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache


Posts: 95
It looks as though an issue has been created to display PDFs inline. Issue number 3533
http://code.google.com/p/primefaces/issues/detail?id=3533

This does not appear not work correctly.
J2EE 6 Glassfish 3.1.2 Mojarra 2.1.26 (SNAPSHOT 20111206) primefaces 4.0
Netbeans 7.2
Browser Firefox 19.0.1 database postgresql-9.1.3


Return to General