Renard-API-MuPDF-mutool

 view release on metacpan or  search on metacpan

lib/Renard/API/MuPDF/mutool.pm  view on Meta::CPAN

			my $media_box = exists $page_hash->{MediaBox} ? $page_hash->{MediaBox} : $root_media_box;
			$page_hash->{CropBox} = { %$media_box };
		}
	}

	return $page_info;
}

fun get_mutool_outline_simple($pdf_filename) {
	my $outline_text = _call_mutool(
		qw(show),
		$pdf_filename,
		qw(outline)
	);

	my @outline_items = ();
	utf8::upgrade($outline_text);
	open my $outline_fh, '<:crlf', \$outline_text;
	while( defined( my $line = <$outline_fh> ) ) {
		$line =~ /^
			(?<prefix>[+|-])
			(?<indent>\t*)
			"(?<text>.*)"
			\t
			(?<reference>
				# #123,20,40
				( \# (?<page>\d+)(,(?<dx>-?\d+),(?<dy>-?\d+))? )
				|
				# #page=123&zoom=nan,20,40
				# #page=123&view=Fit
				( \# page=(?<page>\d+)(&(view|zoom)=[^&,]+?)*(,(?<dx>-?\d+),(?<dy>-?\d+))? )
				|
				\Q(null)\E
			)
			$
		/x;
		my %copy = %+;
		$copy{level} = length($copy{indent}) - 1;
		$copy{text} =~ s/\\x([0-9A-F]{2})/chr(hex($1))/ge;
		$copy{open} = $copy{prefix} eq '-';
		delete $copy{prefix};
		delete $copy{indent};
		delete $copy{reference};
		# 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)
	);

	utf8::upgrade($trailer_text);
	open my $trailer_fh, '<: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,
	);

	utf8::upgrade($object_text);
	open my $object_fh, '<:crlf', \$object_text;
	do { local $/ = ''; <$object_fh> };
}

fun get_mutool_get_info_object_parsed( $pdf_filename ) {
	my $trailer = Renard::API::MuPDF::mutool::ObjectParser->new(
		filename => $pdf_filename,
		string => Renard::API::MuPDF::mutool::get_mutool_get_trailer_raw($pdf_filename),
	);

	my $info = $trailer->resolve_key('Info');
}


1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Renard::API::MuPDF::mutool - Retrieve PDF image and text data via MuPDF's mutool

=head1 VERSION

version 0.006

=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 1.121 second using v1.01-cache-2.11-cpan-364913b4093 )