0

我有以下脚本可以从 jpg 制作 pdf。它工作正常,但是当文件夹为空或没有 jps 时,它会创建并清空 pdf 文件。如何避免?

#!/usr/bin/perl

use PDF::API2;
use strict;

# assume all just one directory path
my $folder = join(' ', @ARGV);

# deal with dos paths
$folder =~ s|\\|/|g;

$folder =~ s|/$||;

my $pdf_file = $folder . '.pdf';

die "Not a folder!\n" unless -d $folder;
die "There's already a pdf of that name!\n" if -f $pdf_file;

my $pdf = PDF::API2->new;

opendir DIR, $folder;
while(my $file = readdir DIR) {
    next unless $file =~ /\.je?pg$/i;
    my $jpg = $folder . '/' . $file;

    my $image = $pdf->image_jpeg($jpg);
    my $page = $pdf->page();
    $page->mediabox(0,0,$image->width, $image->height);
    $page->trimbox(0,0,$image->width, $image->height);
    my $gfx = $page->gfx;
    $gfx->image($image, 0, 0);
}
close DIR;

$pdf->saveas($pdf_file);
4

1 回答 1

3

我相信你可以用 做一些事情glob,但添加可能更容易

my $someFilesFound;

在while循环之前。在循环体中将其设置为 1 并在调用之前对其进行测试saveas

于 2013-11-07T20:50:35.990 回答