PDFlib Cookbook

cookbook

text_output/watermark

Create an editable watermark.

Download PHP Code  Switch to Java Code  Show Output 

<?php

/*
 *
 *
 * Create an editable watermark
 * 
 * Required software: PDFlib 9.0.6
 * Required data: image file
 */

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

$imagefile = "nesrin.jpg";

try {
    $p = new pdflib();
    
    /* This means we must check return values of load_font() etc. */
    $p->set_option("errorpolicy=return SearchPath={{" . $searchpath . "}}");
    $p->set_option("stringformat=utf8");

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

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

    /*
     * Create watermark. The watermark by default applies to all
     * pages in the document
     */
    $p->begin_template_ext(0, 0, "watermark={location=ontop opacity=60%}");

    $p->fit_textline("Preliminary", 0, 0,
            "fontsize=12 fontname=Helvetica-Bold encoding=unicode "
                    . "fillcolor=red boxsize={595 842} stamp=ll2ur");

    $p->end_template_ext(0, 0);

    /*
     * Create page content.
     */
    $image = $p->load_image("auto", $imagefile, "");

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

    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
    $p->fit_textline("The watermark can be edited in Acrobat", 20, 600,
            "fontname=Helvetica encoding=unicode fontsize=14");

    $p->fit_image($image, 0.0, 0.0, "boxsize={595 600} fitmethod=meet");
    $p->close_image($image);
    $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=text_on_color.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;
?>