Skip to content

Instantly share code, notes, and snippets.

@DykiSA
Last active April 20, 2020 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DykiSA/1b768c3e296a983a0b398b2b3a08a07d to your computer and use it in GitHub Desktop.
Save DykiSA/1b768c3e296a983a0b398b2b3a08a07d to your computer and use it in GitHub Desktop.
Generate PDF using PHP Codeigniter + wkHtmlToPdf

Codeigniter PHPWkHTMLToPDF

Requirements

  1. wkhtmltopdf - executable binary to generate pdf file from html file
  2. phpwkhtmltopdf - php library to handle html file creation then execute wkhtmltopdf cli
  3. composer - dependency manager to install php dependencies

Setting up the codeigniter

  1. Install wkhtmltopdf binary and ensure it's work well on your machine
  2. Add phpwkhtmltopdf dependency to your project.
  3. Add WKPDF.php to your libraries directory (application/libraries/)
  4. Add phpwkhtmltopdf.php to your config directory (application/config/)
  5. Update the configuration phpwkhtmltopdf.php file to align with your environment

Available configs are available at https://github.com/mikehaertl/phpwkhtmltopdf

How to use?

Generate pdf in your controller:

$this->load->library('WKPDF');
// true in third params is to determine
// whether the pdf file should be downloaded
// otherwise it will be shown in your browser as preview
if (!$this->wkpdf->generate('<h1>Hello World!</h1>', 'hello.pdf', true)) {
    echo $this->wkpdf->getError();
}
<?php
/**
* phpwkhtmltopdf config
* @link https://gist.github.com/DykiSA/1b768c3e296a983a0b398b2b3a08a07d
* @source https://github.com/mikehaertl/phpwkhtmltopdf
*/
/**
* path to the wkhtmltopdf executable
*/
$config['phpwkhtmltopdf']['binary'] = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf';
/**
* path to store generated temp files
*/
$config['phpwkhtmltopdf']['tmpDir'] = APPPATH . '../temp/';
$config['phpwkhtmltopdf']['commandOptions'] = [
'useExec' => true
];
<?php defined('BASEPATH') or exit('No direct script access allowed');
use mikehaertl\wkhtmlto\Pdf as HtmlToPDF;
/**
* A simple codeigniter library to help setup phpwkhtmltopdf
* @link https://gist.github.com/DykiSA/
*/
class WKPDF
{
private $CI;
private $error;
private $options;
/**
* intialize the class WKPDF
* @param array $options override options for phpwkhtmltopdf
*/
public function __construct($options = [])
{
$this->CI =& get_instance();
$this->initialize($options);
}
/**
* initialize the library
* @param array $options override options for phpwkhtmltopdf
*/
public function initialize($options = []) {
// get options
$this->CI->load->config('phpwkhtmltopdf');
$this->options = $this->CI->config->item('phpwkhtmltopdf');
if (is_array($options) && !empty($options)) {
$this->options = array_merge($this->options, $options);
}
}
/**
* generating html
* @param string $content content of the pdf file
* @param string $filename will be shown as downloaded file
* @param bool $is_download download = true/false
* @return bool true if sucess otherwise false
*/
public function generate($content, $filename, $is_download)
{
// clear error messgae
$this->error = null;
// init phpwkhtmltopdf
$pdf = new HtmlToPDF($this->options);
// add content
$pdf->addPage($content);
// generate pdf
$is_success = false;
if ($is_download) {
$is_success = $pdf->send($filename);
} else {
$is_success = $pdf->send();
}
if (!$is_success) {
// update error message
$this->error = $pdf->getError();
}
return $is_success;
}
/**
* get error message from last action
* @return string error message
*/
public function getError()
{
return $this->error;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment