How to send specific forms to printer on save?

Hi out there.

I have a challenge: I made a webdatabase in Classic asp with a MySQL backend. When the user saves a form with a few variables, those variables must be sent to a printer (a Zebra GK420t label printer). I tried different solutions, but there I get the header and footer from the browser as well, and sometimes nothing at all. I want the specific variables only to be printed, AND they must be setup to fill the labels... like inserting </br> and also control font-size.
I would appreciate BIG time if anyone can come up with a solution! :-)

Best regards

Ulrich
ullenulleAsked:
Who is Participating?
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
This is not the type of thing where you can create a "Printer Friendly" page and print.  You have to go through your computers printer driver and there is not an easy way to do that through your browser on your own.

I have use what used to be called jzebra and now goes under qzindustries http://qzindustries.com/download for several years.  It used to be be free and now there is a small cost associated.  The reason has to to do with the java vulnerability discovered a few years ago and now these types of java applets need to have a certificate.  With the added expense the Developer moved his open source project from free to paid.  They still offer a free version but there will be some nasty warnings every time (from the browser).

Essentially you are using the java applet to send RAW printer commands to your printer.  If you look on page 12 of this manual for your printer https://www.zebra.com/content/dam/zebra/manuals/en-us/printer/zkdu-um-en.pdf you will see where they show how to send EPL or ZPL commands.  It looks like Y9600,N,8,1.  That is what you will need to send.

Get the driver installed on your printer, then download qzindustries.   There is documentation on this http://qzindustries.com/TutorialRawWin

Then download the sample project they have http://qzindustries.com/TutorialWebApplet and just get that work first.  

You will want to follow the process to use javascript. Notice the way it is done as below.
 function print() {
         // Send characters/raw commands to applet using "append"
         // Hint:  Carriage Return = \r, New Line = \n, Escape Double Quotes= \"
         qz.append("A37,503,0,1,2,3,N,QZ-PRINT TEST PRINT\n");

         // Send characters/raw commands to printer
         qz.print();
      }

Open in new window

You will need to build this in asp.  Either the entire thing
 

thePrintLine1 = "QZ-PRINT TEST PRINT"
thePrintLine2 = "LINE 2"
dim fnPrint
fnPrint = "<script>"
' remember to convert the double quotes to single quotes
fnPrint=fnPrint&"function print() {"
fnPrint=fnPrint&"    qz.append('A37,503,0,1,2,3,N,"&thePrintLine1&"\n');"
fnPrint=fnPrint&"    qz.append('A37,503,0,1,2,3,N,"&thePrintLine2&"\n');"
fnPrint=fnPrint&"    qz.print();"
fnPrint=fnPrint&"}"
fnPrint=fnPrint&"print()"
fnPrint=fnPrint&"</script>"

Open in new window

The reason you do it this way is because javascript runs on the client after the page loads and asp code runs on the server before the page loads.  By building the javascript code like this it will render as you hand coded it when it renders.

Your next step is just getting this type of sample to work.

I found the best way to make this work is to build a page like this that is only used for printing and access it via ajax.

That means you will have a "lable_print.asp" page or whatever you call it. Have it accept a get or post.

 

thePrintLine1 = request.form("line1")
thePrintLine2 =  request.form("line2")
dim fnPrint
fnPrint = "<script>"
' remember to convert the double quotes to single quotes
fnPrint=fnPrint&"function print() {"
fnPrint=fnPrint&"    qz.append('A37,503,0,1,2,3,N,"&thePrintLine1&"\n');"
fnPrint=fnPrint&"    qz.append('A37,503,0,1,2,3,N,"&thePrintLine2&"\n');"
fnPrint=fnPrint&"    qz.print();"
fnPrint=fnPrint&"}"
fnPrint=fnPrint&"print()"
fnPrint=fnPrint&"</script>"
response.write "something to to call back" ' optional

Open in new window

Then on your main page
<input name="Line1">
<input name="Line2">
<button class="sent_to_printer">Print</button>

$(function(){ // make sure you are loading jquery

     $('sent_to_printer').click(function(){
     var line1 = $('input [name="line1"]').val();
     var line2 = $('input [name="line2"]').val();

// It is a good idea to place the ajax page in another folder

     $.ajax({
       method: "POST",
       url: "some_folder/lable_print.asp",
       data: { line1: line1, line2: line2 }
     })
       .done(function( data ) {
         alert( "printed: " + data );
        // should read "printed: something to to call back"
       });

     });
});

Open in new window


