Friday, March 31, 2017

Convert HTML to PDF with wkhtmltopdf



1. Download and install wkhtmltopdf. (Refer https://wkhtmltopdf.org/).
    How To Install wkhtmltopdf In CentOS 6.5 (https://jaimegris.wordpress.com/2015/03/03/how-to-install-wkhtmltopdf-in-centos-6-5/)
2. Caller page.
Buka Dalam Format Excel
3. Conversion to PDF page (laporan_1_pdf.php)
$host = $_SERVER['HTTP_HOST'];
$idJenis = $_GET['id_jenis'];
$idNegeri = $_GET['id_negeri'];
$idDaerah = $_GET['id_daerah'];
$baseUrl = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$baseUrls = explode('/', $baseUrl);
$appCtx = $baseUrls[1];  
$pdfFileName = "laporan1.pdf";

$url = '"http://'.$host.'/'.$appCtx.'/apps/maklumat/laporan/laporan_1_html.php?id_jenis='.$idJenis.'&id_negeri='.$idNegeri.'&id_daerah='.$idDaerah.'"';
$cmd = 'e:\wkhtmltopdf\bin\wkhtmltopdf.exe '.$url.' pdf/'.$pdfFileName.'';
exec($cmd);

header("Location: http://".$host."/".$appCtx."/apps/maklumat/laporan/pdf/".$pdfFileName); /* Redirect browser */
exit();
4. laporan_1_html.php is your html that your want to convert to PDF. Just render your html here. * Attention: Do not put slash in url variable.
In linux environment, wkhtmltopdf log can be seen at /var/log/httpd/error_log

No comments:

Post a Comment