spatie / pdf-to-image

Convert a pdf to an image
https://murze.be/2015/07/convert-a-pdf-to-an-image-using-php/
MIT License
1.3k stars 228 forks source link

Cannot save Image #146

Closed jaypee888 closed 3 years ago

jaypee888 commented 4 years ago

Hi,

I cannot save a single image from my loop. I'm using xampp. Please help.

This is my code. $full_dir = 'uploads/'; $format = 'png'; $folder = base_path('/public/'.$full_dir.'pages/');
$quality = 100; for($page=1; $page<=$pdf->getNumberOfPages(); $page++) { $page_save_filename = $folder.$page.'.'.$format;
$pdf->setPage($page); $pdf->setOutputFormat($format); //$pdf->setCompressionQuality($quality); $pdf->saveImage($page_save_filename);
}

looks like $pdf->saveImage($page_save_filename); is executed. but there's no error.

vhcraig commented 4 years ago

I know I'm a little late to the party, but hope this helps:

Base on the the function base_path I assume you are using Laravel. By adding a slash in front of public, it creates a double slash in the path, example: /var/www/example-site//public/uploads/pages/

So change this: $folder = base_path('/public/'.$full_dir.'pages/');

To this: $folder = base_path('public/'.$full_dir.'pages/');

Additionally, ensure the paths exist. I'm not sure if it is meant to create the directory for you, so I'm not sure if this is a permission issue, I haven't looked into it.

Lastly, there is no need to set the format using a method as I believe it defaults as it is in the name.

See the complete code I've tested and is working:

$uploadsDir = 'uploads/';
$folder = base_path('public/' . $uploadsDir . 'pages/');
$quality = 100;

for($page = 1; $page <= $pdf->getNumberOfPages(); $page ++) {
    $pageSaveFilename = $folder . $page . '.png';
    $pdf->setPage($page)
        ->setCompressionQuality($quality)
        ->saveImage($pageSaveFilename);
}
spatie-bot commented 3 years ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.