PDFlib Cookbook

cookbook

text_output/text_as_clipping_path

Output text filled with an image.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Text as clipping path:
 * Output text filled with an image 
 *  
 * Use various text rendering modes to place an image clipped by some text
 * outlines.
 * Use the "textrendering=7" parameter to add text to the clipping path. Then
 * place an image clipped by that path.
 * Use "textrendering=5" to stroke text and add it to the clipping path. Then
 * place an image clipped by that path.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: image file
 */

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

$imagefile = "nesrin.jpg";

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

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

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

    /* Load the image */
    $image = $p->load_image("auto", $imagefile, "");
    if ($image == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    /* Start an A4 landscape page */
    $p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
    
    /* Save the current graphics state */
    $p->save();
    
    /*
     * Output some text with the text rendering mode set to "add text
     * to the clipping path". The text outlines will not actually be
     * filled but be added to the clipping path instead.
     */
    $p->fit_textline("Hello!", 30, 250,
        "fontname=NotoSerif-Bold fontsize=250 textrendering=7");
    
    /* Place the image */
    $p->fit_image($image, 0.0, 0.0, "boxsize={842 595} fitmethod=entire");
     
    /* Restore the current graphics state */
    $p->restore();
    
    /* Save the current graphics state */
    $p->save();
    
    /*
     * Output some text with the text rendering mode set to "stroke 
     * text and add it to the clipping path". The text outlines will
     * not actually be filled but be added to the clipping path
     * instead.
     */
    $p->set_graphics_option("linewidth=8");
    $p->fit_textline("Hello!", 100, 50,
        "fontname=NotoSerif-Bold fontsize=200 " .
        "strokecolor={rgb 0.1 0.1 0.1} textrendering=5");
    
    /* Place the image */
    $p->fit_image($image, 0.0, 0.0, "boxsize={842 595} fitmethod=entire");
            
    $p->close_image($image);
          
    /* Restore the current graphics state */
    $p->restore();
    
    $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_as_clipping_path.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;

?>