DocumentViewer is a JSF wrapper of the PDF.js project of the Mozilla Foundation, a general-purpose, web standards-based platform for parsing and rendering PDFs. It displays PDF documents inline without a browser plugin and supports localized languages (supported browsers).
DocumentViewer can be used in three modes: a url, a JSF resource (library and name)
or a PrimeFaces StreamedContent (value).
It is also possible to set the locale and the starting page of the DocumentViewer.
DocumentViewer has some complicated font handling: this tests a PDF with an embedded font.
<div class="card">
<h5>Basic usage</h5>
<p>DocumentViewer can be used in three modes: a <code>url</code>, a JSF resource (<code>library</code> and <code>name</code>)
or a PrimeFaces <code>StreamedContent</code> (<code>value</code>).</p>
<h:form id="formBasic">
<p:outputPanel deferred="true">
<p:tabView>
<p:tab title="URL">
<pe:documentViewer height="500" url="/ui/media/documentviewer/book.pdf"/>
</p:tab>
<p:tab title="Resource">
<pe:documentViewer height="500" library="books" name="hood.pdf" zoom="page-width"/>
</p:tab>
<p:tab title="StreamedContent">
<pe:documentViewer height="500" value="#{basicDocumentViewerController.content}" zoom="page-width"/>
</p:tab>
</p:tabView>
</p:outputPanel>
</h:form>
</div>
<div class="card">
<h5>Advanced Usage</h5>
<p>It is also possible to set the <code>locale</code> and the starting <code>page</code> of the DocumentViewer.</p>
<h:form id="formAdvanced">
<p:outputPanel layout="block" id="container" deferred="true">
<p:panelGrid columns="2" layout="tabular">
<h:outputLabel value="Page"/>
<p:spinner min="1" max="69" value="#{advancedDocumentViewerController.page}">
<p:ajax update="container"/>
</p:spinner>
<h:outputLabel value="Locale"/>
<p:selectOneRadio value="#{advancedDocumentViewerController.locale}">
<f:selectItem itemValue="en" itemLabel="English"/>
<f:selectItem itemValue="de" itemLabel="German"/>
<f:selectItem itemValue="it" itemLabel="Italian"/>
<f:selectItem itemValue="pt_BR" itemLabel="Portuguese"/>
<p:ajax update="container"/>
</p:selectOneRadio>
</p:panelGrid>
<pe:documentViewer
height="500"
locale="#{advancedDocumentViewerController.locale}"
page="#{advancedDocumentViewerController.page}"
url="/ui/media/documentviewer/book.pdf"/>
</p:outputPanel>
</h:form>
</div>
<div class="card">
<h5>Embedded Fonts</h5>
<p>DocumentViewer has some complicated font handling: this tests a PDF with an embedded font.</p>
<h:form id="formEmbedded">
<p:outputPanel deferred="true">
<pe:documentViewer height="500" url="/ui/media/documentviewer/japanese.pdf"/>
</p:outputPanel>
</h:form>
</div>
package org.primefaces.extensions.showcase.controller.documentviewer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serial;
import java.io.Serializable;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Named;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
@Named
@RequestScoped
public class BasicDocumentViewerController implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private String downloadFileName = "pfe-rocks.pdf";
public StreamedContent getContent() {
try {
return DefaultStreamedContent.builder()
.contentType("application/pdf")
.name("jack.pdf")
.stream(() -> {
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();
return new ByteArrayInputStream(out.toByteArray());
}
catch (Exception e) {
e.printStackTrace();
return null;
}
})
.build();
}
catch (final Exception e) {
e.printStackTrace();
return null;
}
}
public String getDownloadFileName() {
return downloadFileName;
}
public void setDownloadFileName(final String downloadFileName) {
this.downloadFileName = downloadFileName;
}
}
package org.primefaces.extensions.showcase.controller.documentviewer;
import java.io.Serial;
import java.io.Serializable;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
@Named
@ViewScoped
public class AdvancedDocumentViewerController implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private int page = 2;
private String locale = "en";
public int getPage() {
return page;
}
public void setPage(final int page) {
this.page = page;
}
public String getLocale() {
return locale;
}
public void setLocale(final String locale) {
this.locale = locale;
}
}