Search code examples
phpexceptionpdflib

Catch a Specific Instance of a PHP Exception (PDFlib)


I have a script using the PDFlib library in PHP which I need to loop over a bunch of blocks and tell me their names and descriptions (if a description exists). PDFlib will always throw an exception when a $pdf->pcos_get_string() call fails, so I need to catch it.

The problem is I need to continue executing my loop after that particular execution is thrown. It doesn't seem like much of an issue, but my loop stops executing after the exception is thrown and caught. Not every block will have a description, so I can't just assume they do.

Code below:

try{
    $num_blocks = $pdf->pcos_get_number($input_file,'length:pages[0]/blocks');

    for($i=0;$i<$num_blocks;$i++){
        # This works fine
        $block_name = $pdf->pcos_get_string($input_file,'pages[0]/blocks['.$i.']/Name');

        try{
            # This will always throw an exception when there is no description
            $block_desc = $pdf->pcos_get_string($input_file,'pages[0]/blocks['.$i.']/Description');
        }catch(PDFlibException $e){
            # I want to print this, then go to the next loop iteration
            print 'Found block '.$block_name.' with no description'.PHP_EOL;
            continue;
        }catch(Exception $e){
            print 'Some other exception occurred'.PHP_EOL;
        }

        if($block_desc)
            # Print the block name
            print 'Found block '.$block_name.' with description '.$block_desc.PHP_EOL;
        }
}catch(PDFlibException $e){
    print $e;
    exit(1);
}catch(Exception $e){
    print $e;
    exit(1);
}

I expect output like:

Found block Address with description Where you live

Found block Name with description Your name

Found block State with no description

Found block Zip with description Your zipcode

... (sixteen lines of this)

My output actually looks like:

Found block Address with description Where you live

Found block Name with description Your name

Found block State with no description

My real issue here is the script stops executing in the middle of my loop as soon as it finishes the exception handler. What do I need to do to ensure the loop keeps executing?

Thanks in advance for your help.


Solution

  • as soon an exception occurs, the PDFlib object could not be longer used. (see PDFlib 9 Tutorial, chapter 3.1.1 "Exception Handling"): "It is important to understand that the generated PDF document cannot be finished when an exception occurred"

    So the solution is slightly different:

    • take care, that no exception occurs. You should check if the block property you try to retrieve is available.
    • you can do this, with the prefix "type:" to the pCOS path. Within the PDFlib 9 Tutorial chapter 12.7 "Querying Block Names and Properties with pCOS" you find the following explanation:

    Non-existing Block properties and default values. Use the type prefix to determine whether a Block or property is actually present. If the type for a path is 0 or null the respective object is not present in the PDF document. Note that for predefined properties this means that the default value of the property will be used.

    So a simple sample code for the above block Description might be:

        if ($pdf->pcos_get_number($input_file,'type:pages[0]/blocks['.$i.']/Description')!= 0){
                $block_desc = $pdf->pcos_get_string($input_file,'pages[0]/blocks['.$i.']/Description');
         }
    

    Check, if the pcos_get_number() return for the type a value != 0 (0 means "Null object or object not present (use to check existence of an object)", see pCOS Path Reference, chapter 4.2, table 4.1) If this value is available, you can retrieve the string without the prefix.

    You can also use the type value for creating a more general code, because so you can simple determine, if you have to retrieve a string, number, dictionary etc.

    You might find a complete Sample code in the PDFlib cookbook: http://www.pdflib.com/pdflib-cookbook/block-handling-and-pps/query-block-properties/php-query-block-properties/ Should demonstrate everything you need.