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

Failed to read the file when trying to convert pdf to jpg #231

Closed Ishantnarula21 closed 3 months ago

Ishantnarula21 commented 3 months ago

Failed to read the file when trying to convert pdf to jpg:

I'm encountering an error while attempting to convert a PDF file to an image using the Pdf class. The specific line causing the error is:

use Spatie\PdfToImage\Pdf;

$pdf = new Pdf($filePath); // This line is giving error
Error Message:
[Insert the exact error message you're receiving here.]

Steps to Reproduce:
Ensure a PDF file named example.pdf exists in the storage/app directory.
Use the following code:

$filePath = storage_path('app/example.pdf');
$pdf = new Pdf($filePath); // This line is giving error
$pdf = $pdf->setPage(1)->saveImage(storage_path('app/example-1.jpg'));

Additional Context:

I've tried several approaches to resolve this issue without success. One of the attempts involved using Imagick directly, but I encountered another error stating "no decode delegate for this image format."

Error :

"error": "Failed to read the file",
patinthehat commented 3 months ago

@Ishantnarula21 Have you tried changing your Imagick policy.xml resource settings? This error can occur if the PDF has hi-res images or needs a lot of memory to convert to an image. Try disabling or increasing the limits for the resource directives, which has worked for me in the past.

Also see this GitHub issue.

Ishantnarula21 commented 3 months ago

@patinthehat currently this is my policy.xml i have give pdf read write access

<!DOCTYPE policymap [
  <!ELEMENT policymap (policy)*>
  <!ATTLIST policymap xmlns CDATA #FIXED ''>
  <!ELEMENT policy EMPTY>
  <!ATTLIST policy xmlns CDATA #FIXED '' domain NMTOKEN #REQUIRED
    name NMTOKEN #IMPLIED pattern CDATA #IMPLIED rights NMTOKEN #IMPLIED
    stealth NMTOKEN #IMPLIED value CDATA #IMPLIED>
]>
<policymap>
  <policy domain="resource" name="memory" value="256MiB"/>
  <policy domain="resource" name="map" value="512MiB"/>
  <policy domain="resource" name="width" value="16KP"/>
  <policy domain="resource" name="height" value="16KP"/>
  <policy domain="resource" name="area" value="128MP"/>
  <policy domain="resource" name="disk" value="1GiB"/>
  <!-- use curl -->
  <policy domain="delegate" rights="none" pattern="URL" />
  <policy domain="delegate" rights="none" pattern="HTTPS" />
  <policy domain="delegate" rights="none" pattern="HTTP" />
  <!-- in order to avoid to get image with password text -->
  <policy domain="path" rights="none" pattern="@*"/>
  <!-- disable ghostscript format types -->
  <policy domain="coder" rights="none" pattern="PS" />
  <policy domain="coder" rights="none" pattern="PS2" />
  <policy domain="coder" rights="none" pattern="PS3" />
  <policy domain="coder" rights="none" pattern="EPS" />
  <policy domain="coder" rights="read|write" pattern="PDF" />
  <policy domain="coder" rights="none" pattern="XPS" />
</policymap>

I have tried with imagick directly getting this issue in this pdf is readable but this issue is there

no decode delegate for this image format `' @ error/constitute.c/ReadImage/575
patinthehat commented 3 months ago

@Ishantnarula21 It sounds like you might be missing a library or a related issue. See this issue in the ImageMagick repo.

patinthehat commented 3 months ago

Closing this issue as unrelated. Please open a new issue if you find a bug that's causing this, as all tests are currently passing. Thanks! :+1: