the_student's avatar

Merging Multiple Photos and PDFs into a Single PDF

I've built a method that merges multiple photos and PDFs into a single large PDF file using the following libraries:

use Imagick;
use Spatie\PdfToImage\Pdf;

I'm developing this on a Windows 11 machine.

Issue

When I hardcode the source and destination paths as follows:

$sourcePath = "C:\\Users\\test\\testPhotoCreation\\sourcePathForPdf\\";  
$destinationPdfPath = "C:\\Users\\test\\testPhotoCreation\\pdfCreatedDestination\\merged.pdf";

everything works as expected.

However, when I modify the implementation to accept a user-inputted path (retrieved via an HTML form), I get the following error when executing this line:

$pdf = new Pdf($file);

Error Message

FailedToExecuteCommand `"gs" -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pamcmyk32" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72" -dPrinted=false "-sOutputFile=C:/Users/test/AppData/Local/Temp/magick-gzSUuBgN1oiEKEsxRVP20rd1XIgpCdtM%d" "-fC:/Users/test/AppData/Local/Temp/magick-3ZbYt3YgbMobScyYalUK5TNBTNwjgoPn`

Debugging Steps Taken

  1. Ensured Proper Path Formatting: Used \\ in paths, but the issue persists.
  2. Reverted Formatting Changes: Still encountering the same error.
  3. Only Difference: The failing scenario dynamically receives paths from user input instead of being hardcoded.

Question

What could be causing this issue when using a dynamically received file path? Could it be related to:

  • Path encoding issues on Windows?
  • Permissions or security settings?
  • Ghostscript (gs) not handling dynamically set paths correctly?

I've attached my code below, with the original hardcoded paths commented out.


Full PHP Code


Summary

This function:

  1. Copies all images and PDFs into a temporary folder.
  2. Processes PDFs by converting each page into an image.
  3. Sorts files by order number and page number.
  4. Merges all images into a single PDF using Imagick.
  5. Deletes the temporary folder after processing.

Any insights or suggestions to resolve the issue with dynamically received file paths would be greatly appreciated!

0 likes
0 replies

Please or to participate in this conversation.