In lieu of embedded-files-related examples, quite a few others distribution provides might be considered "suitable" to study, especially considering "Internals" and "Reference" (warning: 53 Mb PDF).

use strict; use warnings; use feature 'say'; use Data::Dump; use Encode 'decode'; use CAM::PDF; my $doc = CAM::PDF-> new( 'test.pdf' ) or die; dd get_embedded_files( $doc ); # Get a hashref. Keys (filenames) may happen to be encoded as # UTF16-BE, with prepended BOM. Sort it out yourself, if names # are relevant at all. sub get_embedded_files { my $doc = shift; my $names_dict = $doc-> getValue( $doc-> getRootDict-> { Names }) +or return {}; my $files_tree = $names_dict-> { EmbeddedFiles } +or return {}; my @agenda = $files_tree; my $ret = {}; # Hardly ever more than single leaf, but... while ( @agenda ) { my $item = $doc-> getValue( shift @agenda ); if ( $item-> { Kids }) { my $kids = $doc-> getValue( $item-> { Kids }); push @agenda, @$kids } else { my $nodes = $doc-> getValue( $item-> { Names }); my @names = map { $doc-> getValue( $_ )} @$nodes; while ( @names ) { my ( $k, $v ) = splice @names, 0, 2; my $ef_node = $v-> { EF }; my $ef_dict = $doc-> getValue( $ef_node ); my $any_num = ( values %$ef_dict )[ 0 ]-> { value }; my $obj_node = $doc-> dereference( $any_num ); $ret-> { $k } = $doc-> decodeOne( $obj_node-> { value +}, 0 ); } } } return $ret }

If it looks unpleasant and complex, then it is. "getValue" everywhere may lead to slight temporary madness in larger projects.

I only tested it against couple of my files. If anything is broken, here's a backup brute force solution (since files are probably simple and overhead tiny) -- just enumerate all objects, one of them will be yours.

use strict; use warnings; use feature 'say'; use CAM::PDF; use XML::LibXML; my $doc = CAM::PDF-> new( 'test.pdf' ) or die; $doc-> cacheObjects; while ( my ( $k, $v ) = each %{ $doc-> { objcache }}) { next unless $v-> { value }{ type } eq 'dictionary' and $v-> { value }{ value }{ StreamData }; my $str = $doc-> decodeOne( $v-> { value }, 0 ); my $dom = eval { XML::LibXML-> load_xml( string => $str )}; next unless $dom; # Process expected XML, skip possible others (Adobe XMP, etc.) # ... }

In reply to Re: Extracting embedded file from PDF by vr
in thread Extracting embedded file from PDF by staszeko

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.