I've been using HTML2PDF for a while and recently, my script can't load the unique picture in my file.
// Génération du PDF
ob_start();
echo '<page>';
echo '<br/><br/><img src="https://www.xxx.fr/images/xxx.jpg" style="width:200px; float:left; margin-right:20px;" />';
// MORE HTML CODE WITHOUT ERROR
echo '</page>';
$content = ob_get_clean();
require('html2pdf/html2pdf.class.php');
try
{
$pdf = new HTML2PDF('P','A4','fr');
$pdf->writeHTML($content);
ob_end_clean();
$pdf->Output('/home/xxx/www/images/upload/' . $data["id_client"] . '/factures/' . $fichier.'.pdf', 'F');
}
catch(HTML2PDF_exception $e) {
die($e);
}
The script works fine except for this error (in french) :
ERREUR n°6
Fichier : /home/xxx/www/scripts/html2pdf/html2pdf.class.php
Ligne : 1319
Impossible de charger l'image https://www.xxx.fr/images/xxx.jpg
line 1319 in html2pdf.class.php is this one (I never edited this file) :
// if the image does not exist, or can not be loaded
if (count($infos)<2) {
// if the test is activ => exception
if ($this->_testIsImage) {
throw new HTML2PDF_exception(6, $src);
}
// else, display a gray rectangle
$src = null;
$infos = array(16, 16);
}
I can ignore this error using $pdf->setTestIsImage(false); (that simply replaces pictures with colored div) or by deleting the IMG line but that's not really my need. The weird thing is the file exists, as you can check in the browser.
The other subjetcs on StackOverFlow couldn't help me as the problem was slightly different.
What could've happen and how to force the picture into my file ? Let me know if you need more infos.
EDIT : Here's the log error for the picture
[Mon Feb 15 13:14:57 2021] [error] [client 51.68.11.227] ModSecurity: Access denied with code 403 (phase 2). Operator EQ matched 0 at REQUEST_HEADERS. [file "/usr/local/apache2/conf/modsecurity/base_rules/modsecurity_crs_21_protocol_anomalies.conf"] [line "65"] [id "960009"] [rev "2.1.1"] [msg "Request Missing a User Agent Header"] [severity "NOTICE"] [tag "PROTOCOL_VIOLATION/MISSING_HEADER_UA"] [tag "WASCTC/WASC-21"] [tag "OWASP_TOP_10/A7"] [tag "PCI/6.5.10"] [hostname "www.xxx.fr"] [uri "/images/xxx.jpg"] [unique_id "YCplweC3lcgrp0C9HNJpBQAAADM"]
I found a solution just by using the relative path instead of absolute. Thank to @CBroe who lead me to the logs that pointed the User-Agent issue.