I was trying out HTML2PDF, a "nonsense" thing that only has one file plus the composer.json . I show my codes.
print_pdf.php:
<?php
require_once __DIR__.'/vendor/autoload.php';
use Spipu\Html2Pdf\Html2Pdf;
$html2pdf = new Html2Pdf();
$html2pdf->writeHTML('<h1> Hola mundo!! desde html2pdf </h1>');
$html2pdf->output();
?>
composer.json:
{
"require": {
"spipu/html2pdf": "5.*"
}
}
And it gave me the following error:
Fatal error: Uncaught Exception: TCPDF ERROR: Some data has already been output, can't send PDF file in C:\xampp\htdocs\HTML2Pdf\vendor\tecnickcom\tcpdf\tcpdf.php:2950
Stack trace: #0 C:\xampp\htdocs\HTML2Pdf\vendor\tecnickcom\tcpdf\tcpdf.php(7653): TCPDF->Error('Some data has a...')
#1 C:\xampp\htdocs\HTML2Pdf\vendor\spipu\html2pdf\src\Html2Pdf.php(575): TCPDF->Output('document.pdf', 'I')
#2 C:\xampp\htdocs\HTML2Pdf\print_pdf.php(11): Spipu\Html2Pdf\Html2Pdf->output() #3 {main} thrown in C:\xampp\htdocs\HTML2Pdf\vendor\tecnickcom\tcpdf\tcpdf.php on line 2950
I began to investigate and read that the problem could be that there was a space before the <?php
initial. And honestly, it seemed kind of weird to me that that could be the reason, but I took out the space (spaces actually) and it fixed the problem. The PDF appeared correctly.
That's where my doubt comes from, in what I read they said that I had to silence the following:
error_reporting(0) and ini_set('display_errors', 0)
Is it recommended or is it a bad idea to silence those errors?
If it is recommended, how are they silenced?
Just to clarify, where I read that didn't explain how to mute them. And I ask, because when I program and start a file, maybe it's a bad habit, a bad habit of mine, but I start writing the codes from approximately line 5. If they tell me that it is not recommended, there is no drama. I'll get used to writing from line 1 or at least in PHP.
Answering the first question: No, it is absolutely not recommended to disable errors because there is always the possibility of human error to make a code error.
Answering the second question: If you still want to do it, this is how to configure
error_reporting()
Source: php.net
Put yourself in a User perspective of your system. You are browsing the application and suddenly:
The user is not interested in the error (and most likely won't even understand it), so it doesn't add any value and may actually provide information to an attacker.
Returning to your question , is it recommended or a bad idea to silence these errors? , from my point of view it is advisable to hide errors from view, but not to ignore them. Errors should ALWAYS
Fatal error
be corrected , a way to keep seeing if your application produces errors and correct them without affecting the output, is to keep an eye on the log, for example, in a linux console with the command:You can be aware of everything that your application generates, even when it is already in production.
By the way, the error that you get there is because before generating the PDF your script is generating some output (some missing character or a space).
You have to take into account the environment:
My recommendation when configuring the errors is to do it directly at the server level by modifying the error_reporting directive , instead of doing it at runtime, since the development and production servers are not the same we can set a different value for each environment . This prevents us from forgetting to change the directive at runtime before committing any changes to production.
Documentation: error_reporting