在草稿模式下打印到点阵打印机- PHP


Printing to a dot matrix printer in draft mode - PHP

我有一个PHP web应用程序,其中一个生成发票的选项。问题是我需要在草稿模式下使用点阵打印机打印发票。我试过了,但它打印起来和普通打印机一样,字体更大。简单地说,它在页面中以相同的格式打印。

我需要像收据一样打印。我需要用PHP格式化任何东西吗?

嗯,我认为你需要以原始模式打印。

我不能说我已经完全解决了这个问题,但以下是我的尝试。

首先,在点阵打印机连接的系统上应该有一个"通用/仅文本"驱动程序,可能在端口LPT1中。

其次,你应该发送ESCP命令到打印机,而不是使用php的内置进程来打印。

例如,您可以创建一个135 * 66个字符的数组,其中135是通常的页面宽度(以字符为单位),66是通常的打印行数(在24针点阵打印机上,纸张尺寸为8-1/2"x 12")。你应该小心用字符填充数组,而不是字符串,就像这样:
    $GRAND_ARRAY = array();
    $maxCharactersPerLine = 135;
    $maxLines = 66;
    //initialize the array with spaces and the last column of 'r'n
    for($i=0; $i<$maxLines; $i++){
        $pos = $i*$maxCharactersPerLine + ($maxCharactersPerLine-1) + $i + 1;
        $value = "'r'n";
        $this->printChars($GRAND_ARRAY, $pos, $value);
    }
    //make an array that fits every one character of the page
    //plus a column for the "next line" character
    $totalCells = $maxCharactersPerLine * $maxLines + $maxLines - 1;
    //what you want to print
    $value = "name:"; //here is a string with 5 characters
    //where you want to print it on the page
    //$line = 2
    //$column = 5
    //consider that array starts at 0 and not 1
    //$pos = ($column - 1) * $maxCharactersPerLine + $line + $column - 2;
    $pos = 275; //position is 5th character in line 2
    //do not just $GRAND_ARRAY[$pos] = $value because you should
    //make sure that every array cell has only one character
    $this->printChars($GRAND_ARRAY, $pos, $value, 1);
    // also included a flag if you want to print only the $limit characters of $newstring
    public function printChars(&$mainArray, $start, $newString, $limit=0){
        $j = $start;
        $charsArray = $this->mb_str_split($newString);
        //$len = mb_strlen($newString, "UTF-8"); //can't remember why not this
        $len = count($charsArray);
        for($i=0; $i<$len; $i++){
            if($limit > 0  &&  $limit < $i){
                return;
            }else{
                $mainArray[$j] = $charsArray[$i];
                $j = $j + 1;
            }
        }
    }

//this I found here on s.o. i think.
public function mb_str_split( $string ) {
    # Split at all position not after the start: ^
    # and not before the end: $
    return preg_split('/(?<!^)(?!$)/u', $string );
}

创建数组后,我使用了qz-print插件(https://code.google.com/p/jzebra/),其中包含javascript函数和java applet,以便将该数组发送到客户端的"通用/仅文本"点阵打印机。

我使用的javascript函数是:
//just before that, I turn the array into string str.
function printESCP2(str, type, quality, pitch, line_spacing, topOffset, commands) {
    if (notReady()) { return; }
    qz.appendHex("x1Bx40"); //ESC @ for reset settings
    var j = commands.length;
    for(var k=0;k<j;k++){
        console.log(':'+commands[k]);
        qz.appendHex(commands[k]);
    }
    if(topOffset){
        if(topOffset > 0){
            alert("topOffset: "+topOffset);
            for(var k=0; k<topOffset; k++){
                qz.appendHex("x0D"); //CR for carriage return
                qz.appendHex("x0A"); //LF for line feed
            }
        }
    }
    if(quality == <?php echo SettingsPrinter::FONT_QUALITY_LQ; ?>){
        alert("font quality: LQ");
        qz.appendHex("x1Bx78x31"); // ESC x 1 for LQ (31=1)
    }else{
        alert("font quality: draft");
        qz.appendHex("x1Bx78x30"); // ESC x 0 for Draft (30=0)
    }
    if(line_spacing == <?php echo SettingsPrinter::PRINTER_LINE_SPACING_NARROW; ?>){
        //alert("line spacing: narrow (1/8)");
        qz.appendHex("x1Bx30"); //ESC 0 for line spacing minimum (1/8)
    }else{
        //alert("line spacing: normal (1/6)");
        qz.appendHex("x1Bx32"); //ESC 2 for line spacing normal (1/6)
    }
    switch(pitch){
        case '<?php echo SettingsPrinter::FONT_PITCH_10; ?>':
            alert("pitch: 10");
            qz.appendHex("x1Bx50"); //ESC P for pitch 10 cpi
            break;
        case '<?php echo SettingsPrinter::FONT_PITCH_12; ?>':
            alert("pitch: 12");
            qz.appendHex("x1Bx4D"); //ESC M for pitch 12 cpi
            break;
        case '<?php echo SettingsPrinter::FONT_PITCH_15; ?>':
            alert("pitch: 15");
            qz.appendHex("x1Bx67"); // ESC g     for pitch 15 cpi
            break;
        case '<?php echo SettingsPrinter::FONT_PITCH_17; ?>':
            alert("pitch: 17");
            qz.appendHex("x1Bx50"); //ESC P for pitch 10 cpi
            qz.appendHex("x0F"); // SI for condensed resulting in pitch 17 cpi
            break;
        case '<?php echo SettingsPrinter::FONT_PITCH_20; ?>':
            alert("pitch: 20");
            qz.appendHex("x1Bx4D"); //ESC M for pitch 12 cpi
            qz.appendHex("x0F"); // SI for condensed resulting in pitch 20 cpi
            break;
        default:
            alert("pitch unknown -- not set");
            break;
    }
    qz.append(str);
    qz.appendHex("x0D"); //CR for carriage return
    qz.appendHex("x1Bx40"); //ESC @ for reset settings
    window["qzDoneAppending"] = function() {        
        // Tell the apple to print.
    qz.print();     
        // Remove any reference to this function
    window['qzDoneAppending'] = null;
    };
}

实际上可以创建要打印的字符数组作为二维数组,并提交'r'n的最后一列,因为我认为这不是所有点阵打印机都能接受的。在这种情况下,我们应该做

//for each line of the array
qz.append($line); //line-1 turned into string, line-2  etc
qz.appendHex("x0D"); //CR for carriage return after each line

我遇到的一个问题是,如果用户选择17CPI,那么每个字符在纸上的空间就会减少。这意味着在这种情况下,您不能依靠一个空格字符来将数据分散到整个页面。

如果有人认为他们可以贡献,我们非常欢迎。