Search code examples
phppdflib

How to Use--PDF_add_thumbnail function of PHP?


I have a PDF file ad I want to add a image in the document, so I found a function in PDF lib as PDF_add_thumbnail so could any give me an example of how to use the function thru an example?


Solution

  • To add thumbnail you first need to create thumbnail image of the page. Assuming you have the image (this could be another question if you don't know how to create thumbnail for given PDF page) you can add image as thumbnail. Checkout following code.

    
    //getting new instance
    $pdfFile = new_pdf();
    
    PDF_open_file($pdfFile, " ");
    
    //document info
    pdf_set_info($pdfFile, "Auther", "Auther Name");
    pdf_set_info($pdfFile, "Title", "My PDF Title");
    pdf_set_info($pdfFile, "Subject", "PAGE THUMBNAIL");
    
    //starting our page and define the width and highet of the document
    pdf_begin_page($pdfFile, 595, 842);
    
    //start writing from the point 50,780
    PDF_show_xy($pdfFile, "Thumbnail will be added for this page.", 50, 780);
    
    //load image file 
    $image = PDF_load_image($pdfFile,"jpg","pagethumb.jpg","");
    
    //add image as thumbnail - this will be thumbnail for this page
    PDF_add_thumbnail ( $pdfFile , $image );
    
    PDF_end_page($pdfFile);
    PDF_close($pdfFile);
    
    //store the pdf document in $pdf
    $pdf = PDF_get_buffer($pdfFile);
    //get  the len to tell the browser about it
    $pdflen = strlen($pdfFile);
    
    //telling the browser about the pdf document
    header("Content-type: application/pdf");
    header("Content-length: $pdflen");
    header("Content-Disposition: inline; filename=pagethumb.pdf");
    //output the document
    print($pdf);
    //delete the object
    PDF_delete($pdfFile);