HTML to PDF Creation in Cakephp - php

Path : Vendor/dompdf
I am getting Fatal error: Class 'DOMPDF' not found in C:\wamp\www\sms_app\app\Controller\SentMessagesController.php on line 313.
Why i am getting error? This is my code:
function example()
{
//App::import('Vendor','dompdf',array('file'=>'dompdf'.DS.'dompdf_config.inc.php'));
require_once(APP . 'Vendor' . DS . 'dompdf' . DS . 'dompdf_config.inc.php');
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
try{
$this->dompdf = new DOMPDF();
}
catch (Exception $e)
{
echo $e;
}
$papersize = "legal";
$orientation = 'landscape';
$this->dompdf->load_html($html);
$this->dompdf->set_paper($papersize, $orientation);
$this->dompdf->render();
$output = $this->dompdf->output();
file_put_contents('Brochure.pdf', $output);
}

The error message pretty clearly tells you what is wrong. Check the file(s) you include if the class exists in that file, I doubt it is there. If not figure out in which file the class is and load that file. Check how Dompdf is loading its files.

This error clear says you missing a class so please check how actually importing class in your code.

Related

Fillable PDF Form with PHP using PDFtk error

I am trying to fill out pdf files using a github project that i found on [https://github.com/mrdigitalau/PHP-PDFTK-Tutorial]
Basically there is 2 page, one named: GeneratedPDF that hold code:
<?php
namespace Classes;
if(!defined('ACCESSCHECK')) {
die('Direct access not permitted');
}
use mikehaertl\pdftk\Pdf;
class GeneratePDF {
public function generate($data)
{
try {
$filename = 'pdf_' . rand(2000,1200000) . '.pdf';
$pdf = new Pdf('./test.pdf');
$pdf->fillForm($data)
->flatten()
->saveAs( './completed/' . $filename);
//->send( $filename . '.pdf');
return $filename;
}
catch(Exception $e)
{
return $e->getMessage();
}
}
}
And another page name generate.php that holds this code:
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
exit;
}
define('ACCESSCHECK', TRUE);
require_once 'vendor/autoload.php';
use Classes\GeneratePDF;
$data = [
'name_field' => $_POST['fname'] .' ' . $_POST['lname'],
'email_field' => $_POST['email'],
'phone_field' => $_POST['phone'],
'enquiry_field' => $_POST['enquiry']
];
$pdf = new GeneratePdf;
$response = $pdf->generate($data);
header('Location: thanks.php?fname=' . $_POST['fname'] . '&link=' . $response);
The problem is every time i try to fill out a pdf I get this error:
Fatal error: Uncaught Error: Class 'Classes\GeneratePDF' not found in C:\xampp\htdocs\generate.php:23 Stack trace: #0 {main} thrown in C:\xampp\htdocs\generate.php on line 23
Any idea the problem here? Thanks in advance

How do i exit when error from new CallbackFilterIterator

I'm trying to get the files in a directory in my filesystem. Did some research here and found the necessary info to create the following piece of code that works perfectly!
define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT']);
define(FILM_IMG_UPLOAD_DIR, DOCUMENT_ROOT . '/filmography/img/films/');
$files = new filesystemiterator(FILM_IMG_UPLOAD_DIR, FilesystemIterator::SKIP_DOTS);
$filter_files = new CallbackFilterIterator($files, function($cur, $key, $iter) {
return $cur->isFile();
});
$num_files = iterator_count($filter_files);
...
The problem is when the directory does NOT exist, i get the error
Fatal error: Uncaught exception 'UnexpectedValueException' with
message
'FilesystemIterator::__construct(C:/public_html/filmography/img/films/,C:/public_html/filmography/img/films/)...
So, how do I exit the code when i get an error from new CallbackFilterIterator ?
Looks to me like you need to wrap the code in a try-catch block.
define('DOCUMENT_ROOT', '/usr/local/share');
define('FILM_IMG_UPLOAD_DIR', DOCUMENT_ROOT . '/film/');
try {
$files = new filesystemiterator(FILM_IMG_UPLOAD_DIR, FilesystemIterator::SKIP_DOTS);
$filter_files = new CallbackFilterIterator($files, function($cur, $key, $iter) {
return $cur->isFile();
});
$num_files = iterator_count($filter_files);
}
catch(UnexpectedValueException $e) {
echo "App Exception: " . $e->getMessage() . "\n";
}

