PDF-WebKit

 view release on metacpan or  search on metacpan

lib/PDF/WebKit.pm  view on Meta::CPAN

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  $self->_append_stylesheets;
  my @args = $self->command($path);
 
  my $input = $self->source->is_html ? $self->source->content : undef;
  my $output;
 
  my %opt = map +( "binmode_std$_" => ":raw" ), "in", "out", "err";
  run3 \@args, \$input, \$output, \my $err, \%opt;
 
  if ($path) {
    $output = do { local (@ARGV,$/) = ($path); <> };
  }
 
  if (not (defined($output) && length($output))) {
    Carp::croak "command failed: $args[0]";
  }
  return $output;
}
 
sub to_file {
  my $self = shift;

lib/PDF/WebKit.pm  view on Meta::CPAN

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    my $name = $node->getAttribute('name');
    next unless ($name && ($name =~ s{^\Q$prefix}{}s));
    $meta{$name} = $node->getAttribute('content');
  }
 
  return %meta;
}
 
sub _style_tag_for {
  my ($self,$stylesheet) = @_;
  my $styles = do { local (@ARGV,$/) = ($stylesheet); <> };
  return "<style>$styles</style>";
}
 
sub _append_stylesheets {
  my $self = shift;
  if (@{ $self->stylesheets } && !$self->source->is_html) {
    Carp::croak "stylesheets may only be added to an HTML source";
  }
  return unless $self->source->is_html;

t/pdf-webkit-integration.t  view on Meta::CPAN

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  my $pdfkit = PDF::WebKit->new(\'html', header_spacing => 1);
  my $pdf = $pdfkit->to_pdf;
  is(substr($pdf,0,4), '%PDF'); # PDF Signature at beginning of file
};
 
it "should have the stylesheet added to the head if it has one" => sub {
  my $pdfkit = PDF::WebKit->new(\"<html><head></head><body>Hai!</body></html>");
  my $stylesheet = File::Spec->catfile($SPEC_ROOT,'fixtures','example.css');
  push @{ $pdfkit->stylesheets }, $stylesheet;
  $pdfkit->to_pdf;
  my $css = do { local (@ARGV,$/) = ($stylesheet); <> };
  like($pdfkit->source->content, qr{<style>\Q$css\E</style>});
};
 
it "should prepend style tags if the HTML doesn't have a head tag" => sub {
  my $pdfkit = PDF::WebKit->new(\"<html><body>Hai!</body></html>");
  my $stylesheet = File::Spec->catfile($SPEC_ROOT,'fixtures','example.css');
  push @{ $pdfkit->stylesheets }, $stylesheet;
  $pdfkit->to_pdf;
  my $css = do { local (@ARGV,$/) = ($stylesheet); <> };
  like($pdfkit->source->content, qr{<style>\Q$css\E</style><html>});
};
 
it "should throw an error if the source is not html and stylesheets have been added" => sub {
  my $pdfkit = PDF::WebKit->new('http://google.com');
  my $stylesheet = File::Spec->catfile($SPEC_ROOT,'fixtures','example.css');
  push @{ $pdfkit->stylesheets }, $stylesheet;
  eval { $pdfkit->to_pdf };
  like($@, qr/stylesheet.*html/i);
};



( run in 0.722 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )