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

在草稿模式下打印到点阵打印机-PHP,php,Php,我有一个PHP web应用程序,可以选择生成发票。问题是我需要在草稿模式下使用点阵打印机打印发票。我试过了,但它像普通打印机一样打印,字体更大。它只是以页面中相同的格式打印 我需要像收据一样打印。我需要用PHP格式化任何东西吗?嗯,我想你需要以原始模式打印 我并不是说我已经完全解决了这个问题,但这里是我的尝试 首先,点阵打印机连接的系统上应该有一个“通用/纯文本”驱动程序,可能在端口LPT1中 其次,您应该向打印机发送ESCP命令,而不是使用php的内置进程进行打印 例如,您可以创建一个135*

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


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

嗯,我想你需要以原始模式打印

我并不是说我已经完全解决了这个问题,但这里是我的尝试

首先,点阵打印机连接的系统上应该有一个“通用/纯文本”驱动程序,可能在端口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 );
}
我遇到的一个问题是,如果用户选择17CPI,那么每个字符在纸上的空间都会减少。这意味着在这种情况下,不能依靠一个空格字符将数据分布到整个页面


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

我想您正在寻找CSW您使用什么方法进行打印?它是基于网络的应用程序吗?是从浏览器还是从服务器打印?你能给我们看看你已经有的代码吗?
//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;
    };
}
//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