Mxpress-PDF-Mechanize
view release on metacpan or search on metacpan
lib/Mxpress/PDF/Mechanize.pm view on Meta::CPAN
package Mxpress::PDF::Mechanize;
use Zydeco prefix => 'Mxpress::PDF';
class Mxpress::PDF::Mechanize {
BEGIN {
our $VERSION = '0.05';
our $AUTHORITY = 'cpan:LNATION';
}
class Plugin::Mechanize::Screenshot extends Plugin::Image {
has mech_class (type => Str);
has mech_open_args (type => HashRef);
has selector (type => Str);
has js (type => Str);
has screenshot_w_scale (type => Num);
has screenshot_h_scale (type => Num);
has screenshot_left_offset (type => Num);
has screenshot_top_offset (type => Num);
has sleep (type => Num);
factory screenshot (Object $file, Map %args) {
$class->new(
mech_open_args => {
incognito => 0,
autodie => 0,
autoclose => 0,
headless => 1
},
file => $file,
mech_class => 'WWW::Mechanize::Chrome',
padding => 0,
screenshot_h_scale => 1,
screenshot_w_scale => 1,
screenshot_left_offset => 0,
screenshot_top_offset => 0,
align => 'fill',
%args
);
}
method take (Str $url, Map %args) { # around add
$self->set_attrs(%args);
$self->add($self->_mechanized_screenshot($url));
}
method _mechanized_screenshot (Str $url) {
$self->mech->get($url);
$self->mech->eval_in_page($self->js) if $self->js;
$self->mech->sleep($self->sleep) if $self->sleep;
my $img = $self->selector ? do {
my $shiny = $self->mech->selector($self->selector, single => 1);
my ($pos) = $self->mech->element_coordinates($shiny);
$pos->{width} = $pos->{width} * $self->screenshot_w_scale;
$pos->{height} = $pos->{height} * $self->screenshot_h_scale;
$pos->{left} += $self->screenshot_left_offset;
$pos->{top} += $self->screenshot_top_offset;
$self->mech->content_as_png($pos);
} : $self->mech->content_as_png();
open my $fh, "<", \$img;
return ($fh, 'png');
}
has _mech (type => Object); # builder
method mech {
return $self->_mech || $self->_mech($self->mech_class->new(%{$self->mech_open_args}));
}
}
}
1;
__END__
=head1 NAME
Mxpress::PDF::Mechanize - Take a screenshot and add it to the pdf
=head1 VERSION
Version 0.05
=cut
=head1 SYNOPSIS
This is a quick example of how to expand Mxpress::PDF...
( run in 0.492 second using v1.01-cache-2.11-cpan-e93a5daba3e )