sriram_perl_diver has asked for the wisdom of the Perl Monks concerning the following question:

Hi PMs, I'm pretty new to Perl. I have a problem with PDFLIB data matrix barcode generation. Initially when I ran the below code, I got the 'pdflib.upr' file not found issue. So I included the set_parameter("resourcefile") parameter available in pdflib by referring their manuals. The code still doesn't run successfully and it didn't printed the error message either. Here is my code:
#!/usr/bin/perl -w use warnings; use Data::Dumper; ##################### #Including libraries# ##################### use lib '/**/PDFlib'; use lib '/**/PDFModules'; use PDFlib; use POSIX qw(strftime); use File::Copy; use constant MARGIN => 30; use constant PAGE_WIDTH => 792; use constant PAGE_HEIGHT => 612; use constant TOOLTIP_TEXT => ''; use constant SAMPLE_TEXT => 'Test'; my $dir = "."; open INDEX, "$dir/input/index.txt" or die "\nError opening index file. + Please check if the index file exists.\n\n"; my @Inarr; while(<INDEX>) { my $line = $_; chomp($line); print "$line\n"; push @Inarr, "$line"; } close INDEX; my $src_dir = './input'; my $inputFile = substr $Inarr[2], 0, 39; my $dest_dir = './output'; if (length $inputFile == 0) { die("\nIndex file does not have the Input PDF file name.\n\n"); } my $log_dir = "$dest_dir/logs"; my $pdf_dir_name = substr $inputFile,0,-4; my $pdf_dir = "$dest_dir/$pdf_dir_name"; my $i = 0; my $pdfInstance = new PDFlib::PDFlib; $pdfInstance->set_option("resourcefile","/PDFlib/PDF/pdflib.upr"); my $date = strftime "%m%d%Y", localtime; my $time = strftime "%m%d%Y_%H%M%S", localtime; my $newLogFile = "report-$time.txt"; ##################################### #Creating Log and Output directories# ##################################### mkdir $dest_dir unless -d $dest_dir; chmod 0755, $dest_dir; mkdir $log_dir unless -d $log_dir; chmod 0755, $log_dir; mkdir $pdf_dir unless -d $pdf_dir; chmod 0755, $pdf_dir; my $infile_path = "$src_dir/$inputFile"; open LOGFILE, ">$log_dir/$newLogFile" or die "\nError creating Log fil +e.\n\n"; print LOGFILE "-Begin Log. Timestamp:$time-"; print LOGFILE "\n"; my $outfile = $pdf_dir.'/'."modified_".$pdf_dir_name.'.pdf'; if ($pdfInstance->begin_document("$outfile", "compatibility=1.7ext3") +== -1) { print LOGFILE ("ERROR in creating Output document:", $pdfInstance- +>get_errmsg()); print LOGFILE "\n-End of Log-"; print "\nError in creating Output document.\n\n"; } my $font = $pdfInstance->load_font("Helvetica", "winansi", ""); if ($font == 0) { print LOGFILE ("\nERROR in creating font instance:", $pdfInstance- +>get_errmsg()); print "\nERROR in creating font instance\n\n"; } my $opendoc = $pdfInstance->open_pdi_document($infile_path, ""); if ($opendoc == -1) { print LOGFILE ("ERROR in opening Input PDF file:", $pdfInstance->g +et_errmsg()); print LOGFILE "\n-End of Log-"; print "\nError in opening Input PDF file.\n\n"; exit; } $pdfInstance->set_info("Creator","Sriram"); $pdfInstance->set_info("Title", "Test"); my $pagehdl = $pdfInstance->open_pdi_page($opendoc, 1, ""); if ($pagehdl == -1) { print LOGFILE ("ERROR in opening the Input PDF file instance:", $p +dfInstance->get_errmsg()); print LOGFILE "\n-End of Log-"; print "\nError in opening the Input PDF file instance.\n\n"; } $pdfInstance->begin_page_ext(10, 10, ""); $pdfInstance->fit_pdi_page($pagehdl, 0, 0, "adjustpage"); $pdfInstance->close_pdi_page($pagehdl); $newoptlist = "barcode={symbology=DataMatrix dataprep=0 ecc=0 xsymwidt +h=10} font=" . $font; if($pdfInstance->create_field(180, 360, 288, 276, "testvalue", "textfi +eld", $newoptlist)) { print LOGFILE "\nField created\n"; } $pdfInstance->end_page_ext(""); $pdfInstance->close_pdi_document($opendoc); print LOGFILE "-End of Log-"; print "\nProcess completed successfully.\n\n"
Please help me out! Thanks in advance

