HTTP ERROR 500 (Consulta MySQL gerar relatório com TCPDF)
Consegui fazer o script funcionar com a biblioteca "FPDF" porém não consegui fazer a quebra de linhas.
Vou postar dois scripts o primeiro funciona perfeitamente mas não faz o que preciso que seria, em uma página com um formulário eu informa a data inicial e final para a consulta e uma placa ou em branco para consultar todas as placas no período.
Primeiro caso funcionando sem filtros, abre uma página já com o resultado e com possibilidade de gerar o arquivo PDF.
<?php
function formatoData($data){
$array = explode("-", $data);
// $data = 2016-04-14
// $array[0]= 2016, $array[1] = 04 e $array[2]= 14;
$novaData = $array[2]."/".$array["1"]."/".$array[0];
return $novaData;
}
function fetch_data()
{
$output = '';
$conn = mysqli_connect("localhost", "root", "", "requisicao");
$sql = "SELECT * FROM manutencao ORDER BY id ASC";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result))
{
$output .= '<tr>
<td>'.$row["data"].'</td>
<td>'.$row["placa"].')</td>
<td>'.$row["km"].'</td>
<td>'.$row["descricao"].'</td>
<td>'.$row["valor"].'</td>
</tr>
';
}
return $output;
}
if(isset($_POST["generate_pdf"]))
{
require_once("../classes/tcpdf/tcpdf.php");
$obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$obj_pdf->SetCreator(PDF_CREATOR);
$obj_pdf->SetTitle("Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP");
$obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont('helvetica');
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);
$obj_pdf->setPrintHeader(false);
$obj_pdf->setPrintFooter(false);
$obj_pdf->SetAutoPageBreak(TRUE, 10);
$obj_pdf->SetFont('helvetica', '', 11);
$obj_pdf->AddPage();
$content = '';
$content .= '
<h4 align="center">Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP</h4><br />
<table border="1" cellspacing="0" cellpadding="3">
<tr>
<th width="13%">Data</th>
<th width="13%">Placa</th>
<th width="10%">KM</th>
<th width="54%">Descrição</th>
<th width="10%">Valor</th>
</tr>
';
$content .= fetch_data();
$content .= '</table>';
$obj_pdf->writeHTML($content);
$obj_pdf->Output('file.pdf', 'I');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Relatório de Despesas</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>
<body>
<br />
<div class="container">
<h4 align="center"> Relatório HTML</h4><br />
<div class="table-responsive">
<div class="col-md-12" align="right">
<form method="post">
<input type="submit" name="generate_pdf" class="btn btn-success" value="Generate PDF" />
</form>
</div>
<br/>
<br/>
<table class="table table-bordered">
<tr>
<th width="10px">Data</th>
<th width="10px">Placa</th>
<th width="20px">KM</th>
<th width="100px">Descrição</th>
<th width="10px">Valor</th>
</tr>
<?php
echo fetch_data();
?>
</table>
</div>
</div>
</body>
</html>
Segundo caso como eu pensei em fazer.... =/
<?php
require_once("../classes/tcpdf/tcpdf.php");
function formatoData($data){
$array = explode("-", $data);
$novaData = $array[2]."/".$array["1"]."/".$array[0];
return $novaData;
}
$output = '';
$conn = mysqli_connect("localhost", "root", "", "requisicao");
// $sql = "SELECT * FROM manutencao ORDER BY id ASC";
if (empty($_POST["id_placa"])){
$sql = "SELECT * FROM manutencao WHERE data BETWEEN '{$_POST["data_ini"]}' AND '{$_POST["data_fim"]}'";
}
if (!empty($_POST["id_placa"])){
$sql = "SELECT * FROM manutencao WHERE data BETWEEN '{$_POST["data_ini"]}' AND '{$_POST["data_fim"]}' AND placa = '{$_POST["id_placa"]}'";
}
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result))
{
$retornadatada = formatoData($row["data"]);
$output .= '<tr>
<td>'.$retornadatada.'</td>
<td>'.$row["placa"].'</td>
<td>'.$row["km"].'</td>
<td>'.$row["descricao"].'</td>
<td>'.$row["valor"].'</td>
</tr>
';
}
return $output;
}
$obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$obj_pdf->SetCreator(PDF_CREATOR);
$obj_pdf->SetTitle("Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP");
$obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont('helvetica');
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);
$obj_pdf->setPrintHeader(false);
$obj_pdf->setPrintFooter(false);
$obj_pdf->SetAutoPageBreak(TRUE, 10);
$obj_pdf->SetFont('helvetica', '', 11);
$obj_pdf->AddPage();
$content = '';
$content .= '
<h4 align="center">Relatório</h4><br />
<table border="1" cellspacing="0" cellpadding="3">
<tr>
<th width="13%">Data</th>
<th width="13%">Placa</th>
<th width="10%">KM</th>
<th width="54%">Descrição</th>
<th width="10%">Valor</th>
</tr>
';
$content .= fetch_data();
$content .= '</table>';
$obj_pdf->writeHTML($content);
$obj_pdf->Output('file.pdf', 'I');
?>Discussão (2)
Carregando comentários...