Exd/lib/Exd/DeviceToRawFile.pm

73 lines
1.5 KiB
Perl

package Exd::DeviceToRawFile;
use v5.40.0;
use strict;
use warnings;
use Moo;
use GD::Image;
use Path::Tiny;
use Printer::ESCPOS;
has output_file => (
is => 'ro',
required => 1,
);
has _guts_device => (
is => 'lazy'
);
sub _build__guts_device($self) {
my $device = Printer::ESCPOS->new(
driverType => 'File',
deviceFilePath => $self->output_file,
);
}
sub image( $self, $image ) {
my $remaining_height = $image->height;
my $y = 0;
while ($remaining_height > 0) {
my $height = $remaining_height > 255 ? 255 : $remaining_height;
$remaining_height -= $height;
my $print_image = GD::Image->new($image->width, $height);
$print_image->copy( $image, 0, 0, 0, $y, $image->width, $height );
eval {
$self->_guts_device->printer->image($print_image);
};
if ($@) {
die 'Unable to write to file does ' . $self->output_file . " exist?, error: $@.";
}
$y += $height;
}
}
sub lf($self) {
eval {
$self->_guts_device->printer->lf;
};
if ($@) {
die 'Unable to write to file does ' . $self->output_file . " exist?, error: $@.";
}
}
sub print($self) {
eval {
$self->_guts_device->printer->print;
};
if ($@) {
die 'Unable to write to file, does ' . $self->output_file . " exist?, error: $@.";
}
}
sub serialize($self) {
my $hash = {%$self};
delete $hash->{_guts_device};
$hash->{type} = 'file';
return $hash;
}
1;