Good morning everyone! I would like to know if there is any php function that renders a part of the pdf file to an image file. There is the ImageMagick function, but I'm not quite sure.
Good morning everyone! I would like to know if there is any php function that renders a part of the pdf file to an image file. There is the ImageMagick function, but I'm not quite sure.
To solve your problem, I've used the SPATIE package available on github .
Only install the package via composer as per package documentation.
Follow the code below:
<?php
use Spatie\PdfToImage\Pdf as Render;
require __DIR__ . '/vendor/autoload.php';
$pdf_path = __DIR__ . '/pdf/file.pdf';
$images_path = __DIR__ . '/pdf/images/';
if (!file_exists($images_path)) {
echo printf('Path %s não encontrado', $images_path);
die;
}
if (!file_exists($pdf_path)) {
echo printf('Path %s não encontrado', $pdf_path);
die;
}
$pdf_to_image = new Render($pdf_path);
$image = $pdf_to_image->setPage(3)
->setOutputFormat('png')
->saveImage($images_path . date('now'));
if (!$image) {
echo 'Erro ao renderizar PDF';
die;
}
echo 'Sucesso ao renderizar PDF';
Note:
date('now')
to generate the file name Attention to the folder structure
Projeto
-> pdf
->images
-> vendor
The files composer.json
and index.php
are in the project root