akwe-xavante has asked for the wisdom of the Perl Monks concerning the following question:

Hoping someone can help by pointing me in the right direction

OS: openSUSE leap 15

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?

#!/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\) $BookingRe +ference $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 Pri +nter port => 9100, # Printer's Port Nu +mber lineconvert => "Yes", ); print "Content-Type:text/html\n\n"; if ( (($OutstandingBalance <= 0) && ($TypeOfForm ne "Cancelled Booking +")) && ( -e "$PathToTestDocument/$TestDocument") ) { ##### Print a PDF Document to Printer # ##### Line 51 ##### $PrintDocument = $NetworkPrinter->printfile("$Pat +hToTestDocument/$TestDocument"); # FAILS! ##### Line 51 ##### # ##### Print a String of text to Printer # #$PrintDocument = $NetworkPrinter->printstring($TestString); # FAI +LS! (If uncommented) # print " <html> <body> <br> <center><b>Something should get printed out of the printer!?</b></cent +er> <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;

##### Web Servers Error Log Entries

Wed May 01 18:36:17.636352 2019 cgi:error pid 22045 client ***.***.***.***:50537 AH01215: ERROR:Net::Printer719: 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 ***.***.***.***: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 ***.***.***.***:50537 AH01215: ERROR:Net::Printer252: 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 ***.***.***.***:50537 AH01215: at /*****/*****/cgi-bin/testprint.cgi line 51.: /*****/*****/cgi-bin/testprint.cgi

Replies are listed 'Best First'.
Re: NET::Printer Module not printing to local printer
by holli (Abbot) on May 01, 2019 at 21:22 UTC
    Please note that this module only talks to print servers that speak BSD. It will not talk to printers using SMB, SysV, or IPP unless they are set up as BSD printers. CUPS users will need to set up cups-lpd to provide legacy access.
    Are these conditions satisfied? Does it work when you use lp to send a file manually aaand is lp installed on the webserver?


    holli

    You can lead your users to water, but alas, you cannot drown them.

      Thank you holli for your reply

      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.

      Am i a module missing and or do i need to add info the the script so that the print command knows where to find the printer?

      Do i need to install, setup and or configure something elsewhere?

      Changing the $PrinterName = " HP Network Printer" to $PrinterName = "HP_Color_LaserJet_CP2025n" doesn't fix the problem

      is lp installed on the webserver? Tried googling this one and got nowhere, can i ask what's meant by this with a little more info so i can explore further

        running the command lp -d HP_Color_LaserJet_CP2025n "Test Sheet.txt" request id is HP_Color_LaserJet_CP2025n-8 (1 file(s))
        On which machine did you run that? I'm thinking, if the module uses lp and lp is not installed on the webserver (and why should it, it's a webserver) that could be why it doesn't work. And so if you ran that command from your dev machine it doesn't tell us much.


        holli

        You can lead your users to water, but alas, you cannot drown them.
Re: NET::Printer Module not printing to local printer
by bliako (Abbot) on May 02, 2019 at 12:50 UTC

    Net::Printer opens a socket connection to the print server/printer. Does your webserver allow that??? (EDIT: obviously it does because you got something printed out but perhaps the connection is killed?)

    Net::Printer's documentation does not mention the debug option to new(). Add debug => 'Yes' to new Net::Printer() and see if any messages can help you.

    I would also move print "Content-Type:text/html\n\n"; before creating the printer object. I was bitten by printing to std* before printing content-type many times. Move it as far up as it can go.

    Also, I would convert the cgi script you posted to a normal command-line perl script and use that to test (rather than testing with lp ...).

    Replacing Net::Printer with running a system command from a cgi script could become a problem. Because the webserver can be configured not to allow that, at some point in the future. Or to run it on very low priority. Additionally, there are the usual huge security risks that come with allowing a web service taking user input to shell out. The usual response of course being "we are all friends in our intranet". And I stop here.

    bw, bliako