Convert html to pdf using tcpdf in php

Convert html to pdf using tcpdf in php

Download tcpdf and include require file.

include (‘tcpdf/tcpdf.php’);

Extend default class of tcpdf If you want edit or modify pdf

class MYPDF extends TCPDF {
//Page header
public function Header() {
$headerData = $this->getHeaderData();
$this->SetFont(‘helvetica’, ‘B’, 10);

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, ‘UTF-8’, false); //Default for UTF-8 unicode
//$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, ‘ISO-8859-1′, false); // set unicode to ISO-8859-1 so special chars like æ, ø, å will work.
// set default header data
$pdf->setHeaderData($ln=”, $lw=0, $ht=”, $hs='<table id=”head” cellpadding=”1″ cellspacing=”1″ style=”text-align:center;”><tr><td>All Category Report</td></tr></table>’, $tc=array(0,0,0), $lc=array(0,0,0));
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, ”, PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, ”, PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set document information

Create table structure html and apply css in style.

$html ='<style>
th{border:0.5px solid #C0C0C0;background-color:rgb(44,126,193); font-size: 9pt;text-align: center;color:#FFFFFF;font-weight:bold;}
td{ vertical-align: middle;padding-top:5px;border:0.5px solid #C0C0C0;padding:5pt;color:#000000;background-color:#FFFFFF;font-size: 8pt;text-align: center;}
<tr nobr=”true”><th>Id</th><th>Name</th></tr>
<tr nobr=”true”>

Finally write html to pdf to store in this folder create new pdf folder

$pdf->writeHTML($html, true, false, false, false, ”);
$fname = ‘test-‘.date(“d-m-Y-H-i-s”).’.pdf’; // filename test
$pdf->output(dirname(dirname(__FILE__)).”/pdf/test-“.date(“d-m-Y-H-i-s”).’.pdf’, ‘I’); // pdf view
//$pdf->Output(dirname(dirname(__FILE__)).”/pdf/test-“.date(“d-m-Y-H-i-s”).’.pdf’,’F’); // save in pdf folder

Make sure All content of code under <?php ?> tag and remove whitespace before closing tag of php.


How to call webservice in php?

I have called web services in php by using curl function. In below I have given  one example.  I have sent XML Request by using curl function.Because My web services return data on XML Format.

$xml_data = ''; //pass parameter using XML request provide By webservices
$postData = array(     // You can pass parameter using Array and pass in postfields.


$URL = "";

$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);

Continue reading

how to display article intro image in joomla module?

Article display by yourtheme/html/com_content/article/tmpl/default.php file.

you can use module mod_articles_latest so edit mod_articles_latest/tmpl/default.php file.

* @package Joomla.Site
* @subpackage mod_articles_latest
* @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt

defined('_JEXEC') or die;
<ul class="latestnews<?php echo $moduleclass_sfx; ?>">
<?php foreach ($list as $item) : ?>
<a href="<?php echo $item->link; ?>">
<?php echo $item->title; ?></a>
<img src="<?php echo json_decode($item->images)->image_intro; ?>"/> //new line
<?php endforeach; ?>

Export the database table as CSV format using PHP

This code need some files those files are,
as in the db.php file, make this.

database name –> wallstreet
table name–> export_table
column names –> id,name,place
and as you, here i just declare what i have given in.

$conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db=mysql_select_db('wallstreet', $conn) or die(mysql_error());



//header to give the order to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exported-data.csv');

//select table to export the data
$select_table=mysql_query('select * from export_table');
$rows = mysql_fetch_assoc($select_table);

if ($rows)
$rows = mysql_fetch_assoc($select_table);

// get total number of fields present in the database
function getcsv($no_of_field_names)
$separate = '';

// do the action for all field names as field name
foreach ($no_of_field_names as $field_name)
if (preg_match('/\\r|\\n|,|"/', $field_name))
$field_name = '' . str_replace('', $field_name) . '';
echo $separate . $field_name;

//sepearte with the comma
$separate = ',';

//make new row and line
echo "\r\n";

this file is just for do the download, just give the link anchor tag,
usually you know to make just click export the data,
for that just give the link on anchor link, that will export the data table.

<a href="export.php"> export the database table </a>

How to use login() function in joomla?

  • You can use Below code during develop module and component with login functionality, get your username and password dynamically and put in array function.
  • when you call registration successfully function just add this code for autologin  functionality.
  $credentials = array( 'username' => '$name', 'password' => '$password');
    $login_site =& JFactory::getApplication('site');
    $login_site->login($credentials, $options=array());

Sending HTML Email with PHP

// multiple recipients
$to  = '' ;

// subject
$subject = 'Simple Html Email';

// message with HTML body 
$message = '<html>
              <title>Html Email With php</title>
             <div>". strip_tags($_POST['name']) . "</div>
             <div>". strip_tags($_POST['email']) . "</div>
             <div>". strip_tags($_POST['message']) . "</div>

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: hello <>' . "\r\n";
$headers .= 'From: hello <>' . "\r\n";
$headers .= 'Cc:' . "\r\n";
$headers .= 'Bcc:' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

How to use mail() function in php?

mail() function

$to      = '';
$subject = 'the subject';
$message = 'hello';
$headers = 'From:' . "\r\n" .
'Reply-To:' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

Note:-  $to, $subject, $message parameters are require for send mail and headers are optional parameter, you con use header parameter for add additional body mail.