Problem generating PHP / HTML to PDF

1

I have a certificate generator that is implemented with HTML / PHP

I used DOMPDF but it does not assign the variable $html=' <codigo todo> '; it gives error in the IF statements after I assign the variable. The same shows in the documentation and several tutorials on the internet, mine gives the error:

  

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /usr/local/www/apache24/data/applications/declaration2/certificate/105/certificate.php on line 265

In this line it contains: a IF = if ($obj->id=='81'){?>

I used buffer ob_start; it works, however it gets too off the page, I need to adjust it. But there are some certificates that appear the message:

  

Fatal error: Call to a member function get_cellmap () on a non-object in /usr/local/www/apache24/data/applications/declaration2/certificate/100/dompdf/include/table_cell_frame_reflower.cls.php on line 30

I have tried MPDF too, but it only does the background image and the fields of the certified data do not appear.

What to do?

Code of certificado.php :

require_once("dompdf/dompdf_config.inc.php");
ob_start();
if ($obj->cpf == $doc) //isset($certificado_numero) and ($certificado_numero<>''))
{
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<link href="unidade.css" rel="stylesheet" type="text/css">
<style type="text/css">

}



</style>
</head>

<body>

<table width="1070" border="0" cellpadding="1" cellspacing="1"  bgcolor="#FFFFFF">
  <tr>
    <div class="anterior">
    <td width="1068" style="border-bottom: 0px solid #666666;" align="center">
<img src="<?= $imagem; ?>" width="1070" height="698" /></td>
    </div>
  </tr>
 </table>

<div id='posiciona1'><?= utf8_decode($participante)?></div>
<div id='posiciona2'><b><?=$obj->num_certificado?></b></div>
<div id='posiciona4'><b><?= $horario; ?></b></div>
<div id='posiciona5'><? echo $observacoes."<p>"; ?></div>


</body>

<?

}
else
{
?>

<script type="text/javascript">
   alert( 'Este Certificado ainda n�o foi registrado. Aguarde!');
   self.close();
  </script>

<?
}
?>
</html>
<?php
$html = ob_get_contents();
ob_end_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper('a4', 'landscape');
$dompdf->render();
$dompdf->stream("newfile.pdf");
?>
    
asked by anonymous 30.09.2014 / 15:45

2 answers

2

As Hannibal said, let's break it down.

Your Code

Your code had the wrong approach to the problem. Well, you try to call HTML and then rename your DOMPDF .

You should put all of your HTML content within an openable one, for example:

$htmlPagina = "
<table width="1070" border="0" cellpadding="1" cellspacing="1"  bgcolor="#FFFFFF">
  <tr>
    <div class="anterior">
    <td width="1068" style="border-bottom: 0px solid #666666;" align="center">
<img src="<?= $imagem; ?>" width="1070" height="698" /></td>
    </div>
  </tr>
 </table>

<div id='posiciona1'><?= utf8_decode($participante)?></div>
<div id='posiciona2'><b><?=$obj->num_certificado?></b></div>
<div id='posiciona4'><b><?= $horario; ?></b></div>
<div id='posiciona5'><? echo $observacoes."<p>"; ?></div>"

I would like to point out that your code <?= utf8_decode($participante)?> will return an error, or rather will not return anything at all. To resolve this, type as follows: <? echo utf8_decode($participante)?> .

I must identify to you that the variables present in the HTML should be cleaned (filled) before passing the statement to DOMPDF , ie you should have them stored and instantiated previously.

DOMPDF code

I will not dwell on this issue because its application is quite simple. To render and create a Certificate file, use the following line:

// Incluímos a biblioteca DOMPDF
require_once("dompdf/dompdf_config.inc.php");

// Instanciamos a classe
$dompdf = new DOMPDF();

// Passamos o conteúdo que será convertido para PDF
$dompdf->load_html($html);

// Definimos o tamanho do papel e
// sua orientação (retrato ou paisagem)
$dompdf->set_paper('A4','portrait');

// O arquivo é convertido
$dompdf->render();

// Salvo no diretório temporário do sistema
// e exibido para o usuário
$dompdf->stream("nome-do-arquivo.pdf");

Your JS

You can make an HTML page with your JS as a preliminary to the PHP call.

You can do this by allowing js script to open the file and its compilation.

Tip : You can call PHP variables normally, like you do in a query ; and you can also call the HTML file externally, ie write the HTML on a separate page and have PHP read the file and give fopen on it.

If you have questions about nomenclature or indentation, see the DOMPDF documentation in Google Doc

    
30.09.2014 / 16:28
0

I have used the pdfcrowd class in my last projects, and I had no problems.

Installation

Copy pdfcrowd.php to your source directory.

Example

Server side PDF generation. This code converts the web page and sends the generated PDF to the browser (do not forget to use your "username" and "apikey"):

require 'pdfcrowd.php';

try
{   
    // create an API client instance
    $client = new Pdfcrowd("username", "apikey");

    // convert a web page and store the generated PDF into a $pdf variable
    $pdf = $client->convertURI('http://example.com/');

    // set HTTP response headers
    header("Content-Type: application/pdf");
    header("Cache-Control: no-cache");
    header("Accept-Ranges: none");
    header("Content-Disposition: attachment; filename=\"created.pdf\"");

    // send the generated PDF 
    echo $pdf;
}
catch(PdfcrowdException $e)
{
    echo "Pdfcrowd Error: " . $e->getMessage();
}


// Other basic operations:
// convert an HTML string
$html = "<html><body>In-memory HTML.</body></html>";
$pdf = $client->convertHtml($html);

// convert an HTML file
$pdf = $client->convertFile('/path/to/local/file.html');

The same can be found and downloaded here

    
29.10.2014 / 13:34