0

我使用以下代码从 jpgs 制作 pdf 文件。它工作正常,但我想要实现的是修改这个脚本,以便它可以在没有任何用户输入的情况下使用。我想右键单击 Windows 中的文件夹选择此脚本并制作 pdf。所以这个脚本需要获取一个文件夹名称并创建一个 pdf 文件而不询问它的名称。如果最终pdf的名称与jps所在的文件夹相同,那将是完美的。请问你能帮帮我吗?

注册表项如下:

"C:\草莓\perl\bin\perl.exe" "D:\test\790024.pl" %1

代码如下:

#!usr/bin/perl -w
    use strict;
    use PDF::API2;
    #concatenates image files of type JPEG, GIF, and PNG in a specific directory into a pdf file that will
    #be spit out into that same directory upon completion

    #very fun


    print "Input a filepath to a directory (ending with a /. ex: 'C:/path/to/dir/'): ";
    my $dir = <STDIN>;
    chomp $dir;
    print "\nInput the name for the pdf file you wish to create (ex: 'pictures'): ";
    my $pdf_name = <STDIN>;
    chomp $pdf_name;

    #grab a list of files in our very special directory.
    my @images = sort glob("$dir*") or die "No images\n";

    #xreate a new document with no pages
    my $pdf = PDF::API2->new;

    #loop through our images, create a page for each image
    # - while adding the image to the created page -> this is done in the subs
    for my $file (@images){
        #call sub based on what regex matches 
        add_jpg($file) if ($file =~ /.jpg/);
        #add_png($file) if ($file =~ /\.png$/);
        #add_png($file) if ($file =~ /.png/);
        #add_gif($file) if ($file =~ /.gif/);

    }

    $pdf->saveas("$dir$pdf_name.pdf");


    #subs 
    sub add_jpg{
        my $jpg = shift;
        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);

    }

    sub add_png{
        my $png = shift;
        my $image = $pdf->image_png($png);
        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);
    }

    sub add_gif{
        my $gif = shift;
        my $image = $pdf->image_gif($gif);
        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);
    }
4

1 回答 1

1

您可以使用@ARGV 数组来获取命令行参数。

#!/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);
于 2013-11-02T15:11:16.673 回答