PDFlib Cookbook

cookbook

form_fields/form_textfield_input_format

Format the data entered in a form field of type "textfield" according to the specified rules

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Form text field input format:
 * Format the data entered in a form field of type "textfield" according to
 * the rules defined.
 * 
 * Create a text field for displaying a price and format the input as
 * "x,xxx.xx" with the text " Euro" appended. 
 * 
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */
/* This is where the data files are. Adjust as necessary */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Form Text Field Input Format";


$width=160; $height=30; $llx = 10; $lly = 700;

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 . "}");

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

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

    $font = $p->load_font("NotoSerif-Regular", "winansi", 
                         "simplefont nosubsetting");
    if ($font == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    /* Start page */
    $p->begin_page_ext(0, 0, " width=a4.width height=a4.height");
    
    /* -----------------------------------------------
     * Format a form text field for displaying a price
     * -----------------------------------------------
     * 
     * For a correct formatting of the number, define an action which
     * formats the number as "x,xxx.xx", e.g. "1,200.50", and appends the
     * text " Euro". 
     */
    $format_action = $p->create_action("JavaScript",
        "script={AFNumber_Format(2, 0, 0, 0, \" Euro\", false); }");
           
    /* Create a form field of type "textfield" called "price" with the
     * JavaScript action supplied in the "action" option 
     */
    $optlist = "backgroundcolor={lightblue} bordercolor={gray 0} " .
        "maxchar=10 scrollable=false tooltip={Enter the price} " .
        "font=" . $font . " fontsize=24 " .  
        " action={format=" . $format_action . "}";
    
    $width=250;
    $p->create_field($llx, $lly, $llx + $width, $lly + $height, "price",
        "textfield", $optlist);
    $lly-=40;
    
    $p->setfont($font, 12);
    $p->fit_textline("Enter a price, e.g. 150.95. A maximum of 10 digits " .
        "is allowed to be entered.", $llx, $lly, 
        "fontname=NotoSerif-Regular fontsize=12");
    $p->fit_textline("After pressing \"Tab\" or \"Enter\", the price is " .
        "formatted as \"x,xxx.xx\" with the text \" Euro\" appended.", 
        $llx, $lly-=20, 
        "fontname=NotoSerif-Regular fontsize=12");
    
    $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=form_textfield_input_format.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;

?>