mike42 / escpos-php

PHP library for printing to ESC/POS-compatible thermal and impact printers
Other
2.54k stars 862 forks source link

Bill Continuation Header Issue #835

Open gitsampath opened 4 years ago

gitsampath commented 4 years ago

Hi Mike,

I have an issue with my bill printing. As you can see from the attached image, the first bill printed after switch on the printer prints the bill header properly. But from second bill on wards the bill header prints in partial. This seems to be an issue with top margin of the page. I'm unable to fix this issue with escpos printer settings.

Please kindly help me to resolve this issue.

Thank you very much! Sam.

Following is the code which I use to print the erroneous bill.

               if ($billPrinterName != '') {

            $connector = new CupsPrintConnector($billPrinterName);

            /* Start the printer */
            $printer = new Printer($connector);

            try {

                $company = $this->company_information_model->getAll();

                $licenseProperties = ioncube_license_properties();

                $companyName = '';
                $companyAddress = '';
                $telephone = '';
                if($company != null){

                    if ($licenseProperties && sizeof($licenseProperties) > 0) {
                        if (array_key_exists("licensedTo", $licenseProperties)) {
                            $companyName = $licenseProperties['licensedTo']['value'];
                        } else {
                            $companyName = $company[0]->company_name;
                        }
                    } else {
                        $companyName = $company[0]->company_name;
                    }     

                    $companyAddress = $company[0]->address;
                    $telephone = $company[0]->primary_telephone_number;
                }

                /* Name of shop */
                $printer -> initialize();
                $printer -> setJustification(Printer::JUSTIFY_CENTER);
                $printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT);
                $printer -> text($companyName . "\n");
                $printer -> selectPrintMode();
                $printer -> text($companyAddress . "\n");
                $printer -> text("TP : " . $telephone . "\n");
                $printer -> feed();

                /* Title of receipt */
                $printer -> setEmphasis(true);

                $printer -> text("BULK CREDIT INVOICE PAYMENT\n");

                $printer -> setEmphasis(false);

                $printer -> setJustification(Printer::JUSTIFY_CENTER);
                $printer -> text(".................................\n");

                $date = date("F j, Y, g:i a");
                $printer -> text($date . "\n");
                $printer -> text($warehouseName . "\n");
                $printer -> feed(1);

                if ($customerName != '') {
                    $printer -> text(new invoiceDataRowInBillPrint("Customer", $customerName));
                }

                $printer -> text(new invoiceDataRowInBillPrint("Staff", $salesRepName));

                /* Information for the receipt */
                $printer -> setJustification(Printer::JUSTIFY_CENTER);
                $printer -> text(".................................\n");

                if ($paymentType == "Cash Payment") {
                    $printer -> text(new invoiceDataRowInBillPrint("Cash Payment", number_format($amount, 2)));
                } else if ($paymentType == "Cheque Payment") {
                    $printer -> text(new invoiceDataRowInBillPrint("Cheque Payment", number_format($amount, 2)));
                } else if ($paymentType == "Credit Card Payment") {
                    $printer -> text(new invoiceDataRowInBillPrint("Credit Card Payment", number_format($amount, 2)));
                }

                $printer -> text(new invoiceDataRowInBillPrint("Balance Credit Payment", number_format($balanceCreditPayment, 2)));

                $printer -> feed(1);

                if ($chequeNumber != '' && $chequeRealizeDate != '') {
                    $printer -> text(new invoiceDataRowInBillPrint("Cheque Number", $chequeNumber));
                    $printer -> text(new invoiceDataRowInBillPrint("Realize Date", $chequeRealizeDate));
                }

                if ($paidInvoices != '') {
                    $printer -> text(new invoiceDataRowInBillPrint("Closed Invoices", $paidInvoices));
                }

                $printer -> selectPrintMode();

                $printer -> setJustification(Printer::JUSTIFY_CENTER);
                $printer -> text(".................................\n");

                /* Footer */
                $printer->setJustification(Printer::JUSTIFY_CENTER);
                $printer->text("Software By Artifectx Solutions\n");
                $printer->text("Web : www.artifectx.com / TP : 0779738068\n");
                $printer -> selectPrintMode();

                /* Cut the receipt and open the cash drawer */
                $printer->cut();
                //$printer->pulse();
                $printer->close();
            } catch (Exception $e) {
                /*
                 * loadPdf() throws exceptions if files or not found, or you don't have the
                 * imagick extension to read PDF's
                 */
                echo $e->getMessage();
            }
        }

