pdfsvgepsphp-7.4php-7.3

PHP - Convert PDF or ai file to svg file


I have a PDF file and an AI file and I want it to convert SVG.

I have installed PHP IMAGICK extension in my localhost and also installed the GHOSTSCRIPT. code which I have tried is

$image = new Imagick();
$image->setResolution(1000,1000);
$image->readImage('C:\xampp\htdocs\project\test.pdf');
$color = $image->getImageColors();
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->scaleImage(600, 270);
$image->setImageFormat("svg");
$image->writeImage('C:\xampp\htdocs\project\test_convert.svg');

Following error is coming:

Fatal error: Uncaught ImagickException: PDFDelegateFailed `The system cannot find the file specified. ' @ error/pdf.c/ReadPDFImage/794 in C:\xampp\htdocs\Project\Test.php:4 Stack trace: #0 C:\xampp\htdocs\Project\Test.php(4): Imagick->readimage('C:\xampp\htdocs...') #1 {main} thrown in C:\xampp\htdocs\Project\Test.php on line 4

I'm using Xampp and it has installed Imagick x86 (32 bits), so installing Ghostscript for 32 bits and tried 64 bits and also renamed gswin64.exe to gs.exe

Can anyone help me out to solve this error?


Solution

  • try the following code

    $file= fopen('anycloud.ai', 'rb'); // pu here your ai or eps file
    
    $img = new imagick(); // [0] can be used to set page number
    $img->setResolution(300,300);
    $img->readImageFile($file);
    $img->setImageFormat( "jpg");
    $img->setImageCompression(imagick::COMPRESSION_JPEG);
    $img->setImageCompressionQuality(90);
    $img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
    $img->writeImage('convert_jpg_from_ai.jpg');