How to Convert a PDF Document to a Preview Image in PHP
- Convert PDF Document to Preview Image with PHP and Ghostscript
- Convert PDF Document to Preview Image with PHP and ImageMagick
This tutorial will discuss two methods you can use to convert a PDF document to a set of preview images in PHP.
Preview adds a layer of security to your content because the content on images can not be copied and pasted. They also offer other functionalities which we will not dwell on.
The easiest way to convert PDF documents to preview images is by utilizing third-party libraries. These are:
- Ghostscript
- ImageMagick
Convert PDF Document to Preview Image with PHP and Ghostscript
This command-line utility is available on Windows, Linux, and Mac. Follow these steps to convert PDF documents to preview images.
-
To start the installation, go to Ghostscript’s official website, download the executable file, and follow the setup instructions.
-
Run the code below to verify the installation.
$gs --version
-
In your PDF file’s directory, run the command below.
$gs -dSAFER -dBATCH -sDEVICE=jpeg \ -dTextAlphaBits=4 -dGraphicsAlphaBits=4 \ -dFirstPage=1 -dLastPage=1 -r300 \ -sOutputFile=preview.jpg input.pdf
-
The command above will create an image of the starting page on your document. We will call the
exec()
function to use the command in PHP, as shown below.<?php exec( "ls -l", $output_str, $return_val ); foreach ( $output_str as $line ) { echo $line . "\n"; } ?>
The code above will load all directories and files to the console. We can now use PHP code to execute the Ghostscript command.
-
Here is the PHP script we used.
<?php
function my_pdf ( $file ) {
$file_info = file_get_contents( $file );
if ( preg_match( "/^%PDF-[0-1]\.[0-9]+/", $file_info ) ) {
return true;
}
else {
return false;
}
}
function our_preview ( $file ) {
$our_format = "png";
$prvw_page = "1";
$resolution = "300";
$our_file = "prvw.jpg";
$command = "gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=" . $our_format . " ";
$command .= "-dTextAlphaBits=" . " -dGraphicsAlphaBits=" . . " ";
$command .= "-dFirstPage=" . $prvw_page . " -dLastPage=" . $prvw_page . " ";
$command .= "-r" . $resolution . " ";
$command .= "-sOutputFile=" . $our_file . " '" . $file . "'";
echo "Running command...\n";
exec( $command, $com_output, $ret_val );
foreach( $com_output as $line ) {
echo $line . "\n";
}
if ( !$ret_val ) {
echo "Preview created !!\n";
}
else {
echo "Error while creating.\n";
}
}
function __main__() {
global $arg;
$inp_file = $arg[1];
if ( my_pdf( $inp_file ) ) {
// Preview for the pdf
create_preview( $inp_file );
}
else {
echo "The file " . $inp_file . " is not a valid PDF document.\n";
}
}
__main__();
?>
Code execution begins at the _main_()
function, where it fetches the PDF file on the command line and verifies its validity. If the file is valid, PHP will execute the Ghostscript command.
Output:
$ php pdf_preview.php input.pdf
Executing command...
GPL Ghostscript 9.22 (2022-08-05)
Copyright (C) 2022 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
Preview created !!
Convert PDF Document to Preview Image with PHP and ImageMagick
-
Start by installing all ImageMagick binaries into your system. Run the command below to install ImageMagick dependencies.
$sudo dnf install gcc php-devel php-pear
-
Run the command below to install ImageMagick.
$ sudo dnf install ImageMagick ImageMagick-devel
-
Then, let’s install the PHP wrapper classes.
$ sudo pecl install imagick $ sudo bash -c "echo "extension=imagick.so" > /etc/php.d/imagick.ini"
-
You must restart your Apache webserver for those using it on a LAMP server.
$ sudo service httpd restart
-
At this point, everything we need is ready. We can now use the earlier PHP script and edit the
create_preview()
function.Use the code below:
function create_preview ( $file ) { $output_format = "jpeg"; $preview_page = "1"; $resolution = "300"; $output_file = "imagick_preview.jpg"; echo "Fetching preview...\n"; $img_data = new Imagick(); $img_data->setResolution( $resolution, $resolution ); $img_data->readImage( $file . "[" . ($preview_page - 1) . "]" ); $img_data->setImageFormat( $output_format ); file_put_contents( $output_file, $img_data, FILE_USE_INCLUDE_PATH ); }
Output:
$ php pdf_preview.php input.pdf Fetching preview...
That is how we create preview images from PDF documents on PHP. Both methods have a similar base functionality. Your choice depends on your preferences.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn