PDFlib Cookbook

cookbook

general/aes256_unicode_password

Create AES-256-encrypted document with a master password containing Unicode characters.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/* 
 *
 * 
 * Demonstrate AES-256 encryption and Unicode passwords.
 * 
 * The file can only be opened with Acrobat X and later.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: font
 */
$outfile = "";
$title = "AES-256 encryption and Unicode passwords";

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


try {
    $p = new PDFlib();

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

    $p->set_option("searchpath={" . $searchpath . "}");
    $p->set_option("stringformat=utf8");
    $p->set_option("charref=true");
    
    /*
     * The password demonstrates of Unicode passwords:
     * 
     * - Greek characer U+0348 PSI
     *   
     * - Normalization of Unicode passwords (LATIN SMALL LIGATURE FI
     *   is normalized to "fi")
     */
    $greek_letter = "\u{03A8}";
    $ligature = "\u{FB01}";
    $normalized_ligature ="fi";
    
    $password = $greek_letter . " " . $ligature;
    $normalized_password = $greek_letter . " " 
                                . $normalized_ligature;

    /*
     * To get AES-256 encryption the PDF version must be set to
     * PDF 1.7 extension level 8.
     */
    $optlist = "compatibility=1.7ext8 "
            . "masterpassword={\xEF\xBB\xBF" . $password . "}";

    if ($p->begin_document($outfile, $optlist) == 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.width height=a4.height");

    $p->fit_textline("AES-256 encryption and Unicode passwords", 50, 700,
                "fontname=NotoSerif-Regular encoding=unicode fontsize=24");
    
    $text =
        "This document produced by PDFlib is AES-256-encrypted. \n\n"
        . "The master password of this document is:\n\n"
        . $password
        . "\n\nUse copy&paste to enter the password in the Acrobat "
        . "prompt for changing the security settings of the document.\n\n"
        . "Because the password is normalized, it can also be "
        . "entered like this (note the decomposed ligature):\n\n"
        . $normalized_password;

    $tf = $p->add_textflow(0, $text, 
        "fontname=NotoSerif-Regular fontsize=16 encoding=unicode leading=150%");
    if ($tf == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    $p->fit_textflow($tf, 50, 50, 500, 650, "");
    
    $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=starter_basic.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);
    exit(1);
}

$p = 0;
?>