File Coverage

blib/lib/XML/Printer/ESCPOS.pm
Criterion Covered Total %
statement 9 11 81.8
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 13 15 86.6


line stmt bran cond sub pod time code
1             package XML::Printer::ESCPOS;
2              
3 1     1   12745 use 5.010;
  1         3  
4 1     1   3 use strict;
  1         1  
  1         14  
5 1     1   2 use warnings;
  1         3  
  1         22  
6 1     1   187 use XML::Parser;
  0            
  0            
7             use XML::Printer::ESCPOS::Tags;
8             use XML::Printer::ESCPOS::Debug;
9              
10             =head1 NAME
11              
12             XML::Printer::ESCPOS - An XML parser for generating ESCPOS output.
13              
14             =head1 DESCRIPTION
15              
16             This module provides a markup language that describes what your ESCPOS printer should do.
17             It works on top of the great and easy to use L. Now you can save your printer
18             output in an XML file and you can write templates to be processed by Template Toolkit or the
19             template engine of your choice.
20              
21             =cut
22              
23             our $VERSION = '0.04';
24              
25             =head1 SYNOPSIS
26              
27             use Printer::ESCPOS;
28             use XML::Printer::ESCPOS;
29              
30             # connect to your printer, see Printer::ESCPOS for more examples
31             my $device = Printer::ESCPOS->new(
32             driverType => 'Network',
33             deviceIp => '192.168.0.10',
34             devicePort => 9100,
35             );
36              
37             my $parser = XML::Printer::ESCPOS->new(printer => $device->printer);
38             $parser->parse(q#
39            
40             bold text
41             underlined text
42            
43             #) or die "Error parsing ESCPOS XML file: ".$parser->errormessage;
44              
45             $device->printer->cutPaper();
46             $device->printer->print();
47              
48              
49             =head1 METHODS
50              
51             =head2 new(printer => $printer)
52              
53             Constructs a new XML::Printer::ESCPOS object. You must provide a printer object you
54             get by Cnew(...)->printer>.
55              
56             If you do not set a printer, the module works in debug mode (see below).
57              
58             =cut
59              
60             sub new {
61             my ( $class, %options ) = @_;
62             $options{printer} ||= XML::Printer::ESCPOS::Debug->new();
63             return bless {%options}, $class;
64             }
65              
66             =head2 parse($xml)
67              
68             Parses the XML data given by C<$xml>. C<$xml> should contain the file content.
69             Returns 1 on success, undef otherwise. If parsing was unsuccessful, you can find the
70             errormessage by calling the C method.
71              
72             =cut
73              
74             sub parse {
75             my ( $self, $xml ) = @_;
76             my $printer = $self->{printer};
77              
78             my $xmlparser = XML::Parser->new( Style => 'Tree', );
79             my $tree = $xmlparser->parse($xml);
80              
81             return _set_error_message('no document found') if !@{$tree};
82             return _set_error_message('more than one base tag found') if @{$tree} > 2;
83             return _set_error_message('document is not an ESCPOS doc (start document with !)') if $tree->[0] ne 'escpos';
84              
85             my $tags = XML::Printer::ESCPOS::Tags->new(
86             printer => $self->{printer},
87             caller => $self,
88             );
89              
90             my $parsed = $tags->parse( $tree->[1] );
91             if ( ref $self->{printer} eq 'XML::Printer::ESCPOS::Debug' ) {
92             return $self->{printer}->as_perl_code();
93             }
94             return $parsed;
95             }
96              
97             =head2 errormessage
98              
99             Returns the last error message.
100              
101             =cut
102              
103             sub errormessage {
104             my $self = shift;
105             return $self->{errormessage};
106             }
107              
108             =head1 INTERNAL METHODS
109              
110             =head2 _set_error_message( $message )
111              
112             Internal method to set the error message in the object before the parser returns.
113              
114             =cut
115              
116             sub _set_error_message {
117             my ( $self, $message ) = @_;
118             $self->{errormessage} = $message;
119             return;
120             }
121              
122             =head1 DEBUG MODE
123              
124             Sometimes it helps to see what the module would do instead of doing it. That's why I added the debug mode:
125             If you do not set a printer object when calling the constructor, an L object is created.
126             It catches all method calls and the C method returns the perl code that would have been executed if
127             you had set the printer object.
128              
129             This XML code
130              
131            
132            
133             fontFamily = "DejaVu Sans"
134             fontStyle = "Bold"
135             fontSize = "76"
136             lineHeight = "115"
137             paperWidth = "832">This module
138            
139            
140             fontFamily = "DejaVu Sans"
141             fontStyle = "Bold"
142             fontSize = "36"
143             lineHeight = "55"
144             paperWidth = "832"
145             >is great for writing templates
146             https://github.com/sonntagd/XML-Printer-ESCPOS
147            
148              
149             will create the following perl code:
150              
151             $device->printer->utf8ImagedText(
152             'This module',
153             'fontFamily',
154             'DejaVu Sans',
155             'fontSize',
156             '76',
157             'fontStyle',
158             'Bold',
159             'lineHeight',
160             '115',
161             'paperWidth',
162             '832'
163             );
164             $device->printer->lf();
165             $device->printer->utf8ImagedText(
166             'is great for writing templates',
167             'fontFamily',
168             'DejaVu Sans',
169             'fontSize',
170             '36',
171             'fontStyle',
172             'Bold',
173             'lineHeight',
174             '55',
175             'paperWidth',
176             '832'
177             );
178             $device->printer->qr(
179             'https://github.com/sonntagd/XML-Printer-ESCPOS'
180             );
181              
182              
183             =head1 AUTHOR
184              
185             Dominic Sonntag, C<< >>
186              
187             =head1 BUGS
188              
189             Please report any bugs or feature requests by opening an issue on Github:
190             L
191              
192              
193             =head1 LICENSE AND COPYRIGHT
194              
195             Copyright 2017 Dominic Sonntag.
196              
197             This program is free software; you can redistribute it and/or modify it
198             under the terms of the the Artistic License (2.0). You may obtain a
199             copy of the full license at:
200              
201             L
202              
203             =cut
204              
205             1; # End of XML::Printer::ESCPOS