Pdf jpg конвертер php

How to Convert a PDF to JPEG using PHP

Hey, Today I would like to show you how we can convert PDF to JPEG using imagick extension. Imagick is a native php extension to create and modify images using the ImageMagick API, which is mostly built-in in PHP installation so no need to include any thing. ImageMagick software suite allow us to create, read, edit, and compose bitmap images easily.

PHP – Convert all PDF pages to JPEG

Using following simple example you can convert all pages of PDF to JPEG images.

readImage('myfile.pdf'); // Writes an image or image sequence Example- converted-0.jpg, converted-1.jpg $imagick->writeImages('converted.jpg', false); ?>

As you are seeing, you have to pass a PDF file and it will produce JPEG files for each page of your given PDF file as output. writeImages() function second parameter is false, so it will not join the images, means it will produce image sequence(create images for each page) Example – converted-0.jpg, converted-1.jpg.

Читайте также:  Delete file in cpp

How to Convert a PDF to JPEG using PHP

PHP – Convert specific PDF page to JPEG

If you want to convert specific page for example first page of your PDF file only then define PDF file name like this myfile.pdf[0] and run the script it will show convert only first page of your PDF file. Following is the example –

readImage('test.pdf[0]'); // Writes an image $imagick->writeImages('converted_page_one.jpg'); ?>

PHP – Convert specific PDF page to JPEG with quality

If you need better quality, try adding $imagick->setResolution(150, 150); before reading the file. setResolution() must be called before loading or creating an image.

setResolution(150, 150); // Reads image from PDF $imagick->readImage('test.pdf[0]'); // Writes an image $imagick->writeImages('converted_page_one.jpg'); ?>

If you experience transparency problems when converting PDF to JPEG (black background), try flattening your file:

setResolution(150, 150); // Reads image from PDF $imagick->readImage('myFile.pdf[0]'); // Merges a sequence of images $imagick = $imagick->flattenImages(); // Writes an image $imagick->writeImages('converted_page_one.jpg'); ?>

shared hosting – Convert a PDF to JPEG using PHP

Most of the shared hosting providers do not compile imagick extension with PHP, but imagick binaries will be available, so here is the code to convert PDF to JPEG with imagick binaries.

// source PDF file $source = "myFile.pdf"; // output file $target = "converted.jpg"; // create a command string exec('/usr/local/bin/convert "'.$source .'" -colorspace RGB -resize 800 "'.$target.'"', $output, $response); echo $response ? "PDF converted to JPEG!!" : 'PDF to JPEG Conversion failed'; ?>

You have to change binaries location (/usr/local/bin/convert) to your server location which you can get from your hosting admin.

Lots of things can be done with Imagick extension, explore more about it at – http://php.net/manual/en/book.imagick.php

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face — we are here to solve your problems.

Источник

Split, reduce and convert PDF to JPEG using PHP ImageMagick

During these days I’m working on a PHP project based on manipulation of PDF files uploaded by users. Documents must be truncated, splitted to images, quality reducted, etc.

Because of the project’s nature, libraries’ inclusion is problematic, so I was forced to study a solution for the issue that involves only PHP official extensions. The result is some code based on ImageMagick extension.

In this post I’ll note down what I found because I hope that it can be useful for someone in my own situation.

Example 1: convert to low quality PDF

Let’s start with very simple example. Following lines will produce a new PDF file containing a low quality image for each page from original PDF:

php $image = new imagick(); $image->readImage('original.pdf'); $image->writeImages('poor_quality.pdf', true);

Example 2: convert to low quality JPEGs

This code will produce a list of low quality JPGs, one for each page from original PDF:

php $image = new imagick(); $image->readImage('original.pdf'); $image->writeImages('poor_quality.jpg', false);

Only difference from previous example is in line 4:

  • file extension is .jpg
  • last parameter is false , otherwise imagemagick will output a single jpg with all pages overlapped

Output of this code will be a list of files like:

$ ls poor_quality-0.jpg poor_quality-1.jpg poor_quality-2.jpg

Example 3: convert to high quality JPEGs

This code will produce a list of JPGs, like previous example, but with high quality images:

php $image = new imagick(); $image->setResolution(300, 300); $image->readImage('original.pdf'); $image->setImageCompressionQuality(100); $image->writeImages('high_quality.jpg', false);

Most important line, compared to previous example, is line 3 in which we set resolution parameters. It’s important to execute this instruction before read PDF file.

Example 4: convert subset of pages to JPEGs (only for small PDFs)

Following code can be used to convert a subset of page to JPGs. Unfortunately it is very inefficient and can be very slow for PDF with a lot of pages. For this reason, I point it out for educational reasons only:

 1 2 3 4 5 6 7 8 9 10 11 12 13 
php $image = new imagick(); $image->setResolution(300,300); $image->readImage('original.pdf'); $image->setImageCompressionQuality(100); $image->setBackgroundColor( new ImagickPixel( 'white' ) ); // Remove pages > 10 while ( $image->setLastIterator() && $image->getIteratorIndex() > 10 )  $image->removeImage(); > $image->writeImages( 'high_quality.jpg',false);

There also are methods like setIteratorIndex or setFirstIterator that can be useful for building various types of cycles but, as reported before, in my experience this method can be very slow. More details can be found at ImageMagick.

Example 5: convert subset of pages to JPEGs

Last example is a bit more complex because it reads and writes pages one by one. Anyway it’s also much more efficient than the previous one. It produces a sublist of high quality JPEGs also from very long PDFs in an acceptable time:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 
php $image = new imagick(); $image->setResolution(300,300); $first_page = 0; $last_page = 10; try  for ( $i = $first_page; $i  $last_page; $i ++ )  $image->readImage('original.pdf' . '[' . $i . ']' ); $image->setImageCompressionQuality( 100 ); $image->setBackgroundColor( new ImagickPixel( 'white' ) ); $image->writeImage( 'high_quality-' . $i . '.jpg' ); > > catch (ImagickException $e) if ( $e->getCode() == 1 )  // It's OK. This exception means that there are no more pages to convert. > >

Using [index] after PDF path, Imagemagick read and converts only the page corresponding to the index. In this way, only needed pages will be processed.

More details

I know well that this is not a complete guide, but I hope that it can be useful for someone in my same situation. Anyway, you can merge previous example to obtain different results and find much more details at ImageMagick.

Final thoughts

PHP ImageMagick extension is a very powerful tool but, imho, it’s not very well documented (like most PHP features). Probably, a more complete guide could allow to develop a lot of code without need to include external libraries that adds complexity, maintenance issues, etc.

See also

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Convert PDF to JPG, PNG or GIF in PHP.

License

baraja-core/php-pdf-to-image

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Convert PDF to image and save to disk. Convert PDF to JPG, PNG or GIF in PHP.

It’s best to use Composer for installation, and you can also find the package on Packagist and GitHub.

To install, simply use the command:

$ composer require baraja-core/php-pdf-to-image

You can use the package manually by creating an instance of the internal classes, or register a DIC extension to link the services directly to the Nette Framework.

$configuration = new Configuration( pdfPath: __DIR__ . '/example.pdf', savePath: __DIR__ . '/example.jpg', format: 'jpg' ); // Render PDF to image and save to disk. \Baraja\PdfToImage\Convertor::convert($configuration);

Supported configuration options

Name Type Default value
pdfPath string
savePath string
format string ‘jpg’
trim bool false
cols int , null null
rows int , null null
bestfit bool false

baraja-core/php-pdf-to-image is licensed under the MIT license. See the LICENSE file for more details.

About

Convert PDF to JPG, PNG or GIF in PHP.

Источник

Оцените статью