APPO PDF Book Reader embeds PDF books in WordPress pages and posts. Readers can jump to a page, zoom, search selectable text and open bookmarks when the PDF includes them. The reader runs on your site with a bundled copy of Mozilla PDF.js.
Installation and usage
Install and activate the plugin ZIP under Plugins → Add New Plugin → Upload Plugin. Upload a PDF to the Media Library, copy its file URL and add a Shortcode block to your page.
[appo_pdf_book url="https://example.com/wp-content/uploads/my-book.pdf" title="My book" height="720"]
You can use the Media Library attachment ID instead of the URL.
[appo_pdf_book id="123" title="My book"]
The optional height is in pixels and defaults to 720. The viewer adapts to the available width and includes links to open and download the original PDF. Image-only scans need an embedded text layer for search.
The PDF must share the viewer’s origin. Use publicly accessible files unless your server already protects the PDF URL. A password on a WordPress page does not protect a standard Media Library file. Your server must serve .mjs files with a JavaScript MIME type.
Source code
Release 1.0.0 requires WordPress 6.0 or later and PHP 7.4 or later. The WordPress integration is licensed under GPL-3.0-or-later. The bundled Mozilla PDF.js 6.5.438 legacy build is distributed under Apache-2.0, with upstream notices for bundled assets.
appo-pdf-book-reader.php
<?php
/**
* Plugin Name: APPO PDF Book Reader
* Description: Embed a responsive, self-hosted PDF book reader using a shortcode.
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 7.4
* Author: Marco Digireale Costanzo
* Author URI: https://marcocostanzo.com/
* License: GPL-3.0-or-later
* Text Domain: appo-pdf-book-reader
*/
namespace Appo\PdfBookReader;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
const VERSION = '1.0.0';
/**
* Compare HTTP origins, accounting for the default port of each scheme.
*/
function http_origin( string $url ): string {
$parts = wp_parse_url( $url );
if ( empty( $parts['scheme'] ) || empty( $parts['host'] ) ||
isset( $parts['user'] ) || isset( $parts['pass'] ) ) {
return '';
}
$scheme = strtolower( $parts['scheme'] );
if ( ! in_array( $scheme, array( 'http', 'https' ), true ) ) {
return '';
}
$port = $parts['port'] ?? ( 'https' === $scheme ? 443 : 80 );
return $scheme . '://' . strtolower( $parts['host'] ) . ':' . $port;
}
/**
* Render a PDF book from a Media Library attachment ID or an absolute PDF URL.
*/
function render_reader( $attributes ): string {
$options = shortcode_atts(
array( 'id' => '', 'url' => '', 'title' => '', 'height' => '720' ),
$attributes,
'appo_pdf_book'
);
$id = trim( $options['id'] );
$title = sanitize_text_field( $options['title'] );
if ( '' !== $id ) {
$attachment_id = ctype_digit( $id ) ? (int) $id : 0;
if ( 'application/pdf' !== get_post_mime_type( $attachment_id ) ||
'attachment' !== get_post_type( $attachment_id ) ||
'trash' === get_post_status( $attachment_id ) ) {
return '';
}
$pdf_url = wp_get_attachment_url( $attachment_id );
if ( '' === $title ) {
$title = sanitize_text_field( get_the_title( $attachment_id ) );
}
} else {
$pdf_url = trim( $options['url'] );
$path = wp_parse_url( $pdf_url, PHP_URL_PATH );
if ( ! is_string( $path ) ||
'pdf' !== strtolower( pathinfo( rawurldecode( $path ), PATHINFO_EXTENSION ) ) ) {
return '';
}
}
if ( ! $pdf_url ) {
return '';
}
$viewer_url = plugins_url( 'vendor/pdfjs/web/viewer.html', __FILE__ );
$origin = http_origin( $pdf_url );
if ( '' === $origin || $origin !== http_origin( $viewer_url ) ) {
return '';
}
$pdf_url = esc_url_raw( $pdf_url, array( 'https', 'http' ) );
if ( '' === $pdf_url ) {
return '';
}
$viewer_src = add_query_arg( 'file', rawurlencode( $pdf_url ), $viewer_url );
$height = filter_var( $options['height'], FILTER_VALIDATE_INT );
$height = false === $height ? 720 : max( 320, min( 1600, $height ) );
$title = '' !== $title ? $title : __( 'PDF book', 'appo-pdf-book-reader' );
wp_enqueue_style(
'appo-pdf-book-reader',
plugins_url( 'assets/reader.css', __FILE__ ),
array(),
VERSION
);
return sprintf(
'<div class="appo-pdf-book-reader"><iframe src="%1$s" title="%2$s" width="100%%" height="%3$d" loading="lazy" referrerpolicy="same-origin" allowfullscreen></iframe><p class="appo-pdf-book-reader__links"><a href="%4$s" target="_blank" rel="noopener">%5$s</a> <a href="%4$s" download>%6$s</a></p></div>',
esc_url( $viewer_src ),
esc_attr( $title ),
$height,
esc_url( $pdf_url ),
esc_html__( 'Open PDF in a new tab', 'appo-pdf-book-reader' ),
esc_html__( 'Download PDF', 'appo-pdf-book-reader' )
);
}
add_shortcode( 'appo_pdf_book', __NAMESPACE__ . '\\render_reader' );
assets/reader.css
.appo-pdf-book-reader {
max-width: 100%;
margin-block: 1.5rem;
}
.appo-pdf-book-reader > iframe {
display: block;
box-sizing: border-box;
width: 100%;
max-width: 100%;
max-height: max(320px, 85vh);
border: 1px solid currentColor;
border-radius: 0.35rem;
}
.appo-pdf-book-reader__links {
display: flex;
flex-wrap: wrap;
gap: 0.35rem 1.25rem;
margin-block: 0.75rem 0;
}
pdfjs-viewer.patch
Extract the official PDF.js legacy archive into vendor/pdfjs/, then apply the following patch from that directory. The release omits the upstream sample PDF and debugging source maps. See the upstream PDF.js source and license notices.
--- a/web/viewer.mjs
+++ b/web/viewer.mjs
@@ -5373,10 +5373,10 @@
value: "",
kind: OptionKind.VIEWER + OptionKind.PREFERENCE
}], ["annotationEditorMode", {
- value: 0,
+ value: -1,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE
}], ["annotationMode", {
- value: 2,
+ value: 1,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE
}], ["capCanvasAreaFactor", {
value: 200,
@@ -5391,7 +5391,7 @@
value: "./debugger.mjs",
kind: OptionKind.VIEWER
}], ...[["defaultUrl", {
- value: "compressed.tracemonkey-pldi-09.pdf",
+ value: "",
kind: OptionKind.VIEWER
}]], ["defaultZoomDelay", {
value: 400,
@@ -5406,7 +5406,7 @@
value: false,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE
}], ...[["disablePreferences", {
- value: false,
+ value: true,
kind: OptionKind.VIEWER
}]], ...[], ["enableAltText", {
value: false,
@@ -5451,7 +5451,7 @@
value: true,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE
}], ["enableScripting", {
- value: true,
+ value: false,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE
}], ["enableSelectionRendering", {
value: true,
--- a/web/viewer.html
+++ b/web/viewer.html
@@ -29,7 +29,7 @@
<meta
http-equiv="Content-Security-Policy"
- content="default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:; style-src 'self'; img-src 'self' blob: data:; media-src blob:; font-src 'self' data:; connect-src * blob: data:; base-uri 'none'; form-action 'none';"
+ content="default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:; style-src 'self'; img-src 'self' blob: data:; media-src blob:; font-src 'self' data:; connect-src 'self' blob: data:; base-uri 'none'; form-action 'none';"
/>
<!-- This snippet is used in production (included from viewer.html) -->
Write to Marco on WhatsApp: 340 49 31 860 about this page.