Problem generating PHP/HTML for PDF

Asked

Viewed 2,812 times

1

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

I used DOMPDF but it does not assign the variable $html=' <codigo todo> '; it gives error in IF’s commands after I assign the variable. Same shows in the documentations and various tutorials on the internet, mine gives the error:

Parse error: syntax error, Unexpected T_CONSTANT_ENCAPSED_STRING in /usr/local/www/Apache24/data/applications/declaraca2/certificate/105/certificate.php on line 265

On that line contains: a IF = if ($obj->id=='81'){?>

I used the buffer ob_start; it works, but it gets too off the page, I need to adjust it. But it has 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/declaraca2/certificate/100/dompdf/include/table_cell_frame_reflower.cls.php on line 30

I tried MPDF too, but it only makes 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");
?>

2 answers

2


As Hannibal said, let’s go in pieces.

Your Code

Your code got the wrong approach to the problem. Well you try to call HTML and then render yours DOMPDF.

You should put all your HTML content inside an avariable, 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 stress that your code <?= utf8_decode($participante)?> return error, or better, return nothing at times. To resolve this, write as follows: <? echo utf8_decode($participante)?>.

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

DOMPDF code

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

// 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 preliminary to calling PHP.

To do so you can make the script js allow, or not, the opening of the file and its compilation.


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

If you have any questions about nomenclature or indentation, see DOMPDF documentation on Google Doc

0

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

Installation

Copy pdfcrowd.php to your source directory.

Example

Server side PDF Generation. This code converts a web page and sends the generated PDF to the browser (don’t 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

Browser other questions tagged

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