pdf-to-image provides a convenient interface for converting PDF to JPEG/PNG using Ghostscript.
- PHP version 7.4
Before using pdf-to-image, ensure that you have Ghostscript on your system:
sudo apt-get install ghostscript
Options is an associative array with option
as key and default
as value.
Option | Default | Description |
---|---|---|
resolution |
300 |
Resolution (dots per inch) of the generated images. |
compression_quality |
100 |
Compression quality for JPEG images (applies only to JPEG output). |
alpha_bits |
4 |
Number of bits (between 1 to 4 ) for alpha channel transparency in PNG images. |
disable_color_management |
true |
Disable color management during PDF conversion. |
disable_font |
true |
Disable font inclusion in the generated images. |
disable_annotations |
true |
Disable annotations in the PDF during conversion. |
These functions are ordered in use-case. From reading the file, to configure the conversion, to processing the PDF to JPG or PNG.
Set the path to the Ghostscript executable:
$pdf_to_image->set_path(path);
- path: relative or real path to the Ghostscript executable.
Set a PDF to be read and converted. Referenced as path
:
$pdf_to_image->read(path);
- path: relative or real path to the PDF file.
Configure the conversion process for the PDF:
$pti->set_configuration(options);
- options: see options.
Convert to type
which can either be JPG or PNG the given directory:
$pdf_to_image->process(directory, type);
- directory: the directory in which the resulting conversions should be outputted.
- type: specifies the file format for the conversion, either JPG or PNG.
Get the Ghostscript command for processing the PDF file:
$pdf_to_image->get_command(options);
- options:
options
passed throughget_command
overwrites current and default configurations and parameters. See options.
Retrieve the amount of pages in a PDF:
$pdf_to_image->get_page_amount(path);
- path: relative or real path to the PDF file.
- The example assumes that the Ghostscript executables are in the system's PATH variables.
- Make sure the web server has the necessary permissions to read the input PDF file and write to the output directory.
- Ensure that the
proc_open
function is not disabled in your PHP configuration. Check thedisable_functions
directive in yourphp.ini
file and removeproc_open
if present. - Ensure that the
-dNumRenderingThreads
parameter within thestandard_command_parameters
property of the PdfToImage class is set to the desired number of CPU cores.