Error generating PDF with mPDF

Asked

Viewed 532 times

0

I rode this script to generate a PDF invoice, but when I try to generate the PDF the message appears:

This PDF document may not be displayed correctly.

<?php
$meses = array(
    '01' => 'Janeiro',
    '02' => 'Fevereiro',
    '03' => 'Março',
    '04' => 'Abril',
    '05' => 'Maio',
    '06' => 'Junho',
    '07' => 'Julho',
    '08' => 'Agosto',
    '09' => 'Setembro',
    '10' => 'Outubro',
    '11' => 'Novembro',
    '12' => 'Dezembro',
);
$multa = ($fatura['Fatura']['total'] / 100) * FloatToSQL($fatura['Fatura']['multa_mora']);
$juros = ($fatura['Fatura']['total'] / 100) * FloatToSQL($fatura['Fatura']['juros_mora']);
$html = '
<html>
<head>
<style>
body {font-family: sans-serif;
    font-size: 10pt;
}
p { margin: 0pt; }
table.items {
    border: 0.1mm solid #000000;
}
td { vertical-align: top; }
.items td {
    border-left: 0.1mm solid #000000;
    border-right: 0.1mm solid #000000;
}
table thead td { background-color: #EEEEEE;
    text-align: center;
    border: 0.1mm solid #000000;
    font-variant: small-caps;
}
.items td.blanktotal {
    background-color: #EEEEEE;
    border: 0.1mm solid #000000;
    background-color: #FFFFFF;
    border: 0mm none #000000;
    border-top: 0.1mm solid #000000;
    border-right: 0.1mm solid #000000;
}
.items td.totals {
    text-align: right;
    border: 0.1mm solid #000000;
}
.items td.cost {
    text-align: "." center;
}
.box {
    border: 1px solid #CCC;

    padding: 10px;
}
.box10 {
    width: 10%;
    float: left;
    border: 1px solid #CCC;    
    padding: 10px;
}
.box90 {
    width: 85%;
    margin-left: 3%;
    float: left; 
    padding: 5px;
}
</style>
</head>
<body>

