Renard-Incunabula-MuPDF-mutool
view release on metacpan or search on metacpan
lib/Renard/Incunabula/MuPDF/mutool.pm view on Meta::CPAN
my $stext = XMLin( $stext_xml,
KeyAttr => [],
ForceArray => [ qw(page block line font char) ] );
return $stext;
}
fun get_mutool_page_info_raw($pdf_filename) {
my $stdout = _call_mutool(
qw(pages),
$pdf_filename
);
# remove the first line
$stdout =~ s/^[^\n]*\n//s;
# wraps the data with a root node
return "<document>$stdout</document>"
}
fun get_mutool_page_info_xml($pdf_filename) {
my $page_info_xml = get_mutool_page_info_raw( $pdf_filename );
my $page_info = XMLin( $page_info_xml,
KeyAttr => [],
ForceArray => [ qw(page) ] );
return $page_info;
}
fun get_mutool_outline_simple($pdf_filename) {
my $outline_text = _call_mutool(
qw(show),
$pdf_filename,
qw(outline)
);
my @outline_items = ();
open my $outline_fh, '<:encoding(UTF-8):crlf', \$outline_text;
while( defined( my $line = <$outline_fh> ) ) {
$line =~ /^(?<indent>\t*)(?<text>.*)\t#(?<page>\d+)(,(?<dx>\d+),(?<dy>\d+))?$/;
my %copy = %+;
$copy{level} = length $copy{indent};
delete $copy{indent};
# not storing the offsets yet and not every line has offsets
delete @copy{qw(dx dy)};
push @outline_items, \%copy;
}
return \@outline_items;
}
fun get_mutool_get_trailer_raw($pdf_filename) {
my $trailer_text = _call_mutool(
qw(show),
$pdf_filename,
qw(trailer)
);
open my $trailer_fh, '<:encoding(UTF-8):crlf', \$trailer_text;
do { local $/ = ''; <$trailer_fh> };
}
fun get_mutool_get_object_raw($pdf_filename, $object_id) {
my $object_text = _call_mutool(
qw(show),
$pdf_filename,
$object_id,
);
open my $object_fh, '<:encoding(UTF-8):crlf', \$object_text;
do { local $/ = ''; <$object_fh> };
}
fun get_mutool_get_info_object_parsed( $pdf_filename ) {
my $trailer = Renard::Incunabula::MuPDF::mutool::ObjectParser->new(
filename => $pdf_filename,
string => Renard::Incunabula::MuPDF::mutool::get_mutool_get_trailer_raw($pdf_filename),
);
my $info = $trailer->resolve_key('Info');
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Renard::Incunabula::MuPDF::mutool - Retrieve PDF image and text data via MuPDF's mutool
=head1 VERSION
version 0.004
=head1 FUNCTIONS
=head2 _call_mutool
_call_mutool( @args )
Helper function which calls C<mutool> with the contents of the C<@args> array.
Returns the captured C<STDOUT> of the call.
This function dies if C<mutool> unsuccessfully exits.
=head2 get_mutool_pdf_page_as_png
get_mutool_pdf_page_as_png($pdf_filename, $pdf_page_no)
This function returns a PNG stream that renders page number C<$pdf_page_no> of
the PDF file C<$pdf_filename>.
=head2 get_mutool_text_stext_raw
get_mutool_text_stext_raw($pdf_filename, $pdf_page_no)
This function returns an XML string that contains structured text from page
number C<$pdf_page_no> of the PDF file C<$pdf_filename>.
The XML format is defined by the output of C<mutool> looks like this (for page
23 of the C<pdf_reference_1-7.pdf> file):
<?xml version="1.0"?>
<document name="(null)">
<page height="666" width="531">
( run in 2.703 seconds using v1.01-cache-2.11-cpan-364913b4093 )