Render a part of a PDF document to an image file

1

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.

    
asked by anonymous 19.07.2017 / 15:59

1 answer

0

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:

  • I used 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

19.07.2017 / 17:48