Because there can be errors that need to be corrected along the way, try taking this in steps.  First get the sample page to work.  We know there should be no issues there.  If there is a problem, you know to check your set up.

Next try the single page asp page, then the ajax page as that has it's own set of issues.  

To check what is being sent and produced in your ajax page, use the browser console. All browsers have this feature and here is the chrome docs https://developer.chrome.com/devtools/docs/javascript-debugging.  If you go to the network tab, run the page, then find the link to your ajax page and click it.  You can see what is sent to and produced on the ajax page.
0
 
NerdsOfTechTechnology ScientistCommented:
You might be able to set your page to trusted and set trusted to low security with appropriate javascript settings so you don't get certificate errors. This of course is assuming that this is run internally.
0
 
ullenulleAuthor Commented:
Hello all.
Thank you for your responses so far. I'll dig into it in a few days.
Best regards
Ulrich
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for Scott Fell's comment #a40720730

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
ullenulleAuthor Commented:
Hi Scott.

Please accept my apology for my lack of response. I still didn't get to test your suggestion, but as soon as I do, I will let you know how it worked out. :-)

Best regards

Ulrich
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Thank you.
0
 
ullenulleAuthor Commented:
Hello everyone.

A long lasting issue has been solved, and I like to share the solution in here to help others. The soloution could be done in pure JavaScript and simply added to my pages before the </body> tag:

<script type="text/javascript" src="../r_lab/demo/js/qz-websocket.js"></script>
<script type="text/javascript">
deployQZ();
function getCertificate(callback) {
    callback("-----BEGIN CERTIFICATE-----\n" +
        ".... Looooooooooooots of certificate codes....+
        "-----END CERTIFICATE-----\n");
}
function signRequest(toSign, callback) {
    callback();
}
    function qzDonePrinting() {
        if (qz.getException()) {
            alert('Error printing:\n\n\t' + qz.getException().getLocalizedMessage());
            qz.clearException();
            return;
        }
   }	
function findPrinters() {
      qz.findPrinter('\\{bogus_printer\\}');
}
function qzDoneFinding() {
      var printers = qz.getPrinters().replace(/,/g, '\n');
}
function findPrinter(name) {
    var p = document.getElementById('printer');
    if (name) {
        p.value = name;
    }
        qz.findPrinter(p.value);
        window['qzDoneFinding'] = function() {
            var p = document.getElementById('printer');
            var printer = qz.getPrinter();
            window['qzDoneFinding'] = null;
        };
}
function printZPL() {
	qz.findPrinter('Zebra');
	qz.setEncoding(850);
    var get_proevemateriale = document.getElementById('proevemateriale').value;
	var get_projectname = document.getElementById('projectname').value;
	var get_boxname = document.getElementById('boxname').value;
	var get_proeve_date = document.getElementById('proeve_date').value;
	var get_diagnose = document.getElementById('diagnose').value;
    qz.append('^XA');
	qz.append('^FS^FO20,30^ADN36,25^FD');
	qz.append(get_proevemateriale);
	qz.append('^FS^FO20,55^ADN36,25^FD');
	qz.append(get_projectname+' - '+get_proeve_date);
	qz.append('^FS^FO20,80^ADN36,25^FD');
	qz.append(get_boxname);
	qz.append('^FS^FO20,105^ADN36,25^FD');
	qz.append(get_proeve_date);
	qz.append('^FS^FO20,130^ADN36,25^FD');
	qz.append(get_diagnose);
    qz.append('^FS');
    qz.append('^XZ');
    qz.print();
    }
</script>

Open in new window



function printZPL()  at the end of the codes are the one to edit in order to make the printed labels look as planned. Data is being pulled from forms on the webpage. QZ provided lots of support to make it Work. It all appears so easy when it finally Works. :-)

Best regards

Ulrich
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
>It all appears so easy when it finally Works.

That is correct. Tres is very helpful with his product.

It looks like what you are doing is sending data probably via asp/vb to different html elements like

<div id="proevemateriale">some text here</div>

Open in new window


Your line 40 above grabs the text "some text here" then line 47 outputs that text.  Line 46 and 48 are the RAW codes that adjust things like fonts and spacing.

What I have done instead of sending data to an element is just place it directly to a js variable or directly to the print command.\

Instead of your line 47 above
qz.append(get_proevemateriale);

Open in new window

I have simply used
<%
' ASP CODE
proevemateriale = "some text"
%>

<!-- code inside <script> tag for js -->
qz.append(<%=proevemateriale%>);

Open in new window


Then  no need to create html elements (unless of course you are also sending this to screen)
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.