PHP Fatal error : Class 'DOMPDF' not found on line 30 - php

I am using DOMPDF to generate PDFs from HTML.
I have copied all required files from github (encoding branch). But it says class DOMPDF not error as below.
link for dompdf_config.inc.php in gitbub : https://github.com/dompdf/dompdf/tree/encoding
Here is my code :
require_once("APIs/dompdf-encoding/dompdf_config.inc.php");
$cart_body='<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>New Order Placed</title></head><body><p>Test Printing...</p></body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($cart_body);//body -> html content which needs to be converted as pdf..
$dompdf->render();
$dompdf->stream("sample.pdf"); //To popup pdf as download
Actual Output is :
Fatal error: Class 'DOMPDF' not found in /home/web/www/test_dompdf.php
on line 30
Line 30 is $dompdf = new DOMPDF();
Note: Other Master Branch is working fine. I need this encoding branch as it solves encoding font related issues.

I've tested your code and it works fine for me - sample.pdf file is being downloaded in browser. I've downloaded library from https://github.com/dompdf/dompdf/releases/tag/v0.6.1 url (not only the encoding branch(
Probably you haven't moved the whole project to selected directory or you haven't downloaded the whole library. I moved the whole downloaded directory content to APIs/dompdf-encoding directory and I have here files dompdf_config.inc.php and directories lib, include and www.
EDIT
As you edited you want to use only encoding branch, what you have to do is adding the following code at the beginning of your file:
use Dompdf\Adapter\CPDF;
use Dompdf\Dompdf;
use Dompdf\Exception;
EDIT2
The whole working code:
<?php
use Dompdf\Adapter\CPDF;
use Dompdf\Dompdf;
use Dompdf\Exception;
require_once("APIs/dompdf-encoding/dompdf_config.inc.php");
$cart_body='<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>New Order Placed</title></head><body><p>Test Printing...</p></body></html>';
$dompdf = new Dompdf();
$dompdf->load_html($cart_body);//body -> html content which needs to be converted as pdf..
$dompdf->render();
$dompdf->stream("sample.pdf"); //To popup pdf as download
I have also changed DOMPDF to Dompdf just in case (in Windows both are working)

This solution works for version 2.0.2.
<?php
require __DIR__ . '/vendor/autoload.php';
use Dompdf\Dompdf;
$html =
'<html><body>' .
'<p>Put your html here, or generate it with your favourite ' .
'templating system.</p>' .
'</body></html>';
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents('/tmp/Brochure.pdf', $output);

Related

dompdf placing same font in full bodytag

I have installed tangerine in my dompdf. Now what is happening is when i try on my windows machine it works fine. But in ubuntu when i put the same code in xampp the generated pdf is massed up. I used the tangerine font with only my h3 tags but in the generated pdf the font style is placed in whole body. Here is my dompdf settings
<?php
// Include autoloader
require_once 'dompdf/autoload.inc.php';
// Reference the Dompdf namespace
use Dompdf\Dompdf;
// Instantiate and use the dompdf class
$dompdf = new Dompdf(array('isPhpEnabled' => true));
// Load content from html file
ob_start();
include_once 'pdfcontent.php';
$output = ob_get_clean();
$dompdf->loadHtml($output);
//$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('b4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("funeral",array("Attachment"=>0));
?>
Well the the problem was basically with the cached dompdf file. I have deleted the dompdf folder and uploaded again. Now it works fine.

How to save generated pdf in server using dompdf php lib

I am using dompdf php lib for generating pdf.
Right now it ask user to save the file in their machine.
Code i tried so far:
<?php
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream($filename.'.pdf');
$output = $dompdf->output();
file_put_contents($filename.'.pdf', $output);
?>
Give "write" permission to the directory you need to put the file.

How to include dompdf without composer

I want to use dompdf in my php website and I try to include the library without Composer and I can not get it to work.
The code I try to test is:
<?php
include "../../plugins/dompdf/Autoloader.php";
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
?>
But i get the error:
Fatal error: Class 'Dompdf' not found ...
Can anyone explain me how to include the library without install composer in the server?
Thank You.
With the right include is working like a charm, as said Samrap i was including the wrong file.
Now the code is:
<?php
//Configure the directory where you have the dompdf
require_once "../../plugins/dompdf/autoload.inc.php";
use Dompdf\Dompdf;
//$dompdf = new dompdf();
//$dompdf = new DOMPDF();
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
?>
Thank you Samrap for your help.
You're requiring the wrong autoload file. The docs clearly state to include this file for autoloading:
require_once 'dompdf/autoload.inc.php';
If you look at that file, you'll see it does require Autoloader.php but performs a few other bootstrapping tasks as well.

HOW TO: convert HTML to PDF with PHP/DOMpdf?

