Skip to content

A PHP class for PDF to JPEG/PNG conversion using Ghostscript.

Notifications You must be signed in to change notification settings

echtejosh/pdf-to-image

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 

Repository files navigation

@pdf-to-image

pdf-to-image provides a convenient interface for converting PDF to JPEG/PNG using Ghostscript.

Requirements

  • PHP version 7.4

Installation

Before using pdf-to-image, ensure that you have Ghostscript on your system:

sudo apt-get install ghostscript

Options

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.

Functions

These functions are ordered in use-case. From reading the file, to configure the conversion, to processing the PDF to JPG or PNG.

set_path(path)

Set the path to the Ghostscript executable:

$pdf_to_image->set_path(path);
  • path: relative or real path to the Ghostscript executable.

read(path)

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.

set_configuration(options)

Configure the conversion process for the PDF:

$pti->set_configuration(options);

process(directory, type)

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.

Additional Functions

get_command(options)

Get the Ghostscript command for processing the PDF file:

$pdf_to_image->get_command(options);
  • options: options passed through get_command overwrites current and default configurations and parameters. See options.

get_page_amount(path)

Retrieve the amount of pages in a PDF:

$pdf_to_image->get_page_amount(path);
  • path: relative or real path to the PDF file.

Additional Notes

  • 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 the disable_functions directive in your php.ini file and remove proc_open if present.
  • Ensure that the -dNumRenderingThreads parameter within the standard_command_parameters property of the PdfToImage class is set to the desired number of CPU cores.

About

A PHP class for PDF to JPEG/PNG conversion using Ghostscript.

Topics

Resources

Stars

Watchers

Forks

Languages