PdfUtils

PDF documents manipulation methods

package

Default

Methods

Performs maximum possible optimization to a specified pdf document, by appliyng the pdftk command line tool. We should place this tool on our project storage/binary folder pdftk is free and can be downloaded from: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

compressDocument(string $pdftkPath, string $pdfPath, string $outputPath = '') : boolean
static

VERY IMPORTANT:

  1. pdftk execute permission MUST be enabled at least for the file owner
  2. Make sure you are using the binary executable of pdftk that fits your OS (centos, windows...) and processor (32bit / 64bit) or it may not work

Arguments

$pdftkPath

string

Full executable path to the pdftk tool. For example: $fileStorageManager->binaryGetAppPath('pdftk')

$pdfPath

string

Full path to the pdf source file. Example: ProjectPaths::RESOURCES.'/pdf/mypdf.pdf'

$outputPath

string

Leave it empty (default value) to override the source pdf document or specify a full system path (including the destination filename) where the compressed result will be stored.

Response

boolean

True if compression was performed or false if something failed

Extract all the possible text from the given pdf document

extractDocumentText(string $pdfPath) : string
static

Arguments

$pdfPath

string

Full path to the pdf source file. Example: ProjectPaths::RESOURCES.'/pdf/mypdf.pdf'

Response

string

All the text that could be extracted from the pdf

Given a PDF document, this method will generate a picture for each one of the document pages.

generateDocumentJpgPictures(string $ghostScriptPath, string $pdfPath, string $outputPath, \org\turbocommons\src\main\php\utils\number $jpgQuality = 90, string $dpi = '200', string $outFileMask = '/%d.jpg') : \org\turbocommons\src\main\php\utils\number
static

Requires GhostScript, that is an open source library to manipulate PS and PDF files, that normally comes bundled with linux distributions. If not available, we must install it on our machine, or better download (http://www.ghostscript.com/download/gsdnld.html) the pre compiled binaries and place it on storage/binary

Arguments

$ghostScriptPath

string

Full executable path to the ghostscript tool. For example: 'gs' if we are using the version installed on our machine or $fileStorageManager->binaryGetAppPath('gs') if we have placed it on our storage binary folder. It is recommended to user always the latest version, so we better download it and place it on storage/binary

$pdfPath

string

Full path to the pdf source file. Example: ProjectPaths::RESOURCES.'/pdf/mypdf.pdf'

$outputPath

string

Full path to a file system EMPTY folder where all the generated pictures will be stored. If the specified folder is not empty or does not exist, an exception will happen.

$jpgQuality

\org\turbocommons\src\main\php\utils\number

90 by default. Specifies the jpg quality for all the generated pictures

$dpi

string

200 by default, defines the pixel density for all the generated pictures. This will in fact affect the final resolution of the images.

$outFileMask

string

'/%d.jpg' by default. Allows us to define a pattern for the generated file names (%d will be replaced by the page number). Example: '%05d.jpg' will generate a jpg file with 5 digits, like '00012.jpg'. More info on the GostScript manual for the option -o

Response

\org\turbocommons\src\main\php\utils\number

The total number of generated pages or -1 if an error happened

Given a PDF document, this method will generate a picture for the specified page.

generatePageJpgPicture(string $ghostScriptPath, string $pdfPath, string $page, \org\turbocommons\src\main\php\utils\number $jpgQuality = 90, string $dpi = '200') : string
static

Requires GhostScript, that is an open source library to manipulate PS and PDF files, that normally comes bundled with linux distributions. If not available, we must install it on our machine, or better download (http://www.ghostscript.com/download/gsdnld.html) the pre compiled binaries and place it on storage/binary

Arguments

$ghostScriptPath

string

Full executable path to the ghostscript tool. For example: 'gs' if we are using the version installed on our machine or $fileStorageManager->binaryGetAppPath('gs') if we have placed it on our storage binary folder. It is recommended to user always the latest version, so we better download it and place it on storage/binary

$pdfPath

string

Full path to the pdf source file. Example: ProjectPaths::RESOURCES.'/pdf/mypdf.pdf'

$page

string

The number of the page we want to convert to a picture. FIRST PAGE STARTS AT 0

$jpgQuality

\org\turbocommons\src\main\php\utils\number

90 by default. Specifies the jpg quality for the generated picture

$dpi

string

200 by default, defines the pixel density for the generated picture. This will in fact affect the final resolution of the image.

Response

string

A binary string containing the generated picture, or null if some problem happened

Count the number of pages on a PDF document.

getPagesCount(string $pdfInfoPath, string $pdfPath) : integer
static

This method requires an external command line tool called pdfinfo, that we should place on our project storage/binary folder Its free and can be downloaded from: http://www.foolabs.com/xpdf/download.html

VERY IMPORTANT:

  1. pdfinfo execute permission MUST be enabled at least for the file owner
  2. Make sure you are using the binary executable of pdfinfo that fits your OS (centos, windows...) and processor (32bit / 64bit) or it may not work

Arguments

$pdfInfoPath

string

Full executable path to the pdfInfo tool. For example: $fileStorageManager->binaryGetAppPath('pdfinfo')

$pdfPath

string

Full path to the pdf source file. Example: ProjectPaths::RESOURCES.'/pdf/mypdf.pdf'

Response

integer

The total number of calculated pages