How to convert PDF document to preview image in PHP?

| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

In this post, however, we will limit ourselves to a much smaller example, which is to create a preview of an image from a given PDF document. "Why a preview?‚", You ask. Well, it might be needed for his library management system, her online ebook store, or just some crazy weekend programming task. Where do you think you can use this concept in your project? Let me know in the comments.It is no longer possible to implement a complete conversion algorithm from scratch, so we will use third-party libraries to make our task easier. The techniques I found attractive in this scenario are based on the following tools:
  • Ghostscript:is a command line utility available for all three major platforms, viz. Windows, Linux and Mac that interpret PostSript and PDF files. You can read more about this on his official site .
  • ImageMagick:This is a free and open source software package for displaying, converting and editing raster images and vector image files. It is available for most major programming languages ‚Äã‚Äãincluding PHP. Here is the standard documentation for quick overview .
Using Ghostscript To use Ghostscript in your project, start by installing it. If you are on Windows, download the executable from the download page.Linux users can install Ghostscript directly through the default package managers;
# RPM based distros, Fedora 26/27/28 $sudo dnf install ghostscript 
Verify the installation with this command,
$gs --version 
After installation, navigate to the directory containing the PDF file and run the following command.
$gs -dSAFER -dBATCH -sDEVICE = jpeg -dTextAlphaBits = 4 -dGraphicsAlphaBits = 4 -dFirstPage = 1 -dLastPage = 1 -r300 -sOutputFile = preview.jpg input.pdf  This will create an image of the first page from the document. Let’s understand what it actually does;
  • -sDEVICE:sets the output image file format.
  • -sTEXTVAL, - sGRAPHICVAL:sets anti-aliasing for the resulting image. Valid values ‚Äã‚Äãare 1, 2, and 4.
  • -r {NUM}:sets the resolution (in dpi) of the image.
  • -sFirstPage, -sLastPage:sets the first and last page of the document to be displayed.
  • -sOutputFile:sets the name of the output file.
  • input.pdf:this is the actual PDF document that is used to convert.
Now, to use this command in PHP, we call the exec() function. For example: exec ( "ls -l" , $output_str , $return_val ); foreach ( $output_str as $line ) { echo $line . "" ; }
?>;
This example on Linux will execute ls and will print all directories and files to the console.We can use this concept and execute a command from our PHP code. This is how I did it; < tbody> function is_pdf ( $file ) { $file_content = file_get_contents ( $file ); if (preg_match ( " / ^% PDF- [0-1]. [0-9] + / " , $file_content )) { return true; } else { return false; } } function create_preview ( $file ) { $output_format = "jpeg" ; $antialiasing = "4" ; $preview_page = "1" ; $resolution = "300" ; $output_file = "preview.jpg" ; $exec_command = " gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE = " . $output_format . "" ; $exec_command . = "- dTextAlphaBits =" . $antialiasing . "-dGraphicsAlphaBits =" . $antialiasing . "" ; $exec_command . = "- dFirstPage =" . $preview_page . "-dLastPage =" . $preview_page . "" ; $exec_command . = "- r" . $resolution . "" ; $exec_command . = "- sOutputFile =" . $output_file . "’ " . $file . "’ " ; echo " Executing command ... " ; exec ( $exec_command , $command_output , $return_val ); foreach ( $command_output as $line ) { echo $line . "" ; } if (! $return_val ) { echo "Preview created successfully !!" ; } else { echo "Error while creating the preview." ; } } function __ main __() { global $argv ; $input_file = $argv [1]; if (is_pdf ( $input_file )) { // Create PDF preview create_preview ( $input_file ); } else { echo "The input file" . $input_file . "is not a valid PDF document." ; } }
__ main __();
?>
Execution starts with __main __() which accepts a PDF file on the command line. It checks if the input file is valid PDF or not. If valid, it executes the command on the input file.
Exit :
$php pdf_preview.php input.pdf Excuting command ... GPL Ghostscript 9.22 (2017-10-04) Copyright (C) 2017 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 successfully !! 
Using ImageMagick As usual, we’ll start by installing the ImageMagick binaries on the system. Let’s start with the dependencies;
$sudo dnf install gcc php-devel php-pear 
Then install ImageMagick;
$sudo dnf install ImageMagick ImageMagick-devel 
Then install the PHP wrapper classes;
$sudo pecl install imagick $sudo bash -c "echo" extension = imagick.so "> / etc /php.d/imagick.ini "
If you plan to use it in LAMP architecture, consider restarting the Apache web server;
$sudo service httpd restart 

Now that our system is ready, we can use ImageMagick in our sample project. The basic functionality of the script remains the same. All you have to do is replace the content of the create_preview() function with the following code. function create_preview ( $file ) { $output_format = "jpeg" ; $preview_page = "1" ; $resolution = "300" ; $output_file = "imagick_preview.jpg" ; echo " Fetching preview ... " ; $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); } The code is self-explanatory. We define an instance of type Imagick and set various parameters like resolution, file format, etc. The PDF page you want to display is referred to as an array index after the file name. For example:

First page: input.pdf [0] Second page: input.pdf [1]. ... ... Nth page: input.pdf [N - 1] 
Output :
$php pdf_preview.php input.pdf Fetching preview ...  Some of you may be wondering why use this method over the previous one. Well, I found ImageMagick to match PHP code quite well. The command line in programming doesn’t look very good and sometimes becomes common knowledge. However, with the same set of configurations, Ghostscript produced smaller image files than the files produced by ImageMagick. I’m not sure if this is due to some optimization issues, but that is not the difference. Choosing one over the other is simply based on your own taste.So this is how you create a preview for a given PDF document. I hope you learned something new from this post. Which method would you prefer? Any suggestions for further improvements? Feel free to mention them in the comments.
    

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from How to convert PDF document to preview image in PHP?, check other __main__ Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Xu Ungerschaft

Texas | 2023-03-26

Maybe there are another answers? What How to convert PDF document to preview image in PHP? exactly means?. I am just not quite sure it is the best method

Olivia Gonzalez

Vigrinia | 2023-03-26

Simply put and clear. Thank you for sharing. How to convert PDF document to preview image in PHP? and other issues with dis Python module was always my weak point 😁. Will use it in my bachelor thesis

Oliver Schteiner

Texas | 2023-03-26

Thanks for explaining! I was stuck with How to convert PDF document to preview image in PHP? for some hours, finally got it done 🤗. Will get back tomorrow with feedback

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically