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.
Related
Because assigning a value to the "$html" with whole code which is written with php and html is tedious. Is there any way? or can we make whole page as pdf directly?
It really depends on your use case. For example, if the page you want to render to PDF can be accessed via a web server then you can just load that page in your Dompdf script:
// include dompdf then ...
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html_file("http://example.com/document.php");
$dompdf->render();
$dompdf->stream();
If the content and Dompdf logic needs to all be on the same page then you can use output buffering to capture the HTML and feed it to Dompdf:
ob_start();
// HTML + PHP to create output
$html = ob_get_clean();
ob_end_clean();
// include dompdf then ...
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream();
I am currently generating some pdfs with the dompdf library but every time a pdf is being generated my temp folder is being filled with this file and the images associated with it. I am trying to unlink or delete the pdf and any temp files generated with it but the following code is not giving the desired result. Does anyone know what else I can do in this situation?
I am using PHP and have installed the library with composer.
pdf code:
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
//$dompdf->loadHtml('hello world');
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->set_option('isRemoteEnabled', true);
$dompdf -> loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
unlink($dompdf);
I am created PDF using DOMPdf Every thing showing properly but image is not
showing.
<?php
$data1.='<p>Peace,</p><image src="http://localhost/manresaorg/im_Steve-Raymond-1.jpg"></p>Steve Raymond
<br>Associate Director';
$data1.='</main></div>';
?>
<?php
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($data1);
// (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("manresaorg.pdf");
$dompdf->stream("manresaorg.pdf", array("Attachment" => false));
exit(0);
?>
I tried absolute path like:->
<image src="E:\xampp\htdocs\manresaorg\im_Steve-Raymond-1.jpg">
But it is not showing image on PDF.I am using Xampp Server on window.
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
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);