PDF-API2
view release on metacpan or search on metacpan
lib/PDF/API2/Resource/XObject/Form/BarCode/qrcode.pm view on Meta::CPAN
package PDF::API2::Resource::XObject::Form::BarCode::qrcode;
use base 'PDF::API2::Resource::XObject::Form::BarCode';
use strict;
use warnings;
our $VERSION = '2.048'; # VERSION
use Carp;
our @CARP_NOT = qw(PDF::API2);
sub new {
my ($class, $pdf, %options) = @_;
my $self = $class->SUPER::new($pdf, %options);
eval {
require Text::QRCode;
};
if ($@) {
croak "Error loading Text::QRCode: $@";
}
my $mode = $options{'mode'} // '8-bit';
$mode = 'numerical' if $mode eq 'numeric';
$mode = 'alpha-numerical' if $mode eq 'alphanumeric';
$mode = 'alpha-numerical' if $mode eq 'alpha-numeric';
my $generator = Text::QRCode->new(
level => ($options{'error_correction'} // 'L'),
version => ($options{'version'} // 0),
mode => $mode,
casesensitive => ($options{'case_sensitive'} // 1),
);
my $lines_ref = $generator->plot($options{'code'});
my @lines = reverse map { join('', @$_) } @$lines_ref;
$self->render(@lines);
return $self;
}
sub render {
my ($self, @lines) = @_;
$self->fillcolor($self->{' color'});
my $y = $self->{' quzn'};
my $w = $self->{' mils'} / 1000 * 72;
my $h = $self->{' zone'};
foreach my $line (@lines) {
my $x = $self->{' quzn'};
foreach my $char (split m//, $line) {
if ($char eq '*') {
$self->rectangle($x, $y, $x + $w, $y + $h);
$self->fill();
}
$x += $w;
}
$y += $h;
}
$self->{' w'} = 2 * $self->{' quzn'} + length($lines[0]) * $w;
$self->{' h'} = 2 * $self->{' quzn'} + length($lines[0]) * $h;
$self->bbox(0, 0, $self->{' w'}, $self->{' h'});
return $self;
}
1;
__END__
=head1 NAME
PDF::API2::Resource::XObject::Form::BarCode::qrcode - Generate QR codes
=head1 SYNOPSIS
use PDF::API2;
my $pdf = PDF::API2->new();
my $page = $pdf->page();
# Create a QR Code object
my $barcode = $pdf->barcode('qr', 'This is a test');
# Calculate the scale needed for a desired size (72pt / inch)
my $desired_inches = 2;
my $scale = $desired_inches * 72 / $barcode->width();
# Place the QR Code one inch (72pt) from the bottom left corner
$page->object($barcode, 72, 72, $scale);
$pdf->save('qr_test.pdf');
=head1 REQUIREMENTS
L<Text::QRCode> is used to encode QR codes.
=head1 OPTIONS
my $barcode = $pdf->barcode('qr', $value, %options);
The following standard barcode options are supported, as described in
L<PDF::API2/barcode>:
=over
=item * bar_width (default: 1; unit is points)
=item * bar_height (default: bar width)
( run in 0.758 second using v1.01-cache-2.11-cpan-71847e10f99 )