Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

create_annotated_pdf()

Description

Create annotations PDF (old annotate plugin logic)

Parameters

ColumnTypeDefaultDescription
$ref int
$is_collection bool false
$size string "letter"
$cleanup bool false
$preview bool false

Location

include/annotation_functions.php lines 728 to 998

Definition

 
function create_annotated_pdf($ref$is_collection false$size "letter"$cleanup false$preview false)
{
    
# function to create annotated pdf of resources or collections.
    # This leaves the pdfs and jpg previews in filestore/annotate so that they can be grabbed later.
    # $cleanup will result in a slightly different path that is not cleaned up afterwards.

    
global $contact_sheet_preview_size,$lang,$userfullname,$view_title_field,$baseurl,$imagemagick_path,$imagemagick_colorspace,$previewpage,$access;
    
$date date("m-d-Y h:i a");

    include_once 
RESOURCESPACE_BASE_PATH '/include/image_processing.php';

    
$pdfstoragepath get_annotate_file_path($reftrue"pdf");
    
$jpgstoragepath get_annotate_file_path($reftrue"jpg");
    
$pdfhttppath get_annotate_file_path($reffalse"pdf");

    if (
$is_collection) {
        
$collectiondata get_collection($ref);
        
$resources do_search("!collection$ref");
    } else {
        
$resourcedata get_resource_data($ref);
        
$resources do_search("!list$ref");
    }
    
search_add_annotation_count($resources);

    
$size mb_strtolower($size);
    if (
count($resources) == 0) {
        echo 
"nothing";
        exit();
    }
    if (
$size == "a4") {
        
$width 210 25.4;
        
$height 297 25.4;
    } 
// convert to inches
    
if ($size == "a3") {
        
$width 297 25.4;
        
$height 420 25.4;
    }
    if (
$size == "letter") {
        
$width 8.5;
        
$height 11;
    }
    if (
$size == "legal") {
        
$width 8.5;
        
$height 14;
    }
    if (
$size == "tabloid") {
        
$width 11;
        
$height 17;
    }
    
#configuring the sheet:
    
$pagesize[0] = $width;
    
$pagesize[1] = $height;
    
debug("width = {$width}");
    
debug("height = {$height}");

    
$mypdf annotation_pdf_class();
    
$pdf = new $mypdf("portrait""in"$sizetrue'UTF-8'false);
    
$pdf->SetFont('helvetica'''8);
    
// set document information
    
$pdf->SetCreator(PDF_CREATOR);
    
$pdf->SetAuthor($userfullname);
    if (
$is_collection) {
        
$pdf->SetTitle(i18n_get_collection_name($collectiondata) . ' ' $date);
    } else {
        
$pdf->SetTitle(i18n_get_translated($resourcedata['field' $view_title_field]) . ' ' $date);
    }
    
$pdf->SetSubject($lang['annotate_annotations_label']);
    
$pdf->setPrintHeader(false);
    
$pdf->setPrintFooter(false);
    
$pdf->setMargins(.5.5.5);
    
$page 1;
    
$totalpages 1;
    
$m 1;
    do 
// Do the following for each pdf page
        
{
        
// Add a page for each resource
        
for ($n 0$n count($resources); $n++) {
            
$pdf->AddPage();
            
$currentpdfpage $pdf->getPage();
            
$resourcedata $resources[$n];
            
$ref $resources[$n]['ref'];
            
debug("ref = {$ref}");
            
debug("currentpdfpage = {$currentpdfpage}");
            
$access get_resource_access($resources[$n]['ref']); // feed get_resource_access the resource array rather than the ref, since access is included.
            
$use_watermark check_use_watermark();

            
$imgpath get_resource_path($reftrue"hpr"false"jpg", -1$page$use_watermark);
            if (!
file_exists($imgpath)) {
                
$imgpath get_resource_path($reftrue"lpr"false"jpg", -1$page$use_watermark);
            }
            if (!
file_exists($imgpath)) {
                
$imgpath get_resource_path($reftrue"scr"false"jpg", -1$page$use_watermark);
            }
            if (!
file_exists($imgpath)) {
                
$imgpath get_resource_path($reftrue""false"jpg", -1$page$use_watermark);
            }
            if (!
file_exists($imgpath)) {
                
$imgpath get_resource_path($reftrue"pre"false"jpg", -1$page$use_watermark);
            }
            if (!
file_exists($imgpath)) {
                continue;
            }
            
$imagesize getimagesize($imgpath);

            
$whratio $imagesize[0] / $imagesize[1];

            if (
$whratio 1) {
                
$imageheight $height 4// vertical images can take up half the page
                
$whratio $imagesize[0] / $imagesize[1];
                
$imagewidth $imageheight $whratio;
            }

            if (
$whratio >= || $imagewidth $width 1) {
                
$imagewidth $width 1// horizontal images are scaled to width - 1 in
                
$hwratio $imagesize[1] / $imagesize[0];
                
$imageheight $imagewidth $hwratio;
            }

            
$image_x = (($width 1) / 2) - (($imagewidth 1) / 2); # Center image horizontally
            
$image_y 1;

            
$pdf->Text(.5.5i18n_get_translated($resourcedata['field' $view_title_field]) . ' ' $date);
            
$pdf->Image($imgpath$image_x$image_y$imagewidth$imageheight'jpg'"{$baseurl}/?r={$ref}");

            
// set color for background
            
$pdf->SetFillColor(255255200);

            
$style = array('width' => 0.01'cap' => 'butt''join' => 'round' ,'dash' => '0''color' => array(100,100,100));
            
$style1 = array('width' => 0.04'cap' => 'butt''join' => 'round''dash' => '0''color' => array(2552550));
            
$ypos $imageheight 1.5;
            
$pdf->SetY($ypos);
            unset(
$notes);
            if (
$resources[$n]['annotation_count'] != 0) {
                
$notes getResourceAnnotations(
                    
$ref,
                    
$page $page # Supporting legacy logic (from annotate plugin). See upgrade script #28
                
);
                
$notepages 1// Normally notes will all fit on one page, but may not
                
foreach ($notes as $note) {
                    
// If the notes took us to a new page, return to the image page before marking annotation
                    
if ($notepages 1) {
                        
debug('Return to the image page');
                        
$pdf->setPage($currentpdfpage);
                    }

                    
$note['value'] = $note['text'] == ''
                        
implode("\n"array_map(prefix_value('- '), array_column(getAnnotationTags($note), 'name')))
                        : 
$note['text'];

                    
// Convert normalised (0-1) coordinates
                    
$note_x $note['x'] * $imagewidth;
                    
$note_y $note['y'] * $imageheight;
                    
$note_width $note['width'] * $imagewidth;
                    
$note_height $note['height'] * $imageheight;

                    
$pdf->SetLineStyle($style1);
                    
$pdf->Rect($image_x $note_x$image_y $note_y$note_width$note_height);
                    
$pdf->Rect($image_x $note_x$image_y $note_y.1.1'DF'$style1, array(255,255,0));
                    
$ypos $pdf->GetY();
                    
$pdf->Text($image_x $note_x .01$image_y $note_y .01$mfalsefalsetrue00'L');

                    
$pdf->SetY($ypos);
                    
$note_user get_user($note['user']);
                    
$pdf->SetLineStyle($style);

                    
// If the notes went over the page, we  went back to image for annotation, so we need to return to the page with the last row of the table before adding next row
                    
if ($notepages 1) {
                        
$pdf->setPage($currentpdfpage + ($notepages 1));
                    }

                    
$pdf->multiRow($mtrim($note['value']) . " @" $note_user['fullname']);
                    
// Check if this new table row has moved us to a new page, in which case we need to record this and go back to image page before the next annotation
                    
if (isset($notepos)) {
                        
debug('The new (notes) table row moved us to a new page');
                        
$lastnotepos $notepos;
                        
debug("lastnotepos = {$lastnotepos}");
                    }
                    
$notepos $pdf->GetY();
                    if (isset(
$lastnotepos) && $notepos $lastnotepos) {
                        unset(
$lastnotepos);
                        
$notepages++;
                        
debug('$notepages++');
                    }
                    
$ypos $ypos .5;
                    
$m++;
                }
            }
        }
        
// Check if there is another page?
        
if (file_exists(get_resource_path($reftrue"scr"false"jpg", -1$page 1$use_watermark""))) {
            unset(
$notepos);
            unset(
$lastnotepos);
            
$totalpages++;
            
debug('$totalpages++');
        }

        
$page++;
        
debug('$page++');
    } while (
$page <= $totalpages);

    
// reset pointer to the last page
    
$pdf->lastPage();

    
#Make AJAX preview?:
    
if ($preview && isset($imagemagick_path)) {
        if (
file_exists($jpgstoragepath)) {
            
unlink($jpgstoragepath);
        }
        if (
file_exists($pdfstoragepath)) {
            
unlink($pdfstoragepath);
        }
        echo 
$pdf->GetPage(); // for paging
        
$pdf->Output($pdfstoragepath'F');
        
# Set up
        
putenv("MAGICK_HOME=" $imagemagick_path);
        
$ghostscript_fullpath get_utility_path("ghostscript");
        
run_command(
            
"{$ghostscript_fullpath} -sDEVICE=jpeg -dFirstPage=previewpage -o -r100 -dLastPage=previewpage"
            
" -sOutputFile=jpgstoragepath pdfstoragepath",
            
false,
            [
                
'previewpage' => (int) $previewpage,
                
'jpgstoragepath' => new CommandPlaceholderArg($jpgstoragepath'is_safe_basename'),
                
'pdfstoragepath' => new CommandPlaceholderArg($pdfstoragepath'is_safe_basename'),
            ]
        );

        
$convert_fullpath get_utility_path("im-convert");
        if (!
$convert_fullpath) {
            exit(
"Could not find ImageMagick 'convert' utility at location '$command'");
        }

        
run_command(
            
"{$convert_fullpath} -resize contact_sheet_preview_size -quality 90 -colorspace imagemagick_colorspace"
            
" jpgstoragepath jpgstoragepath",
            
false,
            [
                
'contact_sheet_preview_size' => new CommandPlaceholderArg(
                    
$contact_sheet_preview_size,
                    
'is_valid_contact_sheet_preview_size'
                
),
                
'imagemagick_colorspace' => $imagemagick_colorspace,
                
'jpgstoragepath' => new CommandPlaceholderArg($jpgstoragepath'is_safe_basename'),

            ]
        );
        return 
true;
    }

    if (!
$is_collection) {
        
$filename $lang['annotate_annotations_label'] . "-" i18n_get_translated($resourcedata["field" $view_title_field]);
    } else {
        
$filename $lang['annotate_annotations_label'] . "-" i18n_get_collection_name($collectiondata);
    }

    if (
$cleanup) {
        
// cleanup
        
if (file_exists($pdfstoragepath)) {
            
unlink($pdfstoragepath);
        }
        if (
file_exists($jpgstoragepath)) {
            
unlink($jpgstoragepath);
        }
        
$pdf->Output($filename ".pdf"'D');
    } else {
        
// in this case it's not cleaned up automatically, but rather left in place for later use of the path.

        
$pdf->Output($pdfstoragepath'F');
        echo 
$pdfhttppath;
    }
}

This article was last updated 4th July 2025 17:35 Europe/London time based on the source file dated 4th July 2025 10:35 Europe/London time.