I need your help to fix this:
My current code is this:
<?php
require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$html = 'Insert full HTML content';
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("codexworld",array("Attachment"=>0));
?>
This is the error a get:
Why if I have exactly the same code like the basic example, am I getting this error? What am I missing? I don't find in my folder the Autoloader.php, where do I have to get that file?
Well apparently it's an issue after domPdf has moved to Github. It seems that php-font-lib library doesn't exist. So one solution is to manually download it:
Go to https://github.com/PhenX/php-font-lib and download the library.
In the zip file, take the contents of the src/FontLib/ folder and paste that into the folder lib/php-font-lib.
or you can check this answer here

Dompdf error "No block-level parent found. Not good."

require_once("function/dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
foreach($modules as $module){
$output = "Hello " .$module['name'];
$dompdf->load_html($output);
$dompdf->render();
$output_pdf = $dompdf->output();
file_put_contents($dir . $name_modulo . ".pdf", $output_pdf);
}
Fatal error: Uncaught exception 'DOMPDF_Exception' with message 'No block-level parent found. Not good.'
Late to this thread, but based on the post at https://github.com/dompdf/dompdf/issues/902, I was able to fix this issue by removing spaces between <html><head>, </head><body>, and </body></html>
So, instead of having properly formated html code like this:
<html>
<head>
...
</head>
<body>
...
</body>
</html>
I deleted all the new lines or spaces between the tags, now it looks like this :
<html><head>
...
</head><body>
...
</body></html>
And everything is hunky-dory again
dompdf folder > dompdf_config.custom.inc.php file > try to uncomment the line
define("DOMPDF_ENABLE_HTML5PARSER", true);
Also replace unsupported html5 tags & attributes with supported one, clear html errors for better result
Just define
$dompdf->set_option('enable_html5_parser', TRUE);
I think it will fix the issue.
Or can edit the dompdf/dompdf_config.inc.php file
go to line no 322
change
def("DOMPDF_ENABLE_HTML5PARSER", false);
to
def("DOMPDF_ENABLE_HTML5PARSER", true);
go to configuration file dompdf_config.custom.inc.php and uncomment define("DOMPDF_ENABLE_HTML5PARSER", true); and check, basically it requires html5 parser enabled in your configuration file.
Just uncomment define("DOMPDF_ENABLE_HTML5PARSER", true); this line from dompdf_config.custom.inc.php this file.
You can find this file for codeigniter in vendor/dompdf/dompdf this directory.
If you have installed dompdf using composer require then defining the enable_html5_parser option using the set_option method works well if you don't want to modify the vendor folder
So add the following to each document that is creating an error
$dompdf->set_option('enable_html5_parser', TRUE);
For newer versions of Dompdf you can use this to enable the HTML5 parser:
use Dompdf\Options;
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$dompdf = new Dompdf($options);
I don't know why the answer regarding ("DOMPDF_ENABLE_HTML5PARSER", true) was voted down either. Because I spent over 30 hours trying to figure out why this was happening in my website, until I came across this solution. So I enabled the "DOMPDF_ENABLE_HTML5PARSER" by setting it to "TRUE" as noted above. And immediately the error regarding "No block-level parent found" was resolved and my scripts worked. Maybe the 2nd answer was voted down because it duplicated a previous answer (but cut a guy a break, why don't you? Maybe he didn't see it..). Or maybe the "downvoter" has an attitude problem.
I had the same problem with domPDF 0.8.3 on PHP 5.6 and I couldn't find neither dompdf_config.custom.inc.php nor dompdf_config.inc.php and didnĀ“t work either; to solve my problem, I added body {display:block;} to my CSS and had not further problem.
I tried locating the white space or other spaces. didn't succeed, so
I just Minimized the HTML file that is a PDF template
it removed all the spaces, white spaces, etc
worked for me
You can enable the HTML5 parser as mentioned above or you can update the dompdf library. I found updating the dompdf library from 6.2 to 8.2 got past this error BUT if you are using Drupal like I am there's a little bit of trickery you have to do to get Drupal to recognize the newer version of the library (see here: https://www.drupal.org/project/print/issues/3010637 )
In my case, it is a bit different.
The pdf creation code was in the loop but the dom pdf object creation was above the loop. So when it is trying to create the pdf for the second time, I had seen the above error. To resolve it, each and every time I am creating a new object and it worked as expected.
Example:
(Code not working)
//Reference of Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;
//instantiate and use the Options class
$options = new Options();
$options->set('enable_html5_parser', true);
$dompdf = new Dompdf($options);
for($i = 0; $i < $count; $i++) {
//Create PDF
$dompdf->loadHtml($form_data);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$output = $dompdf->output();
file_put_contents($pdf_file_name, $output);
}
Working Code:
//Reference of Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;
//instantiate and use the Options class
$options = new Options();
$options->set('enable_html5_parser', true);
for($i = 0; $i < $count; $i++) {
$dompdf = new Dompdf($options);
//Create PDF
$dompdf->loadHtml($form_data);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$output = $dompdf->output();
file_put_contents($pdf_file_name, $output);
}

Categories