Dynamically set the name of the PDF in documentViewer

Community Driven Extensions Project
Post Reply
ignLadd
Posts: 2
Joined: 28 Nov 2019, 12:01

28 Nov 2019, 13:17

Hello,

Is there a way to parameterize the value of the download attribute of the documentViewer ? I need to set it dynamically.

page:

Code: Select all

<p:dialog id="showReport" widgetVar="showReport" modal="true" resizable="true" dynamic="true">
		<pe:documentViewer cache="true" id="pdfvisualizer" height="500" width="1200" 
		value="#{documentViewer.report}" zoom="page-width" download="parametrize-me-pls.pdf"/>
	    </p:dialog>
bean:

Code: Select all

@SessionScoped
public class DocumentViewerBean  implements Serializable {
	
	private static final long serialVersionUID = -565408561080994105L;

	private StreamedContent report;
	private String nameFile;
	
	public synchronized void getReport(AnagReg reg) throws FileNotFoundException{
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		nameFile = "REG_" + reg.getId().toString() + "_" + sdf.format(new Date()) + ".pdf" ;
		report =  new RegDataReport().buildReportPdf(nameFile, reg);
	}
	
	public StreamedContent getReport() {
		return report;
	}

	public void setReport(StreamedContent report) {
		this.report = report;
	}

	public String getNameFile() {
		return nameFile;
	}

	public void setNameFile(String nameFile) {
		this.nameFile = nameFile;
	}
}
I have already tried to pass the nameFile attribute using expression language [ download="#{documentViewer.nameFile}" ] but it doesn't seem to work

Thank you for your availability.
Primefaces 7.0
Primefaces-extensions 7.0.1
Omnifaces 2.7.1
WildFly Server 10

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

28 Nov 2019, 17:01

The "download" attribute should work if its STREAMED content but the file name MUST end in ".pdf".
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

28 Nov 2019, 17:03

Oh I think you are saying you can't use a ValueExpression as the download name it only works if it is hard coded?
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

28 Nov 2019, 18:42

I see your problem. The #{namedFile} is not yet created in the bean by the time the component renders. Because I just tried this and it worked fine and my PDF downloaded as "pfe-rocks.pdf"

Code: Select all

<pe:documentViewer  value="#{basicDocumentViewerController.content}" download="#{basicDocumentViewerController.downloadFileName}"/>

Code: Select all

public class BasicDocumentViewerController implements Serializable {

	private static final long serialVersionUID = 1L;

	private String downloadFileName = "pfe-rocks.pdf";
	private StreamedContent content;

	public void onPrerender(final ComponentSystemEvent event) {

		try {

			final ByteArrayOutputStream out = new ByteArrayOutputStream();

			final Document document = new Document();
			PdfWriter.getInstance(document, out);
			document.open();

			for (int i = 0; i < 50; i++) {
				document.add(new Paragraph("All work and no play makes Jack a dull boy"));
			}

			document.close();
			content = new DefaultStreamedContent(new ByteArrayInputStream(out.toByteArray()), "application/pdf");
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public StreamedContent getContent() {
		return content;
	}

	public void setContent(final StreamedContent content) {
		this.content = content;
	}
	
	public String getDownloadFileName() {
		return downloadFileName;
	}

	public void setDownloadFileName(final String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}
}

PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

ignLadd
Posts: 2
Joined: 28 Nov 2019, 12:01

29 Nov 2019, 13:18

yes, it works perfectly. thank you so much.
Primefaces 7.0
Primefaces-extensions 7.0.1
Omnifaces 2.7.1
WildFly Server 10

Post Reply

Return to “Extensions”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 18 guests