hyder/converter

A Laravel package that provides an easy-to-use interface to convert PDF files to images using the Imagick PHP extension.

1.0.1 2023-04-09 18:07 UTC

This package is auto-updated.

Last update: 2024-04-30 00:51:32 UTC


README

A Laravel package that allows you to convert PDF files to images. The package uses Imagick PHP extension and GhostScript to convert PDF files to images.

Requirements

  • PHP "^7.3|^8.0"

  • Imagick PHP extension
    Enable the Imagick extension in your php.ini file by uncommenting or adding the following line:

extension=imagick
  • Ghostscript
    Verify that Ghostscript is installed by running the following command:
gs -v

This should display the version number of Ghostscript.

Installation

You can install the package via composer:

composer require hyder/converter

Configuration

You can publish the configuration file to customize the package settings. To publish the configuration file, run the following command:

php artisan vendor:publish --provider="Hyder\Converter\ConverterServiceProvider" --tag="config"

This will create a config/converter.php file where you can configure the package settings.

Optional

The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:

'providers' => [
    // ...
    Hyder\Converter\ConverterServiceProvider::class,
];

Usage

Here's an example of how to convert a PDF file to an image:

use Hyder\Converter\Facades\PdfToImage;

// Minimalistic
$response = PdfToImage::path('/path/to/file.pdf')
    ->save();

// Customize output
$response = PdfToImage::path('/path/to/file.pdf')
    ->format('png')
    ->resolution(200)
    ->maxLimit(5)
    ->setPage('1-3,5')
    ->toDir('/path/to/directory')
    ->save('output-image-name');

Available Methods

Here are the available methods of the package:

path(string $path)

This method sets the path of the PDF file you want to convert to an image.

format(string $format)

This method sets the output format of the image. The supported formats are JPEG, JPG, and PNG.

resolution(int $dpi)

This method sets the resolution of the output image in dots per inch (dpi). The higher the resolution, the better the quality of the image, but also the larger the file size.

maxLimit(int $max)

This method sets a maximum limit for the number of pages to be converted. If the PDF file has more pages than this limit, only the first $max pages will be converted.

setPage($pages)

This method sets the pages of the PDF file that you want to convert. The pages can be specified as single page numbers (e.g. 1,2,3), a range of pages (e.g. 1-3), or a combination of both (e.g. 1,2,3-5). By default only the first page will be converted.

allPage()

This method is used to set all pages of the PDF file for conversion to images. It doesn't make any sense to use maxLimit() or setPage() when using allPage(). If you call these methods before allPage(), they will be ignored.

toDir(string $storageTo)

This method sets the directory where the converted images will be saved. If no directory is specified, the default directory will be used.

save(string $name = "")

This method converts the PDF file to an image and saves it to the specified directory. If no name is specified, the image will be saved with a random name.

Error Handling

After enabling imagick you may get an error in the live server. ImageMagick has some security policies disabling some rights for security reasons. You will have to edit a config file to re-enable the action you need.

Open /etc/ImageMagick-6/policy.xml with your favorite text editor, find the line and replace "none" by "read|write"

Open the file

sudo nano /etc/ImageMagick-6/policy.xml

Find and edit the line

<policy domain="coder" rights="none" pattern="PDF" />

To

<policy domain="coder" rights="read|write" pattern="PDF" />

You can check Here for better understanding.

Credits

Imagick

Contributing

If you would like to contribute to this package, please create a pull request or open an issue.

License

The PDF to Image Converter package is open-source software licensed under the MIT license.