Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Convert PDF to HMTL

pdf html dompdf php

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1 lucasdcsp

lucasdcsp

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 14 July 2014 - 06:49 PM

Hello, I need to convert a personal html document to pdf, I'm doing tests with DOMPDF apparently simple but this is occurring the following error: Unable to stream pdf: headers already sent.

 

Below is code I'm using:

 

<?php
require_once
("dompdf/dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>

 

 

Anyone can identify this error?
 
TKS.
 
 


#2 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 14 July 2014 - 07:06 PM

When you see the error "headers already sent" is because you try (maybe not you, but DOMPDF) to send header data after some normal data was sended.

 

You can look at your code to see if something isn't sending data before this function (a space before a php tag, or an echo/print)

And if you don't find anything, you can put everything in cache with ob_start() at the very start of your script


You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 15 July 2014 - 03:07 AM

Welcome aboard,

 

It's exactly as Vaielab says. How are you calling/loading this php script? are you calling it directly? or is it wrapped into other code?



#4 lucasdcsp

lucasdcsp

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 15 July 2014 - 04:12 AM

Sorry for my english, but I'm using a translator hehe ... 
So I'm calling this code directly. No other function. I'm just running this file in the browser. And as you can see, there is no space between the tag. Neither worked with ob_start ();   :(


#5 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 15 July 2014 - 04:58 AM

The other possibility would be that your file is encoded in utf-8 BOM, witch will add a invisible chars at the begening

 

Here a great post to read to know more on this

http://stackoverflow.../8028987/554660


You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#6 lucasdcsp

lucasdcsp

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 15 July 2014 - 10:25 AM

but neither is downloading the file. I need to run the page to download a. 
If it was utf problem would at least be downloaded.


#7 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 15 July 2014 - 10:28 AM

a utf8 BOM problem... not the same thing as a utf8 problem

 

utf8Bom add invisible chars at the start of your file

php see this invisible char, and since it's not a php command, it send it to the browser (so the header is sended)

Then you try to resend the header, and now you have a problem

 

... did you even read the link I got you?


You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#8 lucasdcsp

lucasdcsp

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 15 July 2014 - 06:34 PM

Content is English, and translate it got a little confusing. But from what I understand I do a header. However've seen many running headless. If you can put a model that is working and by downloading the pdf file for me. I appreciate it. 
 
Thank you.

<?php
require_once("dompdf/dompdf_config.inc.php");
$html = "<html>";
$html .= "<head>";
$html .= "<link rel='stylesheet' href='obj/estilo.css'>";
$html .= "</head>";
$html .= "<body>";
$html .= "<table width='100%' border='1' cellspacing='0' cellpadding='0' style='background:#ffffff;border-collapse:collapse;font-family:verdana;font-size:12px;' bordercolor='#CCCCCC'>";
$html .= "<tr>";
$html .= "<td>";
$html .="<label>".Testeeee."</label>";
$html .= "</td>";
$html .= "</tr>";
$html .= "</table>";
$html .= "</body>";
$html .= "</html>";


$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
Neither worked well.


#9 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts

Posted 15 July 2014 - 11:16 PM

My guess:

$html .="<label>".Testeeee."</label>";

 
Testeeee is undefined, and causing a plaintext error to be sent before the PDF streaming, thus causing further errors regarding information (in this case headers+an error) being already sent.
 
Please ensure error reporting is turned on maximum as per our FAQ in this forum section and only after it is error free, use the PDF library to stream the intended content.

What the others have been trying to explain as well is written in our FAQ, in where a hidden byte is written to the beginning of the file for encoding purposes but causes the above error. This is hard to debug, and can be fixed by turning UTF8 encoding off (or turn off using a UTF "BOM") if there is an available option.
 
Alexander.

Edited by Alexander, 15 July 2014 - 11:22 PM.

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#10 rhossis

rhossis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 98 posts

Posted 16 July 2014 - 01:19 AM

Alex is right. I have tried to reproduce this. Worked when I fixed the Testee issue. Either declare Testee as a variable...

$Testeeee = 'Your text here';
$html .="<label>".$Testeeee."</label>";

or keep it within your string quotes if intended as a string...

$html .="<label>Testeeee</label>";


#11 lucasdcsp

lucasdcsp

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 23 July 2014 - 05:01 AM

Good day, 
 
Unable to resolve the problea was unicode format when you save the file. 
 
Now I have another problem, I need to automatically pick up that file in pdf and send it by email. Following script I am testing.
 
$sql3 = mysql_query ("select u.cd_usuario,u.email, v.* from login as u, veiculo as v where u.cd_usuario = v.cd_usercad order by v.cdVeiculo desc limit 1", $conexao);

    if(mysql_num_rows($sql3) > 0){
		while($RSSS = mysql_fetch_assoc($sql3)){
				
				$Destinatario= array();
				if(substr($RSSS["email"],0,1) == 1){$Destinatario[]= "mail1@mail.com";}
				if(substr($RSSS["email"],1,1) == 1){$Destinatario[]= "mail2@mail.com";}
				if(substr($RSSS["email"],2,1) == 1){$Destinatario[]= "mail3@mail.com";}
				if(substr($RSSS["email"],3,1) == 1){$Destinatario[]= "mail4@mail.com.br";}
				if(substr($RSSS["email"],4,1) == 1){$Destinatario[]= "mail5@mail.inf.br";}
				if(substr($RSSS["email"],5,1) == 1){$Destinatario[]= "mail6@mail.com.br";}

                $Assunto = "Veiculo";
				$html		= "<!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'>
								<body style='margin:0px;font-family:sans-serif; font-size:11px;'>";
                $html     .= "<table width='50%' border='0' style='font-family:verdana;font-size:12px;'>";
                $html     .= "<tr><td><b>CÓDIGO DE CADASTRO:</b><td><b>".strtoupper($RSSS["cdVeiculo"])."</b></td></tr>";
				$html     .= "<tr><td><b>&nbsp;</b></td></tr>";
////DADOS DA EMPRESA SOLICITANTE/////////////////////////////
                $html     .= "<tr><td colspan='2' align='center' bgcolor='#A9A9A9'><b>EMPRESA SOLICITANTE</b><br></td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>Empresa:</b></td><td>".strtoupper($RSSS["empresaSolicitante"])."</td></tr>";
               	$html     .= "<tr><td><b>Responsável:</b></td><td>".strtoupper($RSSS["responsavelSolicitante"])."</td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>Telefone:</b></td><td>".strtoupper($RSSS["telSolicitante"])."</td></tr>";				
				if(substr($RSSS["situacaoFuncional"],0,1) == "1"){$html .="<tr><td><b>Situação Funcional:</b></td><td>".'Empregado'."</td></tr>";}
				if(substr($RSSS["situacaoFuncional"],1,1) == "1"){$html .="<tr><td><b>Situação Funcional:</b></td><td>".'Agregado'."</td></tr>";}
				if(substr($RSSS["situacaoFuncional"],2,1) == "1"){$html .="<tr><td><b>Situação Funcional:</b></td><td>".'Terceiro'."</td></tr>";}
				$html     .= "<tr><td><b>&nbsp;</b></td></tr>";
////DADOS DO MOTORISTA/////////////////////////////////////////
                $html     .= "<tr><td colspan='2' align='center' bgcolor='#A9A9A9'><b>DADOS MOTORISTA</b><br></td></tr>";
				$html     .= "<tr bgcolor='#F4F4F4'><td><b>CPF:</b></td><td>".strtoupper($RSSS["cpfmotVeiculo"])."</td></tr>";
                $html     .= "<tr><td><b>Nome:</b></td><td>".strtoupper($RSSS["nomeMotVeiculo"])."</td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>Celular:</b></td><td>".strtoupper($RSSS["telcelmotVeiculo"])."</td></tr>";
				$html     .= "<tr><td><b>&nbsp;</b></td></tr>";
////DADOS DO VEICULO////////////////////////////////////////
                $html     .= "<tr><td colspan='2' align='center' bgcolor='#A9A9A9'><b>DADOS DO VEÍCULO</b><br></td></tr>";
				$html     .= "<tr bgcolor='#F4F4F4'><td><b>Placa:</b></td><td>".$RSSS["placaVeiculo"]."</td></tr>";
                $html     .= "<tr><td><b>UF:</b></td><td>".$RSSS["uf1"]."</td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>Cidade:</b></td><td>".$RSSS["cidade1"]."</td></tr>";
                $html     .= "<tr><td><b>Marca:</b></td><td>".$RSSS["marcaVeiculo"]."</td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>Ano do Modelo:</b></td><td>".$RSSS["anoModVeiculo"]."</td></tr>";
                $html     .= "<tr><td><b>Modelo:</b></td><td>".strtoupper($RSSS["modeloVeiculo"])."</td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>Ano de Fabricação:</b></td><td>".strtoupper($RSSS["fabricacaoVeiculo"])."</td></tr>";
                $html     .= "<tr><td><b>Cor:</b></td><td>".strtoupper($RSSS["corVeiculo"])."</td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>Chassi:</b></td><td>".strtoupper($RSSS["chassiVeiculo"])."</td></tr>";
                $html     .= "<tr><td><b>Renavan:</b></td><td>".strtoupper($RSSS["renavanVeiculo"])."</td></tr>";
				if(substr($RSSS["combustivelVeiculo"],0,1) == "1"){$html .="<tr bgcolor='#F4F4F4'><td><b>Combustivel:</b></td><td>".'Diesel'."</td></tr>";}
				if(substr($RSSS["combustivelVeiculo"],1,1) == "1"){$html .="<tr bgcolor='#F4F4F4'><td><b>Combustivel:</b></td><td>".'Gasolina'."</td></tr>";}
				if(substr($RSSS["combustivelVeiculo"],2,1) == "1"){$html .="<tr bgcolor='#F4F4F4'><td><b>Combustivel:</b></td><td>".'Alcool'."</td></tr>";}
				$html     .= "<tr><td><b>&nbsp;</b></td></tr>";
////DADOS DO RASTREADOR/////////////////////////////////////////////
                $html     .= "<tr><td colspan='2' align='center' bgcolor='#A9A9A9'><b>DADOS DO RASTREADOR</b><br></td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>Rastreador:</b></td><td>".strtoupper($RSSS["rastreadorVeiculo"])."</td></tr>";
                $html     .= "<tr><td><b>Marca:</b></td><td>".strtoupper($RSSS["marcaRastreador"])."</td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>Gerenciadora:</b></td><td>".strtoupper($RSSS["gerenciadoraRastreador"])."</td></tr>";
                $html     .= "<tr><td><b>Contato:</b></td><td>".strtoupper($RSSS["telRastreador"])."</td></tr>";
				$html     .= "<tr bgcolor='#F4F4F4'><td><b>&nbsp;</b></td></tr>";
////DADOS DO PROPRIETARIO DO VEICULO//////////////////////////////
                $html     .= "<tr><td colspan='2' align='center' bgcolor='#A9A9A9 '><b>DADOS DO PROPRIETÁRIO DO VEÍCULO</b><br></td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>CPF/CNPJ:</b></td><td>".strtoupper($RSSS["cpfProprietario"])."</td></tr>";
                $html     .= "<tr><td><b>Nome:</b></td><td>".strtoupper($RSSS["nomeProprietario"])."</td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>Tel. Residencial:</b></td><td>".strtoupper($RSSS["telresidProprietario"])."</td></tr>";
                $html     .= "<tr><td><b>Bairro:</b></td><td>".strtoupper($RSSS["bairroProprietario"])."</td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4' ><td><b>Endereço:</b></td><td>".strtoupper($RSSS["enderecoProprietario"])."</td></tr>";
                $html     .= "<tr><td><b>Número:</b></td><td>".strtoupper($RSSS["numeroProprietario"])."</td></tr>";
                $html     .= "<tr bgcolor='#F4F4F4'><td><b>UF:</b></td><td>".$RSSS["uf2"]."</td></tr>";
                $html     .= "<tr><td><b>Cidade:</b></td><td>".$RSSS["cidade2"]."</td></tr>";
				$html     .= "<tr><td><b>&nbsp;</b></td></tr>";
                $html     .= "</table>";
                $html     .= "<br><br>OBS: Favor não responder este e-mail. Envio Automático.";
				$html	   .= "</body></html>";
								
												// Incluímos a biblioteca DOMPDF
				 //Chamada para PDF
				require_once("../../dompdf/dompdf_config.inc.php");
				$dompdf = new DOMPDF();
				$dompdf->load_html($html);
				$dompdf->set_paper('letter', 'landscape');
				$dompdf->render();
				$dompdf->stream("teste.pdf"/*, array("Attachment" => 0)*/);
	}

				EnvioMail($Destinatario, $Assunto, $html);
}
?>





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download