I am developing a sales system, with HTML, javascript, php, jquery and ajax (it may not be the best combination but it is my first system), and when I want to transfer the data of a sale to excel with PHPExcel (the typical summary that is delivered to the client) works fine as long as there are not two elements in the list, there can be only one, or 3 or more, but the error only arises when there are only 2 elements in the list. The error says:
Forbidden
You do not have permission to access /src/pages/Excel/DetalleExcel.php on this server.
Additionally, at 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
In all other cases, it works perfectly for me. I leave the code of everything that has to do in that part. In summary, the idea of the system is that when it completes the list of products is placed to sell, this in turn generates an array with all the data in the list and sends them by ajax to a php file to print a label with Button shape of Bootstrap with reference to a file prepared to receive the data and treat them as excel, the data is sent as GET parameters and encoded for security, and when the button is clicked, the file prepared with the data is called and the open the window to download. That's like the basic idea of functioning.
HTML mode where the button that starts everything appears
<div class='modal fade' id='modalExito2' role='dialog'>
<div class='modal-dialog'>
<!-- Modal content-->
<div class='modal-content'>
<div class='modal-header'>
<h4 class='modal-title'>Exito</h4>
<button type='button' class='close' data-dismiss='modal'>×</button>
</div>
<div class='modal-body'>
<p>Los productos fueron descontados correctamente del inventario.</p>
</div>
<div class='modal-footer'>
<div id="btnDescarga"></div>
<button type='button' id='cerrarExito' class='btn btn-success' data-dismiss='modal' onclick=eliminarTrs()>Close</button>
</div>
</div>
</div>
</div>
JavaScript
function array2M(){
var elementos = [];
var precioTotalModificar = document.getElementById("txtPrecioPaqueteModificar").value;
var filas = $('#tBodyModificar').find('tr');
for(i=0; i<filas.length; i++){ //Recorre las filas 1 a 1
var celdas = $(filas[i]).find("td"); //devolverá las celdas de una fila
codigo= $(celdas[0]).text();
nombre= $(celdas[1]).text();
precio= $(celdas[2]).text();
cantidad = $($(celdas[3]).children("input")[0]).val();
// var stock = Comprobar(cantidad, codigo);
// Descontar(stock, codigo);
elementos.push(codigo);
elementos.push(nombre)
elementos.push(precio)
elementos.push(cantidad);
}
var jObject={};
for(i in elementos)
{
jObject[i] = elementos[i];
}
jObject= JSON.stringify(jObject);
$.ajax({
async:false,
cache: false,
data:{jObject: jObject, pfinal: precioTotalModificar},
url: "AJAX/prueba.php",
type: "POST",
success: function(response){
$("#btnDescarga").html(response);
}
});
$("#modalModificarPaquete").modal("hide");
$("#modalExito2").modal();}
Prueba.php
<?
$data = json_decode($_POST['jObject'], true);
$data = serialize($data);
$data = base64_encode($data);
$data = urlencode($data);
$pfinal = $_POST["pfinal"];
echo "<a class='btn btn-primary btn-sm mr-3' href='Excel/DetalleExcel.php?array=".$data."&pfinal=".$pfinal."'>Descarga</a>";
Clicking on the label that appears in Prueba.php goes to DetalleExcel.php (Although I do not think that is the problem here)
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
setlocale(LC_ALL,"es_ES");
$titulo = "Vencidos - " . date("d") . "/" . date("m") . "/" . date("Y");
$fecha = date("d") . "-" . date("m") . "-" . date("Y");
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once '../../../Classes/PHPExcel.php';
if (isset($_GET['array'])) {
$array = base64_decode($_GET['array']);
$array = unserialize($array);
}
$pfinal = $_GET["pfinal"];
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
$objPHPExcel = new PHPExcel();
//------------------------ FUNCIONES!! ----------------
function cellColor($cells,$color){
global $objPHPExcel;
$objPHPExcel->getActiveSheet()->getStyle($cells)->getFill()->applyFromArray(array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => $color
)
));
}
//----------------------- STYLES ------------------
$bordeTodos = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
),
),
);
$bordeExternosStyle = array(
'borders' => array(
'outline' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
),
),
);
$bordeYcentradoYColor = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
),
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => 'ffc000',
),
),
);
$bordeYcentradoYColor2 = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
),
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => '92d050',
),
),
);
$tituloVet = array(
'font' => array(
'bold' => true,
'size' => 16,
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => '0bb5ce',
),
),
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
),
),
);
$productosItems = array(
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
),
);
$stylePfinal = array(
'font' => array(
'bold' => true,
'size' => 16,
),
);
$styleFono = array(
'font' => array(
'size' => 9,
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
$styleVet = array(
'font' => array(
'size' => 11,
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
//----------------- VARIABLES ---------------
$items = sizeof($array);
$row = 10;
$total = ($items/4) + $row - 1;
$item=1;
$i = 0;
$celdasFormat='F10:F'.$total;
$celdasFormat2='H10:H'.$total;
$celdasFormat3='C10:H'.$total;
$filaCodigoNombre = "D10:E" . $total;
$filaCodigo = "D10:D" . $total;
$strPFinal = "F" . ($total+4) . ":G" . ($total +4);
$strPFinal3 = "F" . ($total+4) . ":H" . ($total +4);
$ultimo2 = "G" .($total+2) . ":H" . ($total+2);
$finalLimits1 = "C" . ($total+8) . ":H" . ($total+8);
$finalLimits2 = "C" . ($total+9) . ":H" . ($total+9);
$finalStr1 = "C" . ($total+8);
$finalStr2 = "C" . ($total+9);
$strUltimo2 = "G" . ($total+2);
$strPFinal2 = "F" . ($total+4);
$strPFinalPrecio = "H" . ($total+4);
$ultimo = "H" . ($total +2);
$sumaTotal = "=SUM(H10:H".($total).")";
$bordeExterno = "B2:I" . ($total+10);
$bordeInterno = "B" . ($total+7) . ":I" . ($total+10);
for($i; $i<$items; $i++){
$precio = str_replace('$', '', $array[$i+2]);
$precio = str_replace('.', '', $array[$i+2]);
$precioTotal = "=F" . $row . "*G" . $row;
$objPHPExcel->getActiveSheet()->setCellValue("C{$row}", $item);
$objPHPExcel->getActiveSheet()->setCellValue("D{$row}", $array[$i]);
$objPHPExcel->getActiveSheet()->setCellValue("E{$row}", $array[$i+1]);
$objPHPExcel->getActiveSheet()->setCellValue("F{$row}", $precio);
$objPHPExcel->getActiveSheet()->setCellValue("G{$row}", $array[$i+3]);
$objPHPExcel->getActiveSheet()->setCellValue("H{$row}", $precioTotal);
$item++;
$row++;
$i+= 3;
}
$objPHPExcel->getProperties()
->setCreator('VetPortugal')
->setTitle('Inventario')
->setDescription('Excel con todos los productos incluidos en el sistema')
->setKeywords('Excel VetPotugal PHP')
->setCategory('VetPortugal');
//----------------- DIMENCIONES COLUMNAS------------------------
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(13);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(16);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(16);
//----------------- MERGES ------------------------
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('C3:E5');
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('C7:H7');
$objPHPExcel->setActiveSheetIndex(0)->mergeCells($strPFinal);
$objPHPExcel->setActiveSheetIndex(0)->mergeCells($finalLimits1);
$objPHPExcel->setActiveSheetIndex(0)->mergeCells($finalLimits2);
//----------------- APLICACION DE TEMAS ---------------------
$objPHPExcel->getActiveSheet()->getStyle('C3:E5')->applyFromArray($tituloVet);
$objPHPExcel->getActiveSheet()->getStyle('G4:H4')->applyFromArray($bordeTodos);
$objPHPExcel->getActiveSheet()->getStyle('C7:H7')->applyFromArray($bordeYcentradoYColor);
$objPHPExcel->getActiveSheet()->getStyle('C9:H9')->applyFromArray($bordeYcentradoYColor2);
$objPHPExcel->getActiveSheet()->getStyle($celdasFormat3)->applyFromArray($bordeTodos);
$objPHPExcel->getActiveSheet()->getStyle($filaCodigoNombre)->applyFromArray($productosItems);
$objPHPExcel->getActiveSheet()->getStyle($strPFinal3)->applyFromArray($bordeTodos);
$objPHPExcel->getActiveSheet()->getStyle($strPFinalPrecio)->applyFromArray($stylePfinal);
$objPHPExcel->getActiveSheet()->getStyle($ultimo2)->applyFromArray($bordeTodos);
$objPHPExcel->getActiveSheet()->getStyle($finalStr2)->applyFromArray($styleFono);
$objPHPExcel->getActiveSheet()->getStyle($finalStr1)->applyFromArray($styleVet);
$objPHPExcel->getActiveSheet()->getStyle($bordeExterno)->applyFromArray($bordeExternosStyle);
$objPHPExcel->getActiveSheet()->getStyle($bordeInterno)->applyFromArray($bordeExternosStyle);
//----------------- SETS -----------------------
$objPHPExcel->getActiveSheet()->setCellValue("C3", 'VetPortugal - Centro Veterinario Integral');
$objPHPExcel->getActiveSheet()->setCellValue("G4", 'Fecha Compra');
$objPHPExcel->getActiveSheet()->setCellValue("H4", $fecha);
$objPHPExcel->getActiveSheet()->setCellValue('C7', 'Detalle de la Compra');
$objPHPExcel->getActiveSheet()->setCellValue('C9', 'Item');
$objPHPExcel->getActiveSheet()->setCellValue('D9', 'Código');
$objPHPExcel->getActiveSheet()->setCellValue('E9', 'Nombre');
$objPHPExcel->getActiveSheet()->setCellValue('F9', 'Precio');
$objPHPExcel->getActiveSheet()->setCellValue('G9', 'Cantidad');
$objPHPExcel->getActiveSheet()->setCellValue('H9', 'Total');
$objPHPExcel->getActiveSheet()->setCellValue($strUltimo2, 'Total Compra');
$objPHPExcel->getActiveSheet()->setCellValue($strPFinalPrecio, $pfinal);
$objPHPExcel->getActiveSheet()->setCellValue($strPFinal2, 'Precio Con Descuento / Promoción');
$objPHPExcel->getActiveSheet()->setCellValue($ultimo, $sumaTotal);
$objPHPExcel->getActiveSheet()->setCellValue($finalStr1, 'VetPortugal - Portugal 1015, Esquina Porvenir, Santiago Centro');
$objPHPExcel->getActiveSheet()->setCellValue($finalStr2, 'Fono (2) 2222 6819 - www.vetportugal.cl');
//---------------- FORMATO CELDAS -------------------------
$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow(7, 4)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY);
$objPHPExcel->getActiveSheet()->getStyle($celdasFormat)->getNumberFormat()->setFormatCode('$#,##0');
$objPHPExcel->getActiveSheet()->getStyle($celdasFormat2)->getNumberFormat()->setFormatCode('$#,##0');
$objPHPExcel->getActiveSheet()->getStyle($ultimo)->getNumberFormat()->setFormatCode('$#,##0');
$objPHPExcel->getActiveSheet()->getStyle($filaCodigo)->getNumberFormat()->setFormatCode('#');
$objPHPExcel->getActiveSheet()->getStyle($strPFinalPrecio)->getNumberFormat()->setFormatCode('$#,##0');
//---------------- FUNCIONES ------------------
cellColor('G4', 'ffc000');
cellColor($strUltimo2, 'ffc000');
cellColor($strPFinal2, 'ffc000');
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setShowGridlines(false);
$objPHPExcel->getActiveSheet()->setTitle('Resumen');
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Resumen.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
For your help .. Thanks: D