0

我有一个 PHP Web 应用程序,可以选择生成发票。问题是我需要在草稿模式下使用点阵打印机打印发票。我试过了,但它的打印效果就像普通打印机一样,字体更大。只是它以与页面中相同的格式打印。

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

4

1 回答 1

0

好吧,我认为您需要以原始模式打印。

我没有声称我已经完全解决了这个问题,但这是我的尝试。

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

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

例如,您可以创建一个 135 * 66 字符的数组,其中 135 是通常的页面宽度(以字符为单位),而 66 是通常的打印行数(在具有 8-1/2" x 12" 纸张的 24 针点阵打印机上)尺寸)。您应该小心用字符而不是字符串填充数组,谎言如下:

    $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 );
}

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

我使用的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,那么每个字符在纸上的空间就会减少。这意味着在这种情况下,您不能依靠一个空格字符将数据分布在整个页面上。

如果有人认为他们可以为此做出贡献,他们将受到欢迎。

于 2014-04-29T13:26:50.400 回答