Replies are listed 'Best First'.
Re: Problem with PDFLIB barcode generation
by toolic (Bishop) on Nov 05, 2014 at 17:48 UTC
    use PDFlib;
    Is this different from PDFLib on CPAN (note the capital "L")? If so, then ignore the rest of this post.
    my $pdfInstance = new PDFlib::PDFlib;
    PDFlib::PDFlib looks different from the PDFLib SYNOPSIS:
    use PDFLib; my $pdf = PDFLib->new("foo.pdf");

    Other considerations:

    • Reduce your code to the minimum required to reproduce your problem. If you suspect PDFLib to be the issue, you should be able to demonstrate the problem with about 5 lines of code.
    • Try to contact the module author via email for advice.
      Thanks for the reply toolic
      The PDFLib you suggested from CPAN is different from what I'm using. http://pdflib.com/
      I apologize for putting up the whole code. The place where I'm getting the error is
      my $font = $pdfInstance->load_font("Helvetica", "winansi", ""); if ($font == 0) { print LOGFILE ("\nERROR in creating font instance:", $pdfInstance- +>get_errmsg()); print "\nERROR in creating font instance\n\n"; }
      Both the Print statements are printing out the custom error message I typed (i.e.,ERROR in creating font instance).
      But the exception message coming from $pdfInstance->get_errmsg() is empty. Previously I got the file not found exception for pdflib.upr, which I fixed by using the resourcefile parameter available in pdflib. I added the font location in the upr file I mentioned above. But yet the code is failing.
      The system I'm working on is AIX. I'm not sure whether the 'Helvetica' font is available in that environment. Could this be a reason?

      Thanks
        There is a contact email address on that website. Give it a try.
Re: Problem with PDFLIB barcode generation
by perlron (Pilgrim) on Nov 05, 2014 at 17:58 UTC
    Hi

    Can you minimise the code sample so that monks can just attack the issue you are facing ?
    Your code above is not readily executable on my m/c and when i try to install PDFLIB cannot be installed without pdflif9 software.
    Are you sure this is not just a relative paths issue
    I hope you are aware of perl debug found in perldoc perldebtut or perldebug.
    • run your program in perl -d program.pl (type h for command menu)
    • set a breakpoint at line no. 44 in the case of this script using the command $>b 44
    • continye execution upto break using command $>c
    • step into the set_option method and see why its dieing
    If you make your code sample compact, it will be better i feel
    update a suggestion try even placing the missing file in current directory . I see you are using "." for determining current directory. check FindBin and determine your script path using $FindBin::Bin .

    The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code
      Thanks for the reply perlron

      I posted the specific code above in another reply.
      Many thanks for introducing the debugging steps. I'm a perl beginner and was completely unaware about this.

      Surely I'll use this debugging and will try to figure out what could be the issue stopping my code.

      I fixed the missing file issue by placing it in my current directory. The problem with the pdflib.upr was fixed by then. Now I'm getting an exception, which doesn't get written out to the logfile or console.
      Thanks

        Thats great news. Try using the capture method in module Capture::Tiny to capture errors and print it , if you feel the control is going outside the perl script. (pdflib9 might be causing an issue externally). FYI your error message(if any) from perl command execution will be stored in $! variable which u can print.

        The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code