有什么方法可以使用PHP打印收据吗?

时间:2018-08-08 11:22:12

标签: php printing text-alignment php-printer

我正在尝试这种方式,但是无法正确对齐。我试图在局域网中使用网络打印机“ HP LaserJet Mono PCLmS类驱动程序”进行打印。打印正常,但对齐不完美。

$text1 = $this->string_align("Example item #1", "4.00");
$text2 = $this->string_align("Another thing tea", "3.50");
$text3 = $this->string_align("Something else", "1.00");
$text4 = $this->string_align("A final item", "4.45");
$text5 = $this->string_align("Subtotal", "12.95");
$text6 = $this->string_align("A local tax", "1.30");
$text7 = $this->string_align("Grand Total", "14.25");

$handle = printer_open("HP LaserJet Mono PCLmS Class Driver");
printer_start_doc($handle, "Recept Document");
printer_start_page($handle);
printer_draw_bmp($handle, "D:\\logo.bmp", 1, 1);
$font = printer_create_font("Arial", 50, 70, 300, false, false, false, 0);
printer_select_font($handle, $font);
printer_draw_text($handle, $text1, 100, 400);
printer_draw_text($handle, $text2, 100, 500);
printer_draw_text($handle, $text3, 100, 600);
printer_draw_text($handle, $text4, 100, 700);
printer_draw_text($handle, $text5, 100, 800);
printer_draw_text($handle, $text6, 100, 900);
printer_draw_text($handle, $text7, 100, 1000);
printer_delete_font($font);
printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);
exit;

function string_align($name = '', $price = '', $dollarSign = false){
$rightCols = 10;
$leftCols = 38;
if ($dollarSign) {
$leftCols = $leftCols / 2 - $rightCols / 2;
}
$left = str_pad($name, $leftCols) ;

$sign = ($dollarSign ? '$ ' : '');
$right = str_pad($sign . $price, $rightCols, ' ', STR_PAD_LEFT);
return "$left$right\n";
}

1 个答案:

答案 0 :(得分:2)

尝试使用consolas字体。

这是一种统一宽度的字体,应该在您的打印页面上正确呈现。