IMG_20191113_000956

mike42 commented 4 years ago

Which model of printer you are using?

The cutter is generally located after the thermal head, so some spacing is often unavoidable. Eg. I do not know a way to avoid 2 lines of space before each print on a TM-T20II (corrected see below)

You usually expect your printer to feed the paper before a cut, so that the last line of text is just past the cutter when it slices the paper (eg. TM-T20 user guide contains a diagram on page 18).

The two tricks to know are:

mike42 commented 4 years ago

Posting again to correct to my last comment. I found #768, where I investigated this previously, and noted that ESC i cuts with no line feed on my printer.

To send a command that isn't implemented in the library, write it directly to the connector, so in this case use $connector -> write("\x1bi"); (sends ESC i) instead of $printer -> cut();

Using this code:

<?php
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\CapabilityProfile;
use Mike42\Escpos\Printer;
$profile = CapabilityProfile::load("default");
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector, $profile);

for($i = 1; $i <= 3; $i++) {
  $printer -> text("Line $i\n");
  $connector -> write("\x1bi");
}
$printer -> text("Last line..\n");
$printer -> cut();
$printer -> close();

I printed this receipt on an Epson TM-T20II.

2019-12-cut-2

Provided that this command is available on your printer, you can probably get it to cut in the right spot with some trial and error.

Best of luck!

gitsampath commented 4 years ago

Hi Mike,

Thank you very much for your detailed explanation. I will check this and update the results.

Sam.

aliibrahimroshan commented 4 years ago

HI Mike

I have tried the above command on my Epson TM220 but its printing the same as you mentioned above . its cutting with no feed but its cutting first and then printing :) . As you said do some trial and error i did alot and wasted too much paper :) but still cant get the result pls assist Thanks.

        $profile = CapabilityProfile::load("default");
        $connector = new WP('EPSON DRYCLEAN');
        $printer = new P($connector, $profile);

       // $connector -> write("Hello\n\x1biWorld\n\x1bi");

        $connector -> write("Line One");
        $connector -> write("\x1bi");

        $connector -> write("Line 2");
        $connector -> write("\x1bi");

        $connector -> write("Line 3");
        $connector -> write("\x1bi");

       // $printer -> pulse();
        $printer -> cut();

        $printer -> close();
marcellocello commented 3 years ago

Posting again to correct to my last comment. I found #768, where I investigated this previously, and noted that ESC i cuts with no line feed on my printer.

To send a command that isn't implemented in the library, write it directly to the connector, so in this case use $connector -> write("\x1bi"); (sends ESC i) instead of $printer -> cut();

Using this code:

<?php
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\CapabilityProfile;
use Mike42\Escpos\Printer;
$profile = CapabilityProfile::load("default");
$connector = new FilePrintConnector("php://stdout");
$printer = new Printer($connector, $profile);

for($i = 1; $i <= 3; $i++) {
  $printer -> text("Line $i\n");
  $connector -> write("\x1bi");
}
$printer -> text("Last line..\n");
$printer -> cut();
$printer -> close();

I printed this receipt on an Epson TM-T20II.

2019-12-cut-2

Provided that this command is available on your printer, you can probably get it to cut in the right spot with some trial and error.

Best of luck!

thanks @mike42 , this is the solution to my problem. but I have another problem, when I print an example text

$printer -> text ("Visitor \ n");

then the result will be like this. image Is it possible to add a little black rectangle at the top and bottom of the text to make it look like this? image