Printing from Perl Scripts NET::Printer

Hoping for some help to get me started in the right direction.

I can print documents to my network printer but i’m failing to print a PDF document to the same printer from a perl script.

me’s thinking that i have something missing, something that needs installing and or setting up in addition to what i already have.

running the command lpstat -a returns the following result

lpstat -a
HP_Color_LaserJet_CP2025n accepting requests since Wed 10 Apr 2019 22:05:32 BST

running the command lp -d HP_Color_LaserJet_CP2025n “Test Sheet.txt”
request id is HP_Color_LaserJet_CP2025n-8 (1 file(s))

Prints out the txt file OK, printing a PDF document in the same way works too.

Network Printer: HP CP2025n, Static IP Addess: 10.1.1.25 Port: 9100 Upto date firmware.

HTML Webform sends data to cgi script which produces one or more PDF docs, the same script then sends qualifying PDF docs to someone by email as required.

At the mo i am manually printing out a number of these docs as required when needed.

I would to try and automatically have these printed out for me as they are created instead. The web server is on the same LAN as the printer.

I’m working with a much smaller script here for the purpose of testing the print process which is failing and i have no idea why.

The printer spits out what is largely a blank sheet of paper, with a smiley followed by “HP Network Printer”, or ideed anything i place within the variable $PrinterName.

The PDF doc or the string of text does not get printed onto the paper.

Either it’s not getting sent to the printer in the first place or when it reaches the printer, the printer can’t do anything with it, i’m guessing.

The Web Servers Error Log records two errors detaled below, tried googling these error messages and i’ve failed to find anything that helps, the errors refer to the code on line 51 as marked which as i understand this code this is where the script actually sends either the PDF doc or the string of text to the printer for printing.

These error messages sugest that there is an error in my code on line 51 but i can’t work it out, i’m stumped “not OK” and “not ready” but the printer does print. Are the “not OK” and “not ready” messages generated because the printer is sleeping and it’s taking too long to wake up, but does wake up in time to actually print something? Do the “not OK” and “not ready” messages mean that the printer is incompatible with what i’m trying to achieve in some way?

Am i missing a module?
Do i need to add more info to the script to tell the print command where to find my printer?
Do i need to install / configure something elsewhere to get this to work?



#!/usr/bin/perl

use strict;
use CGI ':standard';
#use PDF::API2;
#use MIME::Lite;
use Net::Printer;
#use utf8;

my (
$WeekNumber,
$DocumentUID,
$CheckInDate,
$BookingReference,
$OutstandingBalance,
$TypeOfForm,
$PathToTestDocument,
$TestDocument,
$TestString,

$PrinterName,
$NetworkPrinter,
$PrintDocument
);

$WeekNumber = "41";
$DocumentUID ="1067";
$CheckInDate = "Sat 5th Oct 2019";
$BookingReference = "WTBA234C4";
$OutstandingBalance = 0;
$TypeOfForm = "Registration Document";
$PathToTestDocument = "/*****/*****";
$TestDocument = "$WeekNumber\_$DocumentUID \($CheckInDate\) $BookingReference $TypeOfForm.pdf";
$TestString = "I Will get This Perl Script to Print Something, i will!?";

$PrinterName = " HP Network Printer";

$NetworkPrinter = new Net::Printer(
       printer     =>  "$PrinterName",             # Printer's Name!
       server      =>  "10.1.1.25",                # IP Address of Printer
       port        =>  9100,                       # Printer's Port Number
       lineconvert =>  "Yes", 
       );
       
print "Content-Type:text/html

";

if ( (($OutstandingBalance <= 0) && ($TypeOfForm ne "Cancelled Booking")) && ( -e "$PathToTestDocument/$TestDocument") ) {

##### Print a PDF Document to Printer
#
$PrintDocument = $NetworkPrinter->printfile("$PathToTestDocument/$TestDocument");   # FAILS!  ##### Line 51 #####
#
##### Print a String of text to Printer
#
#$PrintDocument = $NetworkPrinter->printstring($TestString);     # FAILS!
#
print "
<html>
<body>
<br>
<center><b>Something should get printed out of the printer!?</b></center>
<br />
<center>Network Printer's Name: $PrinterName</center>
<br />
<center>Test Document Path: $PathToTestDocument</center>
<br />
<center>Test Document Name: $TestDocument</center>
<br />
<center>Test String: $TestString</center>
<br />
<center>Type Of Form: $TypeOfForm</center>
<br />
<center>Contents of VarName PrintDocument: $PrintDocument</center> <!-- Empty!? -->
</body>
</html>
";

} else {

print "
<html>
<body>
<body>
<center><b>Something went wrong!?</b></center>
<br />
<center>Network Printer's Name: $PrinterName</center>
<br />
<center>Test Document Path: $PathToTestDocument</center>
<br />
<center>Test Document Name: $TestDocument</center>
<br />
<center>Test String: $TestString</center>
<br />
<center>Type Of Form: $TypeOfForm</center>
<br />
<center>Contents of VarName PrintDocument: $PrintDocument</center>  <!-- Empty!? -->
</body>
<html>
";

};

exit;

##### Servers Error Log Entries
#
#[Wed May 01 18:36:17.636352 2019] [cgi:error] [pid 22045] [client 84.9.188.189:50537] AH01215: ERROR:Net::Printer[719]: Printer  HP Network Printer on Server 10.1.1.25 not okay: /*****/*****/cgi-bin/testprint.cgi
#[Wed May 01 18:36:17.636508 2019] [cgi:error] [pid 22045] [client 84.9.188.189:50537] AH01215:  at /*****/*****/cgi-bin/testprint.cgi line 51.: /*****/*****/cgi-bin/testprint.cgi
#[Wed May 01 18:36:17.636609 2019] [cgi:error] [pid 22045] [client 84.9.188.189:50537] AH01215: ERROR:Net::Printer[252]: Printer  HP Network Printer on 10.1.1.25 not ready!: /*****/*****/cgi-bin/testprint.cgi
#[Wed May 01 18:36:17.636668 2019] [cgi:error] [pid 22045] [client 84.9.188.189:50537] AH01215:  at /*****/*****/cgi-bin/testprint.cgi line 51.: /*****/*****/cgi-bin/testprint.cgi


You may need to post an image of your print result,
I suspect you’re not being very precise about your description of what actually is printed…

ie.
It could be important to know what is actually printed or not for the unsuccessful lines, the following is an example
For the following line

<center>Test Document Path: $PathToTestDocument</center>

I’d expect that at least the following should be printed, and if you see the following followed by empty space then obviously the $PathToTestDocument variable is not being created properly

Test Document Path:

TSU

Thank you for your reply.

I’m getting eveything i expect to get printed to the browser correctly.

I’m trying to print a dynamically created PDF document to a local network printer, it’s this that’s failing.

Printing the contents of the various variables to the browser is just reassuring to me that the data is where it should be.

The only variable that is empty is the contents of $PrintDocument but my gut feeling is that the contents are probably beyond the scope of the browser.