How to convert a PDF document to a preview image in PHP?

Spread the love

Sometimes, we want to convert a PDF document to a preview image in PHP.

In this article, we’ll look at how to convert a PDF document to a preview image in PHP.

How to convert a PDF document to a preview image in PHP?

To convert a PDF document to a preview image in PHP, we can use ImageMagick.

For instance, we write

$im = new imagick("file.pdf[0]");
$im->setImageFormat("jpg");
header("Content-Type: image/jpeg");
echo $im;

to create the $im object with the imagick class called with the path to the image.

Then we call setImageFormat to set the image format to jpg.

Then we call header to return the Content-Type header with the MIME type of the image.

Conclusion

To convert a PDF document to a preview image in PHP, we can use ImageMagick.

Leave a Reply

Your email address will not be published.