Introduction
This document is about converting an html page with images to a pdf file. You have to provide the content of the html file in this code and the code will convert the html content to a pdf content and either you can download it or you can save or you can do both. Images and css will also be included in the output pdf as it were in the source html page. You can see the example here of how a pdf will look like after converting this html sample to pdf. This html to pdf conversion is done using the code from dompdf. This is very simple and straight forward. Yet you can follow certain instructions for additional functionality which is well explained in the main site dompdf.Besides, i have just used this code and i did not yet mastered. My purpose was to convert an html page including image and css to a pdf file and this code really worked well. My purpose was to generate a bill of orders with the company logo and the order listing with different colors. dompdf supports certain fonts which you can find in the download section or in the actual file which you can download directly from dompdf.
Code and Explanation
In the current folder(that is the folder where this html file is) i have two subfolders (cppdf and html) and a file named demo.php. cppdf is the folder which has all the files needed to convert html to pdf and the html folder is having sample html file with css and images. so the following code includes all the needed files like i had said above.
Here i give few lines of code which is present in the demo.php. When you download the complete sources you can find the file in the zip file.
Here i give few lines of code which is present in the demo.php. When you download the complete sources you can find the file in the zip file.
Hope the above code is self explanatory. We include the main config file and include few globals.
Give a name to the output file and set $save_file to true which will mean that the file will be saved in the current working directory.
And then we read the total content of the html file which may have link attributes to include styles and image tags.
And here is the main picture.
Give a name to the output file and set $save_file to true which will mean that the file will be saved in the current working directory.
And then we read the total content of the html file which may have link attributes to include styles and image tags.
And here is the main picture.
$dompdf = new DOMPDF(); $base_path = $_SERVER [ 'DOCUMENT_ROOT' ]. '/html/' ; $dompdf ->load_html( $buff ); if ( isset( $base_path ) ) { $dompdf ->set_base_path( $base_path ); } $dompdf ->render(); |
We create an object dompdf.
And we set the base path.
What is this base path?
The base path parameter is used to resolve relative urls. So, if you have an image tag like so:
<img src="images/pretty.png"/>
and you set the base_path like so:
$dompdf->set_base_path("/var/www/");
dompdf will look for the image at /var/www/images/pretty.png on the webserver.
If you don't set the base_path parameter, and you run dompdf from the /var/www/dompdf-0.5 directory, dompdf will look for the image source at /var/www/dompdf-0.5/images/pretty.png, because the default base_path is the directory of the script being executed.
Hope this makes sense
Reference for this paragraph is taken from http://www.dashinteractive.net/dompdf/index.php?v=1514257
And we set the base path.
What is this base path?
The base path parameter is used to resolve relative urls. So, if you have an image tag like so:
<img src="images/pretty.png"/>
and you set the base_path like so:
$dompdf->set_base_path("/var/www/");
dompdf will look for the image at /var/www/images/pretty.png on the webserver.
If you don't set the base_path parameter, and you run dompdf from the /var/www/dompdf-0.5 directory, dompdf will look for the image source at /var/www/dompdf-0.5/images/pretty.png, because the default base_path is the directory of the script being executed.
Hope this makes sense
Reference for this paragraph is taken from http://www.dashinteractive.net/dompdf/index.php?v=1514257
file_put_contents ( $outfile , $dompdf ->output( array ( "compress" => 0) )); if (!headers_sent() ) $dompdf ->stream( $outfile ); |
The first line will save the converted pdf in the current directory and the next like will try to download the file if you have uncommented it like now. Else the file will be only saved in the disk. To make the download dialog appear you need the use the $dompdf->stream function.
Make sure your root folder have enough permission if you want to test the demo which will save a pdf file in that location.
Make sure your root folder have enough permission if you want to test the demo which will save a pdf file in that location.
No comments:
Post a Comment