PDFlib Cookbook

cookbook

multimedia/movie_annotations

Demonstrate a movie annotation in PDF.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * 
 * RichMedia annotation for Video:
 * Demonstrate the use of a RichMedia annotation to play a Video with
 * Flash controls. 
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: movie file, Shockwave (*.swf) Video player and video skin

 */
/* This is where the data files are. Adjust as necessary */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Movie Annotations";


/* movie file to be referenced by the link */
$moviefile = "mountains.mp4";

/* Auxiliary Flash files for controlling the video in PDF */
$videoplayerfile = "videoplayer.swf";
$videoskinfile = "videoskin.swf";

try {
    $width = 550; $height = 310;

    $p = new pdflib();

    /* This means we must check return values of load_asset() etc. */
    $p->set_option("errorpolicy=return");
    $p->set_option("stringformat=utf8");

    $p->set_option("searchpath={" . $searchpath . "}");

    if ($p->begin_document($outfile, "compatibility=1.7ext3") == 0)
        throw new Exception("Error: " . $p->get_apiname() . ": "
            . $p->get_errmsg());

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);

    // Create template for visualizing the video before activation.
    // You could display an image; we display only a rectangle.
    
    $templ = $p->begin_template_ext($width, $height, "");
    
    $p->set_graphics_option("fillcolor=red");
    $p->rect(0, 0, $width, $height);
    $p->fill();

    $p->fit_textline("Video", 10, 10,
        "fontname=NotoSerif-Regular encoding=unicode fontsize=14 " .
        "fitmethod=meet fillcolor=black position=center boxsize={" . 
        $width . " " . $height . "}");

    $p->end_template_ext(0, 0);

    
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");

    // Load video and auxiliary Flash files as RichMedia assets
    $movie = $p->load_asset("Video", $moviefile, "");
    if ($movie == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    $videoplayer = $p->load_asset("Flash", $videoplayerfile, "");
    if ($videoplayer == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    $videoskin = $p->load_asset("Flash", $videoskinfile, "");
    if ($videoskin == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    /* The option list for the RichMedia annotation specifies the
     * template as static placeholder, controls video embedding and
     * defines the the Flash helpers for playing.
     */
 
    $optlist =
    "template={normal=" . $templ . "} " .
    "richmedia={" .
        "assets={" .
            "{asset=" . $movie       . " name=" . $moviefile .  "} " .
            "{asset=" . $videoplayer . " name=" . $videoplayerfile . "} " .
            "{asset=" . $videoskin   . " name=" . $videoskinfile . "} " .
        "} " .
        "configuration={" .
            "type=Video name=DefaultConfiguration " .
            "instances={{asset=" . $videoplayerfile . " " .
                "params={binding=background " .
                "flashvars={source=" . $moviefile .
                "&skin=" . $videoskinfile . "}}}} " .
        "} " .
    "}";

    // Create the RichMedia annotation with Flash-controlled Video
    $p->create_annotation(25, 300, 575, 610, "RichMedia", $optlist);

    $p->end_page_ext("");

    $p->end_document("");

    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=movie_annontations.pdf");
    print $buf;

} catch (PDFlibException $e) {
    echo("PDFlib exception occurred:\n".
        "[" . $e->get_errnum() . "] " . $e->get_apiname() .
        ": " . $e->get_errmsg() . "\n");
    exit(1);
} catch (Exception $e) {
    echo($e->getMessage());
    exit(1);
}

$p = 0;

?>