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';

In reply to File::Copy - move() function corrupting files by myelinviolin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.