PDFlib Cookbook

cookbook

pdfa/xmp_extension_schema_pdfa2b updated

Demonstrate an XMP extension schema for PDF/A-1.

Download PHP Code  Switch to Java Code  Show Output  Show Input (machine_extension_schema_1.xmp) 

<?php
/*
 * PDF/A extension schema:
 * Demonstrate the use of an XMP extension schema as defined in PDF/A-1
 * 
 * Create a PDF/A document and use the "metadata" option of begin_document() to
 * supply an XMP file containing an XMP extension schema.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: XMP file
 */
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "PDF/A Extension Schema";

$xmpfile = "machine_extension_schema_1.xmp";

try {
    $p = new PDFlib();

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

    /* This means we must check return values of load_font() etc. */
    $p->set_option("errorpolicy=return");
    
    /* Start a PDF/A document with an XMP file supplied, containing an XMP
     * extension schema. The XMP metadata will be read from the file and
     * embedded in the document. PDFlib will merge several internally
     * generated entries into the user-supplied XMP, e.g. xmp:CreateDate. 
     */
    if ($p->begin_document($outfile, 
        "pdfa=PDF/A-2b metadata={filename=" . $xmpfile . "}") == 0)
        throw new Exception("Error: " . $p->get_errmsg());

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

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

    $font = $p->load_font("NotoSerif-Regular", "unicode", "");

    if ($font == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $p->setfont($font, 12);

    $p->fit_textline("An XMP extension schema is read from an XMP file and " .
             "the XMP metadata is embedded in the document.", 50, 400, "");

    $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=xmp_extension_schema_pdfa2b.pdf");
    print $buf;

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