简体   繁体   English

mikehaertl \\ pdftk \\ Pdf空

[英]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. 我正在开发Laravel应用程序,并尝试使用mikehaertl \\ pdftk \\ Pdf从PDF文件检索字段,但它始终返回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. fopen (本质上)返回指向文件的指针,而不是文件内容的指针。 That means when you're putting $content into a file, it's not doing what you're intending. 这意味着当您将$content放入文件中时,它并没有达到您的预期目的。

While you could just replace fopen with file_get_contents , I'd advise just replacing the following lines... 虽然您可以用file_get_contents代替fopen ,但我建议您只替换以下几行...

$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. 从getDataFields()返回false作为返回值表示执行命令时某些操作失败。 A few things to check: 需要检查的几件事:

  1. Is pdftk installed? 是否安装了pdftk
  2. I had to specify in the options of the Pdf class constructor where the pdftk binary was located. 我必须在Pdf类构造函数的选项中指定pdftk二进制文件所在的位置。

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

    I came across that in the documentation for shell commmand . 我在shell commmand文档中发现了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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