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.32k stars 228 forks source link

After converting, the file does not open anymore. Message: Something went wrong. #213

Closed tiagocaus closed 10 months ago

tiagocaus commented 1 year ago

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.

public function upload_file($chave, $file_path): void
    {
        try {
            $this->s3->putObject([
                'Bucket' => $this->bucket_name,
                'Key' => "$chave/$file_path",
                'SourceFile' => $file_path,
                'ACL'    => 'public-read',
            ]);

            echo "Upload successful!\n";
        } catch (Exception $e) {
            //echo "Upload failed: ";
            echo "Upload failed: " . $e->getMessage() . "\n";
        }
    }

var_dump($pdf);

object(Spatie\PdfToImage\Pdf)[374]
  protected 'pdfFile' => string 'original.pdf' (length=12)
  protected 'resolution' => int 144
  protected 'outputFormat' => string 'jpg' (length=3)
  protected 'page' => int 1
  public 'imagick' => 
    object(Imagick)[375]
  protected 'numberOfPages' => int 1
  protected 'validOutputFormats' => 
    array (size=3)
      0 => string 'jpg' (length=3)
      1 => string 'jpeg' (length=4)
      2 => string 'png' (length=3)
  protected 'layerMethod' => int 14
  protected 'colorspace' => null
  protected 'compressionQuality' => int 80
  protected 'thumbnailWidth' => null
spatie-bot commented 10 months 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.