Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

Library code for manipulating PDFs

Copyright: 2012 Davo Smith
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 810 lines (30 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: pdf  - X-Ref

Library code for manipulating PDFs

get_export_font_name()   X-Ref
Get the name of the font to use in generated PDF files.
If $CFG->pdfexportfont is set - use it, otherwise use "freesans" as this
open licensed font has wide support for different language charsets.

return: string

combine_pdfs($pdflist, $outfilename)   X-Ref
Combine the given PDF files into a single PDF. Optionally add a coversheet and coversheet fields.

param: string[] $pdflist  the filenames of the files to combine
param: string $outfilename the filename to write to
return: int the number of pages in the combined PDF

current_page()   X-Ref
The number of the current page in the PDF being processed

return: int

page_count()   X-Ref
The total number of pages in the PDF being processed

return: int

load_pdf($filename)   X-Ref
Load the specified PDF and set the initial output configuration
Used when processing comments and outputting a new PDF

param: string $filename the path to the PDF to load
return: int the number of pages in the PDF

set_pdf($filename, $pagecount = 0)   X-Ref
Sets the name of the PDF to process, but only loads the file if the
pagecount is zero (in order to count the number of pages)
Used when generating page images (but not a new PDF)

param: string $filename the path to the PDF to process
param: int $pagecount optional the number of pages in the PDF, if known
return: int the number of pages in the PDF

copy_page()   X-Ref
Copy the next page from the source file and set it as the current page

return: bool true if successful

create_page_from_source($pageno)   X-Ref
Create a page from a source PDF.

param: int $pageno

copy_remaining_pages()   X-Ref
Copy all the remaining pages in the file


append_comments($allcomments)   X-Ref
Append all comments to the end of the document.

param: array $allcomments All comments, indexed by page number (starting at 0).
return: array|bool An array of links to comments, or false.

add_comment_marker($pageno, $index, $x, $y, $link, $colour = 'yellow')   X-Ref
Add a comment marker to the specified page.

param: int $pageno The page number to add markers to (starting at 0).
param: int $index The comment index.
param: int $x The x-coordinate of the marker (in pixels).
param: int $y The y-coordinate of the marker (in pixels).
param: int $link The link identifier pointing to the full comment text.
param: string $colour The fill colour of the marker (red, yellow, green, blue, white, clear).
return: bool Success status.

add_comment($text, $x, $y, $width, $colour = 'yellow')   X-Ref
Add a comment to the current page

param: string $text the text of the comment
param: int $x the x-coordinate of the comment (in pixels)
param: int $y the y-coordinate of the comment (in pixels)
param: int $width the width of the comment (in pixels)
param: string $colour optional the background colour of the comment (red, yellow, green, blue, white, clear)
return: bool true if successful (always)

add_annotation($sx, $sy, $ex, $ey, $colour = 'yellow', $type = 'line', $path, $imagefolder)   X-Ref
Add an annotation to the current page

param: int $sx starting x-coordinate (in pixels)
param: int $sy starting y-coordinate (in pixels)
param: int $ex ending x-coordinate (in pixels)
param: int $ey ending y-coordinate (in pixels)
param: string $colour optional the colour of the annotation (red, yellow, green, blue, white, black)
param: string $type optional the type of annotation (line, oval, rectangle, highlight, pen, stamp)
param: int[]|string $path optional for 'pen' annotations this is an array of x and y coordinates for
param: string $imagefolder - Folder containing stamp images.
return: bool true if successful (always)

save_pdf($filename)   X-Ref
Save the completed PDF to the given file

param: string $filename the filename for the PDF (including the full path)

set_image_folder($folder)   X-Ref
Set the path to the folder in which to generate page image files

param: string $folder

get_image($pageno)   X-Ref
Generate an image of the specified page in the PDF

param: int $pageno the page to generate the image of
return: string the filename of the generated image

ensure_pdf_compatible(\stored_file $file)   X-Ref
Check to see if PDF is version 1.4 (or below); if not: use ghostscript to convert it

param: stored_file $file
return: string path to copy or converted pdf (false == fail)

ensure_pdf_file_compatible($tempsrc)   X-Ref
Check to see if PDF is version 1.4 (or below); if not: use ghostscript to convert it

param: string $tempsrc The path to the file on disk.
return: string path to copy or converted pdf (false == fail)

get_error_image($errorimagefolder, $pageno)   X-Ref
Generate an localised error image for the given pagenumber.

param: string $errorimagefolder path of the folder where error image needs to be created.
param: int $pageno page number for which error image needs to be created.
return: string File name

test_gs_path($generateimage = true)   X-Ref
Test that the configured path to ghostscript is correct and working.

param: bool $generateimage - If true - a test image will be generated to verify the install.
return: \stdClass

send_test_image()   X-Ref
If the test image has been generated correctly - send it direct to the browser.


add_image_page($imagestoredfile)   X-Ref
This function add an image file to PDF page.

param: \stored_file $imagestoredfile Image file to be added