FPDF - failing to open eps file?

user285594

I am unable to use EPS file in FPDF (but here it is able to open EPS http://www.fpdf.org/en/script/script84.php ). What is wrong in my code?

Error:

Fatal error: Uncaught Exception: FPDF error: File was saved with wrong Illustrator version: nato.eps in eps/fpdf/fpdf.php:271 Stack trace: #0 eps/fpdf_eps.php(42): FPDF->Error('File was saved ...') #1 eps/test.php(7): PDF_EPS->ImageEps('test.eps', 30, 20, 150, 0, 1) #2 {main} thrown in eps/fpdf/fpdf.php on line 271

test.php:

require('fpdf_eps.php');
$pdf=new PDF_EPS();
$pdf->AddPage();
$lnk = $pdf->AddLink();
$pdf->ImageEps('test.eps', 30, 20, 150, 0, $lnk);
$pdf->Output();

fpdf_eps.php:

/*
* Software: FPDF_EPS
* Version:  1.6
* Date:     2008-02-06
* Author:   Valentin Schmidt       */

require('fpdf/fpdf.php');

class PDF_EPS extends FPDF{

function ImageEps ($file, $x, $y, $w=0, $h=0, $link='', $useBoundingBox=true){

    $data = file_get_contents($file);
    if ($data===false) $this->Error('EPS file not found: '.$file);

    $regs = array();

    # EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
    preg_match ('/%%Creator:([^\r\n]+)/', $data, $regs); # find Creator
    if (count($regs)>1){
        $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
        if (strpos($version_str, 'Adobe Illustrator')!==false){
            $a = explode(' ', $version_str);
            $version = (float)array_pop($a);
            if ($version>=9)
                $this->Error('File was saved with wrong Illustrator version: '.$file);
                #return false; # wrong version, only 1.x, 3.x or 8.x are supported
        }#else
        #$this->Error('EPS wasn\'t created with Illustrator: '.$file);
    }

    # strip binary bytes in front of PS-header
    $start = strpos($data, '%!PS-Adobe');
    if ($start>0) $data = substr($data, $start);

    # find BoundingBox params
    preg_match ("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
    if (count($regs)>1){
        list($x1,$y1,$x2,$y2) = explode(' ', trim($regs[1]));
    }
    else $this->Error('No BoundingBox found in EPS file: '.$file);

    $start = strpos($data, '%%EndSetup');
    if ($start===false) $start = strpos($data, '%%EndProlog');
    if ($start===false) $start = strpos($data, '%%BoundingBox');

    $data = substr($data, $start);

    $end = strpos($data, '%%PageTrailer');
    if ($end===false) $end = strpos($data, 'showpage');
    if ($end) $data = substr($data, 0, $end);

    # save the current graphic state
    $this->_out('q');

    $k = $this->k;

    if ($useBoundingBox){
        $dx = $x*$k-$x1;
        $dy = $y*$k-$y1;
    }else{
        $dx = $x*$k;
        $dy = $y*$k;
    }

    # translate
    $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', 1,0,0,1,$dx,$dy+($this->hPt - 2*$y*$k - ($y2-$y1))));

    if ($w>0){
        $scale_x = $w/(($x2-$x1)/$k);
        if ($h>0){
            $scale_y = $h/(($y2-$y1)/$k);
        }else{
            $scale_y = $scale_x;
            $h = ($y2-$y1)/$k * $scale_y;
        }
    }else{
        if ($h>0){
            $scale_y = $h/(($y2-$y1)/$k);
            $scale_x = $scale_y;
            $w = ($x2-$x1)/$k * $scale_x;
        }else{
            $w = ($x2-$x1)/$k;
            $h = ($y2-$y1)/$k;
        }
    }

    # scale
    if (isset($scale_x))
        $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $scale_x,0,0,$scale_y, $x1*(1-$scale_x), $y2*(1-$scale_y)));

    # handle pc/unix/mac line endings
    $lines = preg_split ("/\r\n|[\r\n]/", $data);

    $u = 0;
    $cnt = count($lines);
    for ($i=0;$i<$cnt;$i++){
        $line = $lines[$i];
        if ($line=='' || $line[0]=='%') continue;

        $len = strlen($line);

        $chunks = explode(' ', $line);
        $cmd = array_pop($chunks);

        # RGB
        if ($cmd=='Xa'||$cmd=='XA'){
            $b = array_pop($chunks); $g = array_pop($chunks); $r = array_pop($chunks);
            $this->_out("$r $g $b ". ($cmd=='Xa'?'rg':'RG') ); #substr($line, 0, -2).'rg' -> in EPS (AI8): c m y k r g b rg!
            continue;
        }

        switch ($cmd){
            case 'm':
            case 'l':
            case 'v':
            case 'y':
            case 'c':

            case 'k':
            case 'K':
            case 'g':
            case 'G':

            case 's':
            case 'S':

            case 'J':
            case 'j':
            case 'w':
            case 'M':
            case 'd' :

            case 'n' :
            case 'v' :
                $this->_out($line);
                break;

            case 'x': # custom fill color
                list($c,$m,$y,$k) = $chunks;
                $this->_out("$c $m $y $k k");
                break;

            case 'X': # custom stroke color
                list($c,$m,$y,$k) = $chunks;
                $this->_out("$c $m $y $k K");
                break;

            case 'Y':
            case 'N':
            case 'V':
            case 'L':
            case 'C':
                $line[$len-1] = strtolower($cmd);
                $this->_out($line);
                break;

            case 'b':
            case 'B':
                $this->_out($cmd . '*');
                break;

            case 'f':
            case 'F':
                if ($u>0){
                    $isU = false;
                    $max = min($i+5,$cnt);
                    for ($j=$i+1;$j<$max;$j++)
                        $isU = ($isU || ($lines[$j]=='U' || $lines[$j]=='*U'));
                    if ($isU) $this->_out("f*");
                }else
                    $this->_out("f*");
                break;

            case '*u':
                $u++;
                break;

            case '*U':
                $u--;
                break;

            #default: echo "$cmd<br>"; #just for debugging
        }

    }

    # restore previous graphic state
    $this->_out('Q');
    if ($link)
        $this->Link($x,$y,$w,$h,$link);

    return true;
}

}# END CLASS
KenS

I believe the problem is that you are confusing a script which specifically deals with EPS files created by Adobe Illustrator version 9, with a scritp which can handle general EPS files.

EPS stands for Encapsulated PostScript, PostScript is a programming language. So what is in an EPS file is a PostScript program.

Now machine generated PostScript, such as that produced by Adobe Illustrator, is of course always the same general format and its possible (as here) to write some code which can deal with its content in a limited fashion. However, the program generated by a different application will be different, possibly very different.

The only way to be able to read a general EPS (or PostScript file) is to use a full PostScript interpreter such as Ghostscript.

The script you have above is capable of reading some of the marking content of an EPS generated by a specific version of Adobe Illustrator and rendering a PDF file from it, it is not a general PostScript interpreter and so is not capable of interpreting arbitrary EPS files. The author says as much in the page you linked to in your question.

Only vector drawing is supported, not text or bitmap

Now notice that the exception is right at the beginning of the script where it checks the %%Creatorcomment to see if its the correct version of Illustrator. That's where it fails for you, undoubtedly because your EPS (which you haven't shared) doesn't include the correct comment.

In short you can't use this script to interpret general PostScript or EPS programs. If you want to do that you need something like Ghostscript or Adobe Acrobat Distiller.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I open an EPS file in GIMP 2? "Could not interpret PostScript file"

From Dev

exporting figure to eps file

From Dev

Failing File I/O is_open() in Qt Creator

From Dev

Cron is trying (and failing) to open env file: /etc/environment

From Dev

ifstream failing to open

From Dev

Eclipse failing to open

From Dev

Failing to open spreadsheet

From Dev

why are [ fopen() / open() / fstream file ] failing on Mac OS X for opening a file?

From Dev

FPDF - output dynamic file name

From Dev

Output file to server directory in FPDF

From Dev

PHP, fpdf, damaged file download

From Dev

Node.js sporadically failing to write file with: Error: ENOENT, open 'filename'

From Dev

vbscript called from batch file from task scheduler failing to open excel workbook

From Dev

git failing to find the index when in submodule ("index file open failed: not a directory")

From Dev

git failing to find the index when in submodule ("index file open failed: not a directory")

From Dev

startx failing from debian console in virtualbox VM: (EE) open /dev/dri/card0: No such file or directory

From Dev

What is the exact meaning of %%BoundingBox and %ImageData in an EPS file

From Dev

LaTeX document and BoundingBox of an eps file created by matplotlib

From Dev

Distortion while saving matlab figure as a eps file

From Dev

c# meta data extractor for eps file

From Dev

How to save an .EPS file to PNG with transparency in Python

From Dev

Why is this file failing to download?

From Dev

File upload failing in golang

From Dev

Ajax file upload is failing

From Dev

Jar file failing to execute

From Dev

File Reading Failing

From Dev

including a file is failing in php

From Dev

Lookup in CSV file is failing

From Dev

Try to show pdf file using PHP and fpdf

Related Related

  1. 1

    How do I open an EPS file in GIMP 2? "Could not interpret PostScript file"

  2. 2

    exporting figure to eps file

  3. 3

    Failing File I/O is_open() in Qt Creator

  4. 4

    Cron is trying (and failing) to open env file: /etc/environment

  5. 5

    ifstream failing to open

  6. 6

    Eclipse failing to open

  7. 7

    Failing to open spreadsheet

  8. 8

    why are [ fopen() / open() / fstream file ] failing on Mac OS X for opening a file?

  9. 9

    FPDF - output dynamic file name

  10. 10

    Output file to server directory in FPDF

  11. 11

    PHP, fpdf, damaged file download

  12. 12

    Node.js sporadically failing to write file with: Error: ENOENT, open 'filename'

  13. 13

    vbscript called from batch file from task scheduler failing to open excel workbook

  14. 14

    git failing to find the index when in submodule ("index file open failed: not a directory")

  15. 15

    git failing to find the index when in submodule ("index file open failed: not a directory")

  16. 16

    startx failing from debian console in virtualbox VM: (EE) open /dev/dri/card0: No such file or directory

  17. 17

    What is the exact meaning of %%BoundingBox and %ImageData in an EPS file

  18. 18

    LaTeX document and BoundingBox of an eps file created by matplotlib

  19. 19

    Distortion while saving matlab figure as a eps file

  20. 20

    c# meta data extractor for eps file

  21. 21

    How to save an .EPS file to PNG with transparency in Python

  22. 22

    Why is this file failing to download?

  23. 23

    File upload failing in golang

  24. 24

    Ajax file upload is failing

  25. 25

    Jar file failing to execute

  26. 26

    File Reading Failing

  27. 27

    including a file is failing in php

  28. 28

    Lookup in CSV file is failing

  29. 29

    Try to show pdf file using PHP and fpdf

HotTag

Archive