PDF file does not open after converting. The original file opens normally on the computer. It is 6MB and contains 8 pages.
Input file:
Name: original.pdf
Size: 6MB
Output file:
Name: original.pdf
Size: 1.65MB
PHP: 8.1
Error:
after converting, the file does not open anymore. From failure.
My code:
<?php
namespace App\Classes;
use Exception;
use Spatie\PdfToImage\Pdf;
class FileHandler
{
public function __construct()
{
}
/**
* @throws Exception
*/
public function handle_file($file_path): void
{
$allowed_types = ['image/jpeg', 'image/png', 'application/pdf'];
// Check if the file type is allowed
$file_type = mime_content_type($file_path);
if (!in_array($file_type, $allowed_types)) {
throw new Exception("Invalid file type: {$file_type}");
}
// Reduce the quality of images
if (str_starts_with($file_type, 'image/')) {
$image = imagecreatefromstring(file_get_contents($file_path));
imagejpeg($image, $file_path, 80);
}
// Reduce the size of files
$max_size = 1024 * 1024; // 1 MB
if (filesize($file_path) > $max_size) {
$pdf = new Pdf($file_path);
$pdf->setCompressionQuality(80);
$pdf->setPage(1);
$pdf->saveImage($file_path);
}
}
}
Short class, which takes the converted file and sends it to the server.
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.
PDF file does not open after converting. The original file opens normally on the computer. It is 6MB and contains 8 pages.
Input file: Name: original.pdf Size: 6MB
Output file: Name: original.pdf Size: 1.65MB
PHP: 8.1
Error: after converting, the file does not open anymore. From failure.
My code:
Short class, which takes the converted file and sends it to the server.
var_dump($pdf);