Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got on desk XP-C300H - raster doesnt works #1361

Open
ArniSamano opened this issue Dec 8, 2024 · 0 comments
Open

Got on desk XP-C300H - raster doesnt works #1361

ArniSamano opened this issue Dec 8, 2024 · 0 comments

Comments

@ArniSamano
Copy link

Printer spec seems to support raster, but it doesnt work. Any pointers?

`<?php

require_once 'modules/POS/libraries/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
use Mike42\Escpos\Printer;
use Mike42\Escpos\EscposImage;

class POS_GenerateInvoicePDF_View extends Vtiger_Index_View {

public function process(App\Request $request)
{
    $filePath = $this->generatePDFFile($request);

    $currentUser = Users_Record_Model::getCurrentUserModel();
    $printer_ip = $currentUser->kitchen_printer;

    if (empty($printer_ip)) {
        echo json_encode(["print_status" => false, "message" => "Please finish your printer configuration first."]);
        die;
    }

    $position = strpos($printer_ip, ":");
    $printerIP = trim(substr($printer_ip, 0, $position));
    $printerPort = trim(substr($printer_ip, $position + 1));

    try {
        // Create a network connector to the printer
        $connector = new NetworkPrintConnector($printerIP, $printerPort);

        // Create a new Printer object
        $printer = new Printer($connector);

        // Convert PDF to PNG using Imagick
        $imagick = new Imagick();
        $imagick->setResolution(203, 203); // Set resolution suitable for receipt printers
        $imagick->readImage($filePath);
        $imagick->setImageFormat('png');

        // Trim and resize the image to fit within the printer width (576 pixels)
        $imagick->trimImage(0);
        $imagick->resizeImage(576, 0, Imagick::FILTER_LANCZOS, 1);

        // Save the processed image to a temporary file
        $pngFilePath = 'storage/invoice.png';
        $imagick->writeImage($pngFilePath);
        $imagick->clear();
        $imagick->destroy();

        // Load the PNG image using EscposImage
        $image = EscposImage::load($pngFilePath);

        // Print the image using bitImage
        $printer->raster($image, Printer::IMG_DEFAULT);

        $printer->cut();
        $printer->close();

    } catch (Exception $e) {
        echo json_encode(["print_status" => false, "message" => $e->getMessage()]);
        die;
    }

    echo json_encode(["print_status" => true]);
    die;
}

/** Align with PDF template ID */
public function getTemplateId($templates)
{
    $template_id = "38";

    foreach ($templates as $key => $template) {
        $template_id = $template->has('pdfid') ? $template->get('pdfid') : "";
    }

    return $template_id;
}

/**
 * Generate PDF File
 */
public function generatePDFFile($request)
{
    $pdfModuleName = 'FInvoice';
    $recordId = $request->isEmpty('record', true) ? null : $request->getInteger('record');
    if (empty($recordId)) {
        return false;
    }

    $handlerClass = Vtiger_Loader::getComponentClassName('Model', 'PDF', $pdfModuleName);
    $pdfModel = new $handlerClass();
    $templates = $pdfModel->getActiveTemplatesForRecord($recordId, 'Detail', $pdfModuleName);
    $template_id = $this->getTemplateId($templates);

    $eventHandler = new App\EventHandler();
    $eventHandler->setModuleName($pdfModuleName);
    $eventHandler->setParams([
        'records' => $recordId,
        'templates' => $template_id,
        'viewInstance' => $this,
        'pdfModel' => $pdfModel,
    ]);
    $eventHandler->trigger('PdfGenerateInit');
    $recordId = $eventHandler->getParam('records');

    $pdfFiles = [];
    $pdf = \App\Pdf\Pdf::getInstanceByTemplateId($template_id);
    $template = $pdf->getTemplate();
    $filePath = 'storage/invoice.pdf';

    $template->setVariable('recordId', $recordId);

    $pdf->loadTemplateData();
    $eventHandler->addParams('pdf', $pdf);
    $eventHandler->trigger('PdfGenerate');

    $pdf->output($filePath, 'S');
    return $filePath;
}

}
?>
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant