pe:documentViewer

Community Driven Extensions Project
rlimamiranda
Posts: 8
Joined: 07 Oct 2015, 15:24

26 Oct 2015, 19:17

Good morning , is there any way to specify the file name when the file is downloaded when you're using a DefaultStreamedContent

<pe:documentViewer height="500"
id="documentoView" value="#{managedBean.content}"
style="width:100%;height:100%;" />

Mathieu-Castets
Posts: 45
Joined: 03 Jul 2014, 19:04
Location: Biarritz, France

27 Oct 2015, 11:27

You need to do it programmatically. DefaultStreamedContent has several constructors:
  • DefaultStreamedContent()
  • DefaultStreamedContent(InputStream stream)
  • DefaultStreamedContent(InputStream stream, String contentType)
  • DefaultStreamedContent(InputStream stream, String contentType, String name)
  • DefaultStreamedContent(InputStream stream, String contentType, String name, String contentEncoding)
Just use one of the last two to specify a filename.

Reference: http://www.primefaces.org/docs/api/5.3/ ... ntent.html
PrimeFaces 5.3 - PF Extensions 4.0.0 - OmniFaces 2.2 - Mojara 2.2.12 - GlassFish 4.1.1 - Java 7 - Netbeans 8.1

Ksyusha
Posts: 2
Joined: 22 Jun 2016, 09:30

22 Jun 2016, 10:04

I am using constructor with custom Filename
public DefaultStreamedContent(InputStream stream,
String contentType,
String name)

but when i am trying to save PDF-document its default name is document.pdf.
Also my filename has not been taken.
Could anybody shows working example?

User avatar
Melloware
Posts: 269
Joined: 22 Apr 2013, 15:48

30 Dec 2016, 22:54

It looks like in the pdf.viewer.js which is from the Mozilla PDF.js library they try and guess the name from the URL. But since the streamed content does not have a file name in the URL it can't figure out the name and gives it the default "document.pdf".

Code: Select all

/**
 * Returns the filename or guessed filename from the url (see issue 3455).
 * url {String} The original PDF location.
 * @return {String} Guessed PDF file name.
 */
function getPDFFileNameFromURL(url) {
  var reURI = /^(?:([^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
  //            SCHEME      HOST         1.PATH  2.QUERY   3.REF
  // Pattern to get last matching NAME.pdf
  var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
  var splitURI = reURI.exec(url);
  var suggestedFilename = reFilename.exec(splitURI[1]) ||
                           reFilename.exec(splitURI[2]) ||
                           reFilename.exec(splitURI[3]);
  if (suggestedFilename) {
    suggestedFilename = suggestedFilename[0];
    if (suggestedFilename.indexOf('%') !== -1) {
      // URL-encoded %2Fpath%2Fto%2Ffile.pdf should be file.pdf
      try {
        suggestedFilename =
          reFilename.exec(decodeURIComponent(suggestedFilename))[0];
      } catch(e) { // Possible (extremely rare) errors:
        // URIError "Malformed URI", e.g. for "%AA.pdf"
        // TypeError "null has no properties", e.g. for "%2F.pdf"
      }
    }
  }
  return suggestedFilename || 'document.pdf';
}
PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces 6.1.X / PF Extensions 6.1.5

adamjfusco
Posts: 4
Joined: 14 Aug 2015, 20:09

25 Jan 2017, 19:07

I am also looking for a solution to this and was wondering if anyone has thought of a way around Mozilla PDF.js using the default document.pdf when passing in a StreamedContent object to documentViewer.

User avatar
Melloware
Posts: 269
Joined: 22 Apr 2013, 15:48

25 Jan 2017, 22:58

If you create a ticket on GitHub I will see if there is something clever I can do for the 6.1 Release...

https://github.com/primefaces-extension ... com/issues
PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces 6.1.X / PF Extensions 6.1.5

User avatar
Melloware
Posts: 269
Joined: 22 Apr 2013, 15:48

28 Jan 2017, 16:37

I created issue: https://github.com/primefaces-extension ... issues/436

It is fixed in PF Extensions 6.1. You can use a new property "download" to set the name like...

Code: Select all

<pe:documentViewer height="500" value="#{basicDocumentViewerController.content}" download="extensions-rocks.pdf"/>
PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces 6.1.X / PF Extensions 6.1.5

adamjfusco
Posts: 4
Joined: 14 Aug 2015, 20:09

09 Feb 2017, 19:51

Melloware,
Thank you so much, I cannot wait to test this!

Adam

User avatar
Melloware
Posts: 269
Joined: 22 Apr 2013, 15:48

09 Feb 2017, 20:07

No problem. If you want to see a sneak peak of it in action you can see it on my BETA site for 6.1

http://melloware.dyndns-free.com:8080/pe-showcase/
PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces 6.1.X / PF Extensions 6.1.5

adamjfusco
Posts: 4
Joined: 14 Aug 2015, 20:09

09 Feb 2017, 20:45

Melloware,
It works!!!! - Any estimated release date?
Tooltip is looking pretty sweet too, never noticed that before...

Thank you once again,
Adam

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest