myelinviolin has asked for the wisdom of the Perl Monks concerning the following question:
I am writing a program that moves all pdf files from one location (mine is the desktop) to another "dump" location. As it does this, it appends the pdf to a document placed in a different location. The pdf that is being appended has a generated title page, the purpose of createpdf.pl. I have it to run continuously while the program is "ON" and can be turned off by the user by pressing "q". (I have it on a 1 second sleep for now). It works great when the pdfs are already on the desktop, but if I put a file on the desktop while the program is running, the file on the desktop is left corrupted. The pdf is still moved to the dump location and the main pdf file is still appended, but in order to really get rid of the file on the desktop I have to move the files from the dump folder back to the desktop. I think this may have something to do with the move() function in File::Copy. I used a close function to close the files after they are moved, thinking this might be the reason for a corrupted file to be left there, but it didn't fix anything. **I think the problem is in appendpdf.pl. The whole program runs when you run PDF.pl** The point of the program is to function as an automatic pdf appender when a lot of documents are printing from Adobe PDF printer at once. There isn't a way for the files to be put on the desktop without having to rename each one. You can turn that option off for the printer but then all the files that are named the same get overwritten. I want to grab the pdf as it is being made and put it somewhere else. Hopefully this will be fast enough to work, but if there is some kind of timing issue with the pdf files, it might not work. I am trying to use several modules and the only way I could figure out how to make them all work with each other was to put the pieces in different files.
PDF.pl
#!usr/bin/perl use Term::ReadKey; do 'user_options.pl'; do 'createpdf.pl'; print "Program ON\n"; ReadMode 4; # Turn off controls keys $input=0; until ($input eq "q"){ while (not defined ($input = ReadKey(-1))) { do 'appendpdf.pl'; sleep(1); } } ReadMode 0; # Reset tty mode before exiting print "\nProgram OFF\n";
appendpdf.pl
use CAM::PDF; use File::Copy; #Initialize title page pdf my $doc1 = CAM::PDF->new("$file1") || die "$CAM::PDF::errstr\n"; #Read each pdf file on the desktop opendir(DIR,$directory); my @files = grep{/\.pdf$/}readdir(DIR); closedir(DIR); #Get the names of all the files. foreach(@files[0]){ $name2=$_; $file2="$directory"."$name2"; $doc2 = CAM::PDF->new("$file2") || die "$CAM::PDF::errstr\n"; #Append the thermogram file with each document $doc1->appendPDF($doc2); $doc1->clearAnnotations(); $doc1->cleanoutput("$file1"); #Move each file to the destination folder $file3="$destination"."$name2"; move("$file2","$file3") || die "Move failed: $!"; $file2->close || die "Source file failed to close: $!"; $file3->close|| die "Target file failed to close: $!"; }
createpdf.pl
$file1="$output"."$filename"; use PDF::Create; # initialize PDF my $pdf = PDF::Create->new('filename'=> "$file1",'CreationDate' => + [ localtime ], ) || die "Could not initialize PDF."; # add a A4 sized page my $a4 = $pdf->new_page('MediaBox' => $pdf->get_page_size('A4')); # Add a page which inherits its attributes from $a4 my $page1 = $a4->new_page; # Prepare a font my $f1 = $pdf->font('BaseFont' => 'Helvetica'); #prompt for user input print "\n"; print "Name: "; $name=<>; print "Method: "; $method=<>; print "\n"; #get the date $date=localtime; # Write some text $page1->stringc($f1, 20, 306, 425, "$title"); $page1->stringc($f1, 20, 306, 375, "Author: $name"); $page1->stringc($f1, 20, 306, 350, "Method: $method"); $page1->stringc($f1, 20, 306, 300, "$date\n"); # Close the file and write the PDF $pdf->close || die "Could not write PDF.";
user_options.pl
#The directory where the files that need to be appended are located: $directory = 'C:/Users/X/Desktop/'; #This is where the final pdf file will print: $output = 'M:/perlscripts/'; #This is where the spent files will be collected: $destination='C:/Users/X/Desktop/Generated Thermograms/'; #This is what your final file name will be: $filename='Thermograms.pdf'; #The title in the PDF: $title='Thermograms';
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: File::Copy - move() function corrupting files
by Laurent_R (Canon) on Aug 22, 2014 at 18:02 UTC | |
by myelinviolin (Novice) on Aug 25, 2014 at 16:19 UTC | |
by Laurent_R (Canon) on Aug 25, 2014 at 17:29 UTC | |
Re: File::Copy - move() function corrupting files
by roboticus (Chancellor) on Aug 22, 2014 at 18:15 UTC | |
by myelinviolin (Novice) on Aug 25, 2014 at 16:11 UTC | |
by afoken (Chancellor) on Aug 26, 2014 at 20:22 UTC | |
Re: File::Copy - move() function corrupting files
by 2teez (Vicar) on Aug 22, 2014 at 19:26 UTC |