Convert an HTML / BOOSTRAP / PHP output to PDF

Hi, I am new to web development. I have a report (invoice) created in a blank with boostrap, php and html. I have created another Blank that executes the first blank sending the invoice to print. My idea is to take that output and convert it to PDF. I found in a video the use of the “wkhtmltopdf” program which is part of scriptcase and is supposed to allow it to be done. The truth is that I could not generate the PDF (it does not work for me and I do not know why.
My code is:
$ invoiceId = 51;
$ wkhtmltopdf = “C: / Users / ruben / Program Files / NetMake / v9-php73 / wwwroot / scriptcase / prod / third / wkhtmltopdf / win / wkhtmltopdf”;
$ application = “http://127.0.0.1:8091/scriptcase/app/tempus/blank_factura/index.php?facturaId=”.$facturaId;
$ invoice = “C: / Program Files / NetMake / v9-php73 / wwwroot / scriptcase / tmp / invoice _”. $ invoiceId. “. pdf”;
$ run = $ wkhtmltopdf. ’ ‘. $ application.’ '. $ invoice;

echo “wkhtmltopdf INFORMATION:”. $ wkhtmltopdf. “
”;
echo “application INFORMATION:”. $ application. “
”;
echo “INFO invoice:”. $ invoice. “
”;
echo “INFORMATION execution:”. $ execute. “
”;
$ exe = shell_exec ($ execute);

// Take the file and display it
header (‘Location:’. $ invoice);

If I run it through CMD it sends me the following message:
“It is not recognized as an internal or external command, program, or executable batch file.”

I found videos on the internet about the message and I can’t get it to execute.

TBH, I have never tried this construction. But It’s quite easy to generate a pdf. Have you considered creating a pdf directly?

Hi,
I think there are many more advantages to using an html, you can make the background designs and take advantage of the language changes (today it forces you to have an image for each language), that also applies to regional formats. Scriptcase should have a macro to facilitate the conversion. If you think about it, they currently have to be doing it.

1 Like

Well, I’m not going to argue about that. Thing is, that you can do a lot in HTML that is not supported by PDF and visa-versa. So even if SC would implement something like that, it always will be some kind of subset of HTML. But SC is making use of tcpdf and the tcpdf library support HTML functions. See Example 061 : XHTML + CSS · TCPDF
Perhaps that’s useful?

I review it
Thanks for support.

yo he creado un pdf desde un blank con otro blank dejame tu correo por interno para poder ayudarte.
saludos,

Gracias Fidel, hice que funcionara con solucione wkhtmltopdf

Hola , me podrias compartir la solucion ? tengo el mismo problema … Gracias.

Hola , me podrias compartir la solucion ? … Gracias.

Hola,

La siguiente función permite ubicar el wkhtmltopdf no importe el SO.
function rutaWkhtmltopdf() {
$rutaThird = $this->Ini->path_third;
$rutaWkPdf = “”;

	if (FALSE !== strpos(strtolower(php_uname()), 'windows')) {
		$rutaWkPdf = 'C:\"Program Files"\NetMake\v9-php73\wwwroot\scriptcase\prod\third\wkhtmltopdf\win\wkhtmltopdf ';
	} elseif (FALSE !== strpos(strtolower(php_uname()), 'linux')) {
		if (FALSE !== strpos(strtolower(php_uname()), 'i686')) 	{
			if (FALSE !== strpos(php_uname(), 'Debian 4.19')) {
				$rutaWkPdf = $rutaThird . "/wkhtmltopdf/buster/wkhtmltopdf-i386";
			} elseif (FALSE !== strpos(php_uname(), 'Debian 4.9')) {
				$rutaWkPdf = $rutaThird . "/wkhtmltopdf/stretch/wkhtmltopdf-i386";
			} else {
				$rutaWkPdf = $rutaThird . "/wkhtmltopdf/linux-i386/wkhtmltopdf-i386";
			}
		} else	{
			if (FALSE !== strpos(php_uname(), 'Debian 4.19')) {
				$rutaWkPdf = $rutaThird . "/wkhtmltopdf/buster/wkhtmltopdf-amd64";
			} elseif (FALSE !== strpos(php_uname(), 'Debian 4.9')) {
				$rutaWkPdf = $rutaThird . "/wkhtmltopdf/stretch/wkhtmltopdf-amd64";
			} elseif (FALSE !== strpos(php_uname(), '.el8.')) {
				$rutaWkPdf = $rutaThird . "/wkhtmltopdf/centos8/wkhtmltopdf-amd64";
			} else	{
				$rutaWkPdf = $rutaThird . "/wkhtmltopdf/linux-amd64/wkhtmltopdf-amd64";
			}
		}
	} elseif (FALSE !== strpos(strtolower(php_uname()), 'darwin')) {
		$rutaWkPdf = $rutaThird . "/wkhtmltopdf/osx/Contents/MacOS";
	}

	return $rutaWkPdf;
}

En un blank puedes colocar el siguiente código:

    $wkhtmltopdf = rutaWkhtmltopdf(); // Llamada a la función previa
    $baseDatos      .= "_".$empresaId."/factura";
$rutaDocumento   = $this->Ini->path_doc;
$rutaAdjunto     = $rutaDocumento.'/factura_'.$factura.'.pdf';   // Ruta del PDF resultado
$rutaHTML        = $rutaDocumento.'/factura_'.$factura.'.html'; // Ruta del archivo HTML

if (!file_exists($rutaDocumento)) {
	mkdir($rutaDocumento, 0777, true);
}
   // Si estas en windows crear la ruta del documento final
if (FALSE !== strpos(strtolower(php_uname()), 'windows')) {
	$rutaDocumento = str_replace("Program Files", '"Program Files"', $rutaDocumento);	
} 

$fHTML = fopen($rutaHTML, 'w+') or die("Se produjo un error al crear el archivo"); // Crea el archivo temporal
fwrite($fHTML, $html) or die("No se pudo escribir en el archivo"); // Grava html
fclose($fHTML);

// Corrige la ruta HTML
$rutaHTML = $rutaDocumento.'/factura_'.$factura.'.html';

$paramWK	=	"--encoding utf-8 --page-size Letter --quiet --margin-top 10mm --margin-bottom 10mm ";	
$PDF        = $rutaDocumento.'/factura_'.$factura.'.pdf';
$ejecutar = $wkhtmltopdf.'  '.$paramWK.'  '.$rutaHTML.'   '. $PDF; 
$exe = shell_exec($ejecutar);		

if (FALSE !== strpos(strtolower(php_uname()), 'windows')) {
	$rutaHTML = str_replace('"Program Files"', "Program Files", $rutaHTML);
}