Reputation: 19
How to use snappy library with codeigniter.
i've installed and have it in my libraries folder . like
require_once dirname(FILE) . '/snappy/vendor/autoload.php'; use Knp\Snappy\Pdf;
class wkhtmltopdf extends Pdf
{
function __construct()
{
parent::__construct();
}
}
2 ) then in contoller i've load it through function __construct()
$this->load->library('wkhtmltopdf');
in one of my controller i set binary like
$snappy = new wkhtmltopdf(""C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"");
and try it 4 )
echo $snappy->getOutput(array('http://www.google.com.pk'));
but the below error come.
but it thrown an error. Type: LogicException
Message: You must define a binary prior to conversion.
Filename: C:\xampp\htdocs\reporting-new-icn-huge-changes\application\libraries\snappy\vendor\knplabs\knp-snappy\src\Knp\Snappy\AbstractGenerator.php
Anybody have implemented wkhtmltopdf in codeignter through any method will be very helpful.
Upvotes: 0
Views: 908
Reputation: 19
After download the library using Composer.
$ composer require knplabs/knp-snappy
Add it in the Library folder and give the path for the autoload.php
require_once dirname(__FILE__)."application/libraries/snappy/vendor/autoload.php";
Adjust the above path and outside of the Controller class
use Knp\Snappy\Pdf; # at the top of the controller
#& inside the controller function
{
$snappy = new Pdf("\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\"");
header('Content-Type: application/pdf');
echo $snappy->setOption('page-width', '188mm');
echo $snappy->setOption('page-height', '100mm');
# these options are Optional there are a lot of other options.
#Can check more options Here https://wkhtmltopdf.org/usage/wkhtmltopdf.txt
echo $snappy->getOutput($url);
#OR in case of you want the HTML to put directly open in the Browser.
echo $snappy->getOutputFromHtml($html);
#OR
echo $snappy->generateFromHtml('<p>Some content</p>', 'test.pdf');
# this test.pdf will be saved in hte Codeigniter directory
}
To get out put directly see this question. enter image description here
<img src= "https://i.sstatic.net/haagb.png">
https://wkhtmltopdf.org/downloads.html the Exe file in windows and the same type of executable requires for Linux, Amazon etc. given on that page.
further requires this library which is wrapper used to set settings, output and other stuff etc.
https://github.com/KnpLabs/snappy > this can be installed using git commands in vendor folder
Upvotes: 1