multimedia/javascript_for_renditions
Screen annotation for video: Demonstrate use of Screen annotations, Renditions and Rendition actions to play video and sound.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* JavaScript for renditions:
* Play renditions with JavaScript.
*
* The following use cases are demonstrated:
* - Control a sound rendition from JavaScript actions in bookmarks; the
* rendition is played in a hidden floating window without any Screen
* annotation.
* - Control a video rendition in a Screen annotation with JavaScript.
* - Play sound and video renditions at the same time with JavaScript, also
* without Screen annotation.
*
* Required software: PDFlib/PDFlib+PDI/PPS 10
* Required data: video and sound files
*
*/
/* This is where the data files are. Adjust as necessary */
$searchpath = "../input";
$outfile = "";
$title = "JavaScript for renditions";
$exitcode = 0;
try {
$videofile = "mountains.mp4"; // video file
$soundfile = "chilled.mid"; // sound file
$p = new pdflib();
/* This means we must check return values of load_asset() etc. */
$p->set_option("errorpolicy=return");
$p->set_option("searchpath={" . $searchpath . "}");
if ($p->begin_document($outfile, "destination={type=fitwindow}") == 0)
throw new Exception(
"Error: " . $p->get_apiname() . ": " . $p->get_errmsg());
$p->set_info("Creator", "PDFlib Cookbook");
$p->set_info("Title", $title);
// ================================================================
// Create a hidden rendition from the sound file and store it in the name tree
$rend_sound = $p->load_asset("Rendition", $soundfile, "mimetype=audio/midi style=hidden nametree name=rendition_sound");
if ($rend_sound == 0)
throw new Exception("Error: " . $p->get_errmsg());
// JavaScript for initializing the player
$js_init_player =
"// Initialize the player if not yet done; use as document open action\n" .
"\n" .
"var player = null;\n" .
"\n" .
"function init_player() {\n" .
" if (player && player.isOpen)\n" .
" return;\n" .
"\n" .
" // Retrieve rendition by name and play it in a floating window.\n" .
" var rendition = this.media.getRendition(\"rendition_sound\");\n" .
"\n" .
" if (rendition == null)\n" .
" app.alert(\"Couldn't find Rendition\");\n" .
" else {\n" .
" var floating = {\n" .
" width: 400,\n" .
" height: 300\n" .
" };\n" .
" player = app.media.openPlayer({\n" .
" rendition: rendition,\n" .
" settings: {\n" .
" floating: floating\n" .
" }\n" .
" });\n" .
" }\n" .
"}";
// Load JavaScript string into a virtual (PVF) file
$p->create_pvf("/pvf/javascript/init_player.js", $js_init_player, "");
// Create a JavaScript asset from PVF file
$js_init_player_asset = $p->load_asset("JavaScript", "/pvf/javascript/init_player.js", "");
if ($js_init_player_asset == 0)
throw new Exception("Error: " . $p->get_errmsg());
// Create a JavaScript action from the asset
$act_init = $p->create_action("JavaScript", "javascript=" . $js_init_player_asset);
// The init action is attached to the document open action in
// $p->end_document() later to ensure that the code is available on each page
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
$parent_bookmark = $p->create_bookmark("control sound with JavaScript (click bookmarks)", "open");
// Simple JavaScript actions for controlling playback
$act_play_sound = $p->create_action("JavaScript", "script={init_player(); player.play();}");
$act_stop_sound = $p->create_action("JavaScript", "script={init_player(); player.stop();}");
$act_pause_sound = $p->create_action("JavaScript", "script={init_player(); player.pause();}");
// Bookmarks for controlling playback with JavaScript actions
$p->create_bookmark("play or resume",
"action={activate=" . $act_play_sound . "} parent=" . $parent_bookmark);
$p->create_bookmark("stop",
"action={activate=" . $act_stop_sound . "} parent=" . $parent_bookmark);
$p->create_bookmark("pause",
"action={activate=" . $act_pause_sound . "} parent=" . $parent_bookmark);
$p->end_page_ext("");
// ================================================================
// Create a rendition from the video file
$rend_video = $p->load_asset("Rendition", $videofile, "mimetype=video/mp4 name=rendition_video1 controller");
if ($rend_video == 0)
throw new Exception("Error: " . $p->get_errmsg());
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
$p->create_bookmark("play video with simple JavaScript (click annotation)", "open");
// Create a rendition action for playing the video with JavaScript
$act = $p->create_action("Rendition", "target=annot_video_javascript script={app.media.openPlayer();} rendition=" . $rend_video);
// Create a Screen annotation where the video will be played
$p->create_annotation(25, 300, 575, 610, "Screen",
"title=annot_video_javascript name=annot_video_javascript action={activate=" . $act . "}");
$p->end_page_ext("");
// ================================================================
// Create a rendition from the video file and store it in the name tree
// to make it accessible to JavaScript
$rend_video = $p->load_asset("Rendition", $videofile, "mimetype=video/mp4 nametree name=rendition_video2 controller");
if ($rend_video == 0)
throw new Exception("Error: " . $p->get_errmsg());
// JavaScript for retrieving and playing a named rendition
$js_video =
"var rendition = this.media.getRendition(\"rendition_video2\");\n" .
"\n" .
"if (rendition == null)\n" .
" app.alert(\"Couldn't find rendition\");\n" .
"else {\n" .
" var floating = {\n" .
" align: app.media.align.topCenter,\n" .
" canResize: app.media.canResize.yes,\n" .
" width: 400,\n" .
" height: 300\n" .
" };\n" .
" var player = app.media.openPlayer({\n" .
" rendition: rendition,\n" .
" settings: {\n" .
" windowType: app.media.windowType.floating,\n" .
" floating: floating\n" .
" }\n" .
" });\n" .
"}\n";
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
// Load JavaScript string into a virtual (PVF) file
$p->create_pvf("/pvf/javascript/video.js", $js_video, "");
// Create a JavaScript asset from PVF file
$js_video_asset = $p->load_asset("JavaScript", "/pvf/javascript/video.js", "");
if ($js_video_asset == 0)
throw new Exception("Error: " . $p->get_errmsg());
// Create a JavaScript action from the asset
$act_play_video = $p->create_action("JavaScript", "javascript=" . $js_video_asset);
// The floating window created by the action does not depend on the page
$p->create_bookmark("play sound and video with JavaScript (click bookmark)",
"action={activate={" . $act_play_sound . " " . $act_play_video . "}}");
$p->end_page_ext("");
// Attach the init action for the sound rendition above
$p->end_document("action={open=" . $act_init . "}");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=javascript_for_renditions.pdf");
print $buf;
} catch (PDFlibException $e) {
print("PDFlib exception occurred:\n".
"[" . $e->get_errnum() . "] " . $e->get_apiname() .
": " . $e->get_errmsg() . "\n");
exit(1);
} catch (Exception $e) {
print($e->getMessage());
exit(1);
}
$p = 0;
?>