Skip to content

Instantly share code, notes, and snippets.

@juanchehin
Created July 5, 2024 22:04
Show Gist options
  • Save juanchehin/ac3f48c33f9630c70725be10c50af1a1 to your computer and use it in GitHub Desktop.
Save juanchehin/ac3f48c33f9630c70725be10c50af1a1 to your computer and use it in GitHub Desktop.
$baseDir = dirname(dirname(__DIR__)); // Subir un nivel desde el directorio del script actual
// Construir la ruta absoluta
$imagePath = $baseDir . '/' . $row['archivo'];
$imagePath = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $imagePath);
if (!file_exists($imagePath)) {
$imagePath = $baseDir . '/media/image/logo-lebron.jpg';
$imagePath = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $imagePath);
}
$html .= '<tr>';
$html .= '<td><img src="' . $imagePath . '" width="50" height="50"></td>'; // IMAGEN
$html .= '<td>' . $row['codigo'] . '</td>';
$html .= '<td>' . $row['producto'] . '</td>';
$html .= '<td>' . "$" . $precio_final . '</td>';
$html .= '</tr>';
$html .= '</table>';
$html .= '
</body></html>';
// Configurar Dompdf
$options = new \Dompdf\Options();
// $options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true); // Permitir cargar imágenes remotas
$dompdf = new \Dompdf\Dompdf($options);
// Cargar HTML en Dompdf
$dompdf->loadHtml($html);
$dompdf->output();
// Renderizar PDF
$dompdf->render();
// Acceder al objeto Canvas
$canvas = $dompdf->getCanvas();
$date_time = date('d-m-Y H:i:s');
$canvas->page_text(10, $canvas->get_height() - 20, "$date_time", null, 10, array(0, 0, 0));
// Descargar el PDF generado
$fileName = "productos-lebron-" . date('Ymd') . ".pdf";
$dompdf->stream($fileName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment