blib/lib/Vector/QRCode/IntoPDF.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Vector::QRCode::IntoPDF;
2 1 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use PostScript::Convert;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use PDF::API2;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Vector::QRCode::EPS;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use File::Temp 'tempdir';
9 use File::Spec;
10 use Carp;
11 use Class::Accessor::Lite (
12 new => 0,
13 ro => [qw[pdf_file workdir]],
14 );
15  
16 our $VERSION = "0.05";
17  
18 sub new {
19 my ($class, %opts) = @_;
20 croak 'pdf_file is required' unless $opts{pdf_file};
21  
22 $opts{workdir} ||= tempdir(CLEANUP => 1);
23  
24 bless {%opts}, $class;
25 }
26  
27 sub pdf {
28 my $self = shift;
29 $self->{__pdf} ||= PDF::API2->open($self->pdf_file) or croak $!;
30  
31 $self->{__pdf};
32 }
33  
34 sub imprint {
35 my ($self, %opts) = @_;
36  
37 my %_opts;
38 for my $key (qw/x y page/) {
39 $_opts{$key} = delete $opts{$key} or croak "$key is required";
40 }
41  
42 my $qr = $self->_qr_pdf(%opts) or croak "could not create qrcode";
43 my $page = $self->pdf->openpage($_opts{page}) or croak "illegal page";
44 my $gfx = $page->gfx;
45  
46 my $xobj = $self->pdf->importPageIntoForm($qr, 1);
47 $gfx->formimage($xobj, $_opts{x}, $_opts{y});
48 }
49  
50 sub save {
51 my ($self, $path) = @_;
52 $path ||= $self->pdf_file;
53 $self->pdf->saveas($path);
54 }
55  
56 sub _qr_pdf {
57 my ($self, %opts) = @_;
58  
59 my $qr = Vector::QRCode::EPS->generate(%opts);
60  
61 my $qr_data = $qr->get;
62 my $qr_pdf = File::Spec->catfile($self->workdir, ($qr+0).'.pdf');
63  
64 psconvert(\$qr_data, filename => $qr_pdf, format => 'pdf');
65  
66 PDF::API2->open($qr_pdf);
67 }
68  
69 1;
70 __END__
↑ Summary