A mi backend envio un json como este:
[
{
"br": 2,
"selectPrintMode": 0,
"setColor": 0,
"setDoubleStrike": false,
"setEmphasis": true,
"setFont": 0,
"setJustification": 1,
"setLineSpacing": 1,
"setPrintLeftMargin": 0,
"setPrintWidth": 576,
"setReverseColors": false,
"setTextSize": [
1,
1
],
"setUnderline": 0,
"text": "*** Vales ***"
},
{
"br": 1,
"selectPrintMode": 0,
"setColor": 0,
"setDoubleStrike": false,
"setEmphasis": true,
"setFont": 0,
"setJustification": 1,
"setLineSpacing": 1,
"setPrintLeftMargin": 0,
"setPrintWidth": 576,
"setReverseColors": false,
"setTextSize": [
1,
1
],
"setUnderline": 0,
"text": "Concepto Observacion Monto"
},
{
"br": 1,
"selectPrintMode": 0,
"setColor": 0,
"setDoubleStrike": false,
"setEmphasis": false,
"setFont": 0,
"setJustification": 0,
"setLineSpacing": 1,
"setPrintLeftMargin": 0,
"setPrintWidth": 576,
"setReverseColors": false,
"setTextSize": [
1,
1
],
"setUnderline": 0,
"text": "restrooms Esto tan solo es una "
},
{
"br": 1,
"selectPrintMode": 0,
"setColor": 0,
"setDoubleStrike": false,
"setEmphasis": false,
"setFont": 0,
"setJustification": 0,
"setLineSpacing": 1,
"setPrintLeftMargin": 0,
"setPrintWidth": 576,
"setReverseColors": false,
"setTextSize": [
1,
1
],
"setUnderline": 0,
"text": " prueba "
},
{
"br": 1,
"selectPrintMode": 0,
"setColor": 0,
"setDoubleStrike": false,
"setEmphasis": false,
"setFont": 0,
"setJustification": 0,
"setLineSpacing": 1,
"setPrintLeftMargin": 0,
"setPrintWidth": 576,
"setReverseColors": false,
"setTextSize": [
1,
1
],
"setUnderline": 0,
"text": " "
},
{
"br": 1,
"selectPrintMode": 0,
"setColor": 0,
"setDoubleStrike": false,
"setEmphasis": false,
"setFont": 0,
"setJustification": 0,
"setLineSpacing": 1,
"setPrintLeftMargin": 0,
"setPrintWidth": 576,
"setReverseColors": false,
"setTextSize": [
1,
1
],
"setUnderline": 0,
"text": " "
},
{
"br": 1,
"selectPrintMode": 0,
"setColor": 0,
"setDoubleStrike": false,
"setEmphasis": false,
"setFont": 0,
"setJustification": 0,
"setLineSpacing": 1,
"setPrintLeftMargin": 0,
"setPrintWidth": 576,
"setReverseColors": false,
"setTextSize": [
1,
1
],
"setUnderline": 0,
"text": " 9999.00"
}
]
El problema es que cuando lo recibo en el backend obtengo las llaves text sin los espacios antes y despues del texto (como si estuviese haciendo un trim). ¿Alguien sabe como puedo evitar eso?
[
{
"br": "2",
"selectPrintMode": "0",
"setColor": "0",
"setDoubleStrike": "false",
"setEmphasis": "true",
"setFont": "0",
"setJustification": "1",
"setLineSpacing": "1",
"setPrintLeftMargin": "0",
"setPrintWidth": "576",
"setReverseColors": "false",
"setTextSize": [
"1",
"1"
],
"setUnderline": "0",
"text": "*** Vales ***"
},
{
"br": "1",
"selectPrintMode": "0",
"setColor": "0",
"setDoubleStrike": "false",
"setEmphasis": "true",
"setFont": "0",
"setJustification": "1",
"setLineSpacing": "1",
"setPrintLeftMargin": "0",
"setPrintWidth": "576",
"setReverseColors": "false",
"setTextSize": [
"1",
"1"
],
"setUnderline": "0",
"text": "Concepto Observacion Monto"
},
{
"br": "1",
"selectPrintMode": "0",
"setColor": "0",
"setDoubleStrike": "false",
"setEmphasis": "false",
"setFont": "0",
"setJustification": "0",
"setLineSpacing": "1",
"setPrintLeftMargin": "0",
"setPrintWidth": "576",
"setReverseColors": "false",
"setTextSize": [
"1",
"1"
],
"setUnderline": "0",
"text": "restrooms Esto tan solo es una"
},
{
"br": "1",
"selectPrintMode": "0",
"setColor": "0",
"setDoubleStrike": "false",
"setEmphasis": "false",
"setFont": "0",
"setJustification": "0",
"setLineSpacing": "1",
"setPrintLeftMargin": "0",
"setPrintWidth": "576",
"setReverseColors": "false",
"setTextSize": [
"1",
"1"
],
"setUnderline": "0",
"text": "prueba"
},
{
"br": "1",
"selectPrintMode": "0",
"setColor": "0",
"setDoubleStrike": "false",
"setEmphasis": "false",
"setFont": "0",
"setJustification": "0",
"setLineSpacing": "1",
"setPrintLeftMargin": "0",
"setPrintWidth": "576",
"setReverseColors": "false",
"setTextSize": [
"1",
"1"
],
"setUnderline": "0",
"text": null
},
{
"br": "1",
"selectPrintMode": "0",
"setColor": "0",
"setDoubleStrike": "false",
"setEmphasis": "false",
"setFont": "0",
"setJustification": "0",
"setLineSpacing": "1",
"setPrintLeftMargin": "0",
"setPrintWidth": "576",
"setReverseColors": "false",
"setTextSize": [
"1",
"1"
],
"setUnderline": "0",
"text": null
},
{
"br": "1",
"selectPrintMode": "0",
"setColor": "0",
"setDoubleStrike": "false",
"setEmphasis": "false",
"setFont": "0",
"setJustification": "0",
"setLineSpacing": "1",
"setPrintLeftMargin": "0",
"setPrintWidth": "576",
"setReverseColors": "false",
"setTextSize": [
"1",
"1"
],
"setUnderline": "0",
"text": "9999.00"
}
]
Actualización 1:
El backend lo manejo de esta manera. ¿Por que necesito los espacios? Es porque necesito imprimir en una impresora térmica la cual estoy ordenando los datos como si fuesen una tabla.
public function imprimir(Request $req) {
try{
$profile = CapabilityProfile::load("TM-T88IV");
$connector = new NetworkPrintConnector($req['ip']);
$printer = new Printer($connector, $profile);
$lista = $req['lineas'];
foreach ($lista as $key => $value) {
$printer->selectPrintMode(intval($value['selectPrintMode']));
$printer->setColor(intval($value['setColor']));
$printer->setDoubleStrike(($value['setDoubleStrike']==='true')?true:false);
$printer->setEmphasis(($value['setEmphasis']==='true')?true:false);
$printer->setFont(intval($value['setFont']));
$printer->setJustification(intval($value['setJustification']));
$printer->setLineSpacing(intval($value['setLineSpacing']));
$printer->setPrintLeftMargin(intval($value['setPrintLeftMargin']));
$printer->setReverseColors(($value['setReverseColors']==='true')?true:false);
$printer->setTextSize(intval($value['setTextSize'][0]),intval($value['setTextSize'][1]));
$printer->setUnderline(intval($value['setUnderline']));
$printer->text($value['text']);
}
$printer->feed();
$printer->cut();
$printer->close();
return Response::json($req);
}catch(Exception $e){
return Response::json($e);
}
}
Actualizacion 2:
Los envío en una petición ajax de la siguiente manera:
$.ajax({
type: 'post',
global: true,
url: 'imprimir',
data: datos, //acá va el json que envío.
}).then(
function(data) {
alertify.notify('Impreso correctamente','success');
}, function(data) {
console.log('this will run if the $.ajax fails');
console.log('Error');
console.log(data);
}, function() {
console.log('this will run if the deferred generates a progress update');
}
);