简体   繁体   中英

mikehaertl\pdftk\Pdf Null

I'm developing a Laravel application and I'm trying to retrieve fields from a PDF file using mikehaertl\\pdftk\\Pdf but it always return NULL.

 public function postPdF(Request $request ){
    $file = $request->file('test');
    $content = fopen($file->getRealPath(),'r');
    $name = $file->getClientOriginalName();
    $extension = \File::extension($name);
    $newName = time().".".$extension;
    Storage::disk("local")->put($newName,$content);
    $pathF =  storage_path('app')."\\".$newName; 

    $pdf = new Pdf($pathF);
    $data = $pdf->getDataFields();

}    

If I do

var_dump($pdf)  

it returns

string(0) ""    

If I do

var_dump($data)   

it returns

bool(false)    

How can I fix this problem?

fopen returns (essentially) a pointer to the file, not the file contents. That means when you're putting $content into a file, it's not doing what you're intending.

While you could just replace fopen with file_get_contents , I'd advise just replacing the following lines...

$file = $request->file('test');
$content = fopen($file->getRealPath(),'r');
$name = $file->getClientOriginalName();
$extension = \File::extension($name);
$newName = time().".".$extension;
Storage::disk("local")->put($newName,$content);

...with something like...

$destPath = storage_path('app') . '/' . time() . '.' . $request->test->extension();

Storage::disk('local')
  ->copy($request->test->path(), $destPath);

Receiving false as a return value from getDataFields() is an indication that something failed when executing the command. A few things to check:

  1. Is pdftk installed?
  2. I had to specify in the options of the Pdf class constructor where the pdftk binary was located.

     $pdf = new Pdf( $pathToPdf, ['command' => $pathToPdfTKBinary] ); 

    I came across that in the documentation for shell commmand .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM