在perl中解析pdf

前端之家收集整理的这篇文章主要介绍了在perl中解析pdf前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图从pdf中提取一些信息.我正在尝试使用CAM :: PDF模块中的getpdftext.pl.当我运行$~getpdftext.pl sample.pdf时,它会生成pdf到stdout的文本.

但我正在考虑将其写入文本文件并解析perl中的必填字段.有人可以指导我如何做到这一点?

但是当我尝试在我的perl脚本中调用pdftotext.pl时,我得到一个No No file错误.

#program从pdf中提取文本并将其保存在文本文件

use PDF;

use CAM::PDF;

use CAM::PDF::PageText;

use warnings;

use IPC::System::Simple qw(system capture);

$filein = 'sample.pdf';                                                                   
$fileout = 'output1.txt';  

open OUT,">$fileout" or die "error: $!";

open IN,"getpdftext.pl $filein" or die "error :$!" ;

while(<IN>)
{
    print OUT $fileout;
}

解决方法

让getpdftext.pl做你想做的事可能会更容易.

使用getpdftext.pl中的代码,这个(未经测试的代码)应该将pdf输出到文本文件.

my $filein = 'sample.pdf';                                                                   
my $fileout = 'output1.txt';  

my $doc = CAM::PDF->new($filein) || die "$CAM::PDF::errstr\n";
open my $fo,'>',$fileout or die "error: $!";

foreach my $p ( 1 .. $doc->numPages() ) {
    my $str = $doc->getPageText($p);
    if (defined $str) {
       CAM::PDF->asciify(\$str);
       print $fo $str;
    }
}

close $fo;

猜你在找的Perl相关文章