mPDF not generating PDF in firefox

I am using mPDF library for creating PDF using html data.it works fine in google chrome,but in firefox it displays nothing.No files downloaded and no errors in console.
Here is my code :
require_once( $destination_path."/mpdf/mpdf.php");
$target_path = $destination_path . '/uploads/reports/';
$htmlData='<span>Sample Html Content</span>';
try {
$pdf= new mPDF();
$pdf->debug = true;
$pdf->SetFooter("MyApp" . '|{PAGENO}|' . date(DATE_RFC822));
$pdf->WriteHTML($htmlData);
$pdf->Output($fileOutputPath, 'F');
}catch (Exception $e) {
echo 'Caught exception: ', $e, "\n";
}
i am tried ob_clean() and headers for solve the issue,but it remains same .
Anyone knows how to solve this issue?

reader class not found in GeoIP2

I am trying to install Maxmind's GeoIP2. I did everything by their instructions and I still get this annoying error:
Fatal error: Class 'GeoIp2\Database\reader' not found in C:\Program Files\*\*\localweb\GeoIp2\index.php on line 19
this is how the script looks like inside index.php:
<?php
require_once 'vendor/autoload.php';
use GeoIp2\Database\reader;
// This creates the Reader object, which should be reused across
// lookups.
$reader = new Reader('C:/Program Files/*/*/localweb/GeoIp2/Database/GeoLite2-Country.mmdb');
$record = $reader->country('128.101.101.101');
?>
Anybody can help please ?
This worked for me thanks #Greg Oschwald!
Since I'm not using composer, my code now is:
<?php
require 'geoip2.phar';
try {
$reader = new GeoIp2\Database\Reader('GeoLite2-City.mmdb');
$record = $reader->city('128.101.101.101');
print($record->country->isoCode . "\n"); // 'US'
print($record->country->name . "\n"); // 'United States'
print($record->country->names['zh-CN'] . "\n"); // '??'
print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
print($record->city->name . "\n"); // 'Minneapolis'
print($record->postal->code . "\n"); // '55455'
print($record->location->latitude . "\n"); // 44.9733
print($record->location->longitude . "\n"); // -93.2323
} catch (Exception $e) {
echo 'Could not open Phar: ', $e;
}
Took that phar file from https://github.com/maxmind/GeoIP2-php/releases
Try changing:
use GeoIp2\Database\reader;
to:
use GeoIp2\Database\Reader;
Try to up php version to 7.1.33 or higher

Why can't Smarty load this block tag plugin

Here is my directory structure:
./smartytest.php
./smarty31/* (libs, etc.)
./plugins/block.sayhi.php
The PHP code that initializes smarty is:
require_once('smarty31/libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = getcwd() . '/templates';
$smarty->compile_dir = getcwd() . '/templates_c';
$smarty->plugins_dir[] = getcwd() . '/plugins';
The PHP code for the plugin is:
<?php
function smarty_block_sayhi($params, $content, $smarty, $open) {
if (!$open) {
return 'Hello: ' . $content;
}
}
?>
The error message I get is this:
Fatal error: Uncaught exception 'SmartyCompilerException' with message
'Syntax Error in template "/mypath/phptests/templates/page.tpl" on
line 11 "{sayhi}" unknown tag "sayhi"'
When the plugin was under the smarty31/libs/plugins directory, it loaded fine. Does this sample code not initialize Smarty correctly?
$smarty->plugins_dir[] = getcwd() . '/plugins';
Should be:
$existing = $smarty->getPluginsDir();
$existing[] = getcwd() . '/plugins';
$smarty->setPluginsDir($existing);
Turns out I was looking at a PHP 4 example; Smarty 3.1 uses PHP 5 access modifiers so I couldn't change plugins_dir that way.

Categories