<htmlpageheader name="myheader">
<div class="box">
<table width="100%">
<tr>
<td width="150">
<img src="http://www.intercambioglobal.com.br/img/logo.jpg">
</td>
<td style="font-size:12px">
Intercâmbio Global <span style="font-family:dejavusanscondensed;">&#9742;</span> +55 (11) 3149-8199<br />
INTERCAMBIO OPERADORA DE PROGRAMAS EDUCACIONAIS LTDA<br/>
CNPJ 51.201.713/0001-54 - IATA 57-70654-1 - CADASTUR 26.022964.10.0001-1<br/>
Rua dos Três Irmãos, 625 - Bairro: Morumbi<br />
São Paulo - SP - CEP 05615-190<br />
Site www.intercambioglobal.com.br<br/>E-mail: [email protected]
</td>
</tr>
</table>
</div>
<br/>
<div class="box10">
<table>
<tr text-rotate="90">
<td align="center"></td>
<td align="center"></td>
<td align="center">___________________________________________________</td>
<td align="center">Assinatura do Emitente &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>
</table>
</div>
<div class="box90">
<div class="box">
<table width="100%" style="font-size:12px;">
<tr>
<th width="15%" style="border-right:1px solid black; font-size:8px;" >FATURA / DUPLICATA</th>
<th width="17%" style="border-right:1px solid black; font-size:8px;">VALOR</th>
<th width="20%" style="border-right:1px solid black; font-size:8px;">EMISSÃO</th>
<th width="17%" style="border-right:1px solid black; font-size:8px;">VENCIMENTO</th>
<th width="31%" style="font-size:8px;">Para uso da Inst. Financeira</th>
</tr>
<tr>
<td align="center" style="font-size:9px;"><b>FT0000000'.$fatura['Fatura']['nro_fatura'].'</b></td>
<td align="center" style="font-size:9px;"><b>R$ '.number_format($fatura['Fatura']['total'], 2, ',','.').'</b></td>
<td align="center" style="font-size:9px;"><b>'.DataFromSQL($fatura['Item'][0]['data']).'</b></td>
<td align="center" style="font-size:9px;"><b>'.$fatura['Fatura']['vencimento'].'</b></td>
</tr>
</table>
</div>
<div style="text-align: left; font-size:9px;">
Encargos p/ dia de R$ '.number_format($juros, 2, ',','.').' após dia '.$fatura['Fatura']['vencimento'].'<br/>
</div>
</div>
</htmlpageheader>
<htmlpagefooter name="myfooter">
<div style="border-top: 1px solid #000000; font-size: 9pt; text-align: center; padding-top: 3mm; ">
Página {PAGENO} de {nb} páginas
</div>
</htmlpagefooter>
<sethtmlpageheader name="myheader" value="on" show-this-page="1" />
<sethtmlpagefooter name="myfooter" value="on" />
<div class="texto-pos" style="text-align:right; margin:1%;">
<span style="font-size:8px;">Fatura impressa em '.date('d').' de '.$meses[date('m')].' de '.date('Y').'</span>
</div>
<br />
<br />
<br />
<br />
<br />
<br />
<table width="80%" align="right" style="font-family: serif;" cellpadding="10">
<tr>
<td style="border: 0.1mm solid #888888; font-size:10px; "><span>Sacado:</span> '.$fatura['Fatura']['sacado_nome'].'<br /> '.nl2br($fatura['Fatura']['sacado_endereco']).'   
<br /><br />
<div style="text-align: center;  font-size:10px;">Cobrança: '.nl2br($fatura['Fatura']['cobranca']).'</div>
<div style="text-align: center;  font-size:11px;">Praça de pagamento: <span style="font-size:11px;">'.nl2br($fatura['Fatura']['praca_pagamento']).'</span></div>
<br />
<div style="text-align: center; font-style: italic;  font-size:12px;"><b>Extenso: '.nl2br($fatura['Fatura']['valor_extenso']).'</b></div>
<br/>
<div style="text-align: center; font-size:8; ">
<p>Reconhecemos a exatidão desta Fatura de Prestação de Serviços, na importância acima, que pagaremos a INTERCAMBIO OPERADORA DE PROGRAMAS EDUCACIONAIS LTDA, ou a sua ordem, na praça e vencimento indicados.</p>
</div>
<br />
<br />
<table width="100%">
<tr>
<td width="40%" style="text-align: center">
____/____/________<br/>
DATA DO ACEITE
</td>
<td width="60%" style="text-align: center">
_______________________________________________<br/>
ASSINATURA DO SACADO
</td>
</tr>
</table>
</td>
</tr>
</table>
<br />
<table align="left">
<tr>
    <td align="left">
    * Disponibilizamos o envio de fatura on-line. Entre em contato. <u>[email protected]</u>
    </td>
</tr>
</table>
<br />
<br />
<table class="items" width="100%" style="font-size: 9pt; border-collapse: collapse; " cellpadding="8">
<thead>
<tr>
<td style="font-size:8px;">N. Item</td>
<td style="font-size:8px;">'.utf8_encode('Emissão').'</td>
<td style="font-size:8px;">Nome Pax</td>
<td style="font-size:8px;">'.utf8_encode('Referência').'</td>
<td style="font-size:8px;">R$ Bruto</td>
<td style="font-size:8px;">R$ Outros</td>
<td style="font-size:8px;">R$ Desc</td>
<td  style="font-size:8px;">R$ Liquido</td>
</tr>
</thead>
<tbody>
<!-- ITEMS HERE -->';
$num = 0;
$total = 0;
foreach ($fatura['Item'] as $item){
    $num++;
    $total += (float)$item['valor'];
    $html .= '<tr>
<td align="center" width="5%" style="font-size:8px;">'.$num.'</td>
<td align="center" width="10%" style="font-size:8px;">'.DataFromSQL($item['data']).'</td>
<td align="center" width="18%" style="font-size:8px;">'.$item['nome_pax'].'</td>
<td align="center" width="25%" style="font-size:8px;">'.nl2br($item['descricao']).'</td>
<td align="center" width="12%" style="font-size:8px;">R$ '.number_format($item['valor'], 2, ',', '.').'</td>    
<td align="center" width="10%" style="font-size:8px;">'.$item['outros'].'</td>
<td align="center" width="8%" style="font-size:8px;">'.$item['desconto'].'</td>    
<td class="cost" width="12%" align="center" style="font-size:8px;">R$ '.number_format($item['valor'], 2, ',', '.').'</td>
</tr>
';
}
$html .= '
<!-- END ITEMS HERE -->
<tr class="itens">
<td class="blanktotal" colspan="7"></td>
<td class="totals cost" style="font-size:8px;">Total R$ '.number_format($total, 2, ',', '.').'</td>
</tr>
</tbody>
</table>

