1

I have a php website which generate invoice in the form of html tables, I need to print these html tables to dot-matrix printer. I have tried to print webpage directly with browser print option, but it seems the printer treat it as image because it prints characters dot-by-dot instead of complete characters in a single pass like it would an ascii text file, which result in blurred characters.

Is there any way to make the printer treat it as text file? Or is there any way to convert html page to text file without losing the position styling(spacing, margins, etc)? Or maybe there's an alternative approach I could use? One thing to note is I can't use text-based browser to do this as it will be used by clients.

The invoice is a html table with small logo at the top-left, title, and description as thead, and simple table cell with borders as tbody.

I use Epson LX300+II printer.

raven
  • 123
  • 1
  • 2
  • 11

5 Answers5

2
<?php
 // Download printer driver for dot matrix printer ex. 1979 Dot Matrix      Regular or Consola

?>
<html>
<head>
<title>PHP to Dot Matrix Printer</title>

<style>
@font-face { font-family: kitfont; src: url('1979 Dot Matrix Regular.TTF'); } 

.customFont { /*  <div class="customFont" /> */
font-style: kitfont;
font-size:10;
}
#mainDiv {
height: 324px; /* height of receipt 4.5 inches*/
width: 618px;  /* weight of receipt 8.6 inches*/
position:relative; /* positioned relative to its normal position */
}
#cqm { /*  <img id="cqm" /> */
top: 10px; /* top is distance from top (x axis)*/
left: 105px; /* left is distance from left (y axis)*/
position:absolute; /* position absolute based on "top" and "left"    parameters x and y  */
}

#or_mto { 
position: absolute;
left: 0px;
top: 0px;
z-index: -1; /*image */
}

    #arpno {
top: 80px;
left: 10px;
position:absolute;
}
#payee {
top: 80px;
left: 200px;
position:absolute;
}
#credit {
top: 80px;
right: 30px; /*   distance from right */
position:absolute;
}
#paydate {
top: 57px;
right: 120px;
position:absolute;
}
 </style>

</head>
<body>
<?php
//sample data

$arpno   = 1234567;
$payee   = "Juan dela Cruz";
$credit  = 10000;
$paydate = "Dec. 6, 2015" ;


?>
<div id="mainDiv"> <!--  invisible space -->
<div id="cqm" class="customFont">ABC TRADING</div>
<div id="arpno" class="customFont"><?php echo $arpno; ?></div>
<div id="payee" class="customFont"><?php echo $payee; ?></div>
<div id="credit" class="customFont"><?php echo $credit; ?></div>
<div id="paydate" class="customFont"><?php echo $paydate; ?></div>
<img id="or_mto" src="or_mto.jpg" /> <!---- sample for logo  ---->
</div>

</body>
</html>
bughaw7
  • 21
  • 2
1

For anyone looking to print directly from browser to printer, and assuming you need to print text only, I found the answer in this post how-to-print-page-in-plain-text-format-for-matrix-dot-printer

Github for the application is here.

A simple tutorial can be found here.

Basically you install QZ Tray in your client machine and you add the script provided by them in your html.

Works OK in Windows 10 with a OKI 320 Turbo Dot Matrix Printer, but you have to change the driver for the printer to Generic / Text Only.

Then in javascript you just do something like this:

try {
    
    await qz.websocket.connect();
    console.log("connected")

    const printer = await qz.printers.find("OKI");
    console.log(`Printer ${printer} found !`);

    const config = qz.configs.create(printer);
    const data = [
        'Some Data \n',
        'More Data   and More Data  \n'
    ]

    qz.print(config, data);

} catch(e) { console.log(e) }

Works like a charm!

0

We used to do this using a Java Applet that could write to the LPT port after due permissions on the browser and Java security.

However support for this has been phased out by all browsers. We are running it right now by using older versions of the browsers.

One way we are exploring is the possibility of a java service running on a port on the local PC and calling this service from the browser window.

0

an option to solve this problem is to download the TCPDF library https://tcpdf.org/docs/srcdoc/ and pass the reports to pdf but you have to download some dotmatrix type font and using the link http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf, convert fonts to the format that TCPDF uses.

0

I had almost same requirement like this, i didn't get a perfect solution, so I used an alternative solution,

  • converted the html to text with the help of "w3m"
  • send this text to a python script running on a client machine through socket communication
  • through the python script(used pyusb) i sent this text directly to the printer via USB and printed.

In this solution, can't get all the solution in print.

Sunil Jose
  • 315
  • 2
  • 16