jsf - JSF2.0: Show certain pdf page on load -
i'd open pdf in new page jsf2, , display page in pdf on load. have kind of toc in jsf page, , want jump there page in pdf directly.
what know (this not, need, example of giving adobe reader , other pdf readers page want jump to): open page (chose internet): https://www.cdc.gov/diabetes/pdfs/data/statistics/national-diabetes-statistics-report.pdf#page=10
the #page=10 makes pdf plugin of browser display page 10.
requirements selecting pdf:
- pdf dynamically downloaded webservice according id must reside in managedbeans, since it's secret, , should not passed others (like session id...) (below given anser me passes id in get-parameter, should not done)
- pdf should not reside in filesystem, sinc don't want handling of temporary files (below given answer me utilizes pdfs on fs, stream not work)
now real problem: have change url beeing displayed/used in jsf, can't use normal way , includeviewparams, because insert "?", , not "#" in url.
also, have backing bean, gets content of pdf backend service, based on other parameters i'm giving, solution cool, i'm aware not possible...
does have idea, how solve this? didn't include code, since doesn't work anyways, , need new way solve anyways...
turns out, primefaces has implemented (although implementation has it's restrictions):
<p:media player="pdf" value="#{viewerbean.media}" width="100%" height="100%"> <f:param name="#page" value="#{viewerbean.pagenumber}"/> <f:param name="toolbar" value="1"/> <!--<f:param name="search" value="#{viewerbean.querytext}"/>--> </p:media>
https://www.primefaces.org/showcase/ui/multimedia/media.xhtml
restriction: can't read stream, @ least not stable. save energy, , write stream temp file, , set filename dynamically. not sure, whether complete, should idea:
import javax.faces.bean.managedproperty; import javax.faces.bean.requestscoped; import java.io.*; import javax.annotation.postconstruct; import java.nio.file.files; import java.nio.file.paths; @managedbean @requestscoped public class viewerbean implements serializable { @managedproperty(value = "#{param.page}") private string pagenumber; private file media; @postconstruct public void init() { try { media = files.createtempfile("car", ".pdf").tofile(); try (fileoutputstream outputstream = new fileoutputstream(media)) { ioutils.copy(getstreamedcontent().getstream(), outputstream); } } catch (ioexception e) { logger.error(e); throw new runtimeexception("error creating temp file", e); } } public streamedcontent getmedia() { try { return new defaultstreamedcontent(new fileinputstream(media), "application/pdf"); } catch (filenotfoundexception e) { string message = "error reading file " + media.getabsolutepath(); logger.error(message, e); throw new runtimeexception(message, e); } } }
if pagename not needed, use this: http://balusc.omnifaces.org/2006/05/pdf-handling.html
maybe if can utilize outputlink you'll lucky, ran out of time test option.
Comments
Post a Comment