<span style="font-size:8px;">Emitido por: João Carlos</span>
</body>
</html>
';
//==============================================================
//==============================================================
//==============================================================
//==============================================================
//==============================================================
//==============================================================

define('_MPDF_PATH','mpdf60/');
include_once("mpdf60/mpdf.php");

$mpdf=new mPDF('c','A4','','',20,15,48,25,10,10); 
//$mpdf->SetProtection(array('print'));
$mpdf->SetTitle("Intercâmbio Global - Fatura");
$mpdf->SetAuthor("Intercâmbio Global");
//$mpdf->SetWatermarkText("Fatura");
//$mpdf->watermark_font = 'DejaVuSansCondensed';
//$mpdf->watermarkTextAlpha = 0.1;
$mpdf->SetDisplayMode('fullpage');



$mpdf->WriteHTML($html);


$mpdf->Output(); exit;

exit;

?>

I already checked my 10x code and I can’t identify if it’s semantics or something, if anyone can help me..

1 answer

0


@Michel, this message can be caused by several things, here are some ways to find your problem and how to facilitate the mounting of your pdf.

1° Remove your html and leave only one hello world. If you run, the problem becomes your html content.

2° Save html content to a file teste.html and check if there are any unmarked tags.

3° Activate debug of mpdf Activating the Debug

4° If you want to give more organization to your code you can make macros within html and then replace with the function str_replace example :

$html = str_replace("[FATURA]", $fatura, $html);
$html = str_replace("[VALOR]", $valor, $html);
$html = str_replace("[EMISSAO]", $emissao, $html);
$html = str_replace("[VENCIMENTO]", $vencimento, $html);
<table width="100%" style="font-size:12px;">
  <tr>
    <th width="15%" style="border-right:1px solid black; font-size:8px;">FATURA / DUPLICATA</th>
    <th width="17%" style="border-right:1px solid black; font-size:8px;">VALOR</th>
    <th width="20%" style="border-right:1px solid black; font-size:8px;">EMISSÃO</th>
    <th width="17%" style="border-right:1px solid black; font-size:8px;">VENCIMENTO</th>
    <th width="31%" style="font-size:8px;">Para uso da Inst. Financeira</th>
  </tr>
  <tr>
    <td align="center" style="font-size:9px;"><b>[FATURA]</b>
    </td>
    <td align="center" style="font-size:9px;"><b>[VALOR]</b>
    </td>
    <td align="center" style="font-size:9px;"><b>[EMISSAO]</b>
    </td>
    <td align="center" style="font-size:9px;"><b>[VENCIMENTO]</b>
    </td>
  </tr>
</table>

This way you separate php code from html and can debug it better.

  • following his steps, I was able to filter and see that the problem is in this code snippet, but even seeing the html still seems to me all right, semantics, concatenation php.. http://hastebin.com/bominituqe.xml

  • @Michelhenriq takes a test without the logo

  • Still the same mistake.

  • plays in the mpdf folder and tests http://hastebin.com/zitudehesa.md

  • Notice (8): Undefined offset: 16 [CORE/mpdf60/mpdf.php, line 21162] Notice (8): Undefined offset: 1 [CORE/mpdf60/mpdf.php, line 21193] Output has already been sent from the script - PDF file Generation aborted.

  • is using progressbar?

  • No, without any loading..

  • I am using version 5.7.4 of mpdf, which you use ? check this code: http://hastebin.com/ihegefuqej.md

  • I really don’t know what else to do, but thanks for the help.

  • 1

    Dude, the problem was this line here <div class="texto-pos" style="text-align:right; margin:1%;">&#xA;<span style="font-size:8px;">Fatura impressa em '.date('d').' de '.$meses[date('m')].' de '.date('Y').'</span>&#xA;</div> , somehow, he wouldn’t accept this div, so I did it inside a tr/td and it was all right.

  • @Michelhenriq now that you managed to find the problem edit your question with the answer.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.