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.

Html to convert pdf save in to folder and Mail

Html to convert pdf save in to folder and Mail

Here Example of html table structure to convert to pdf using jspdf.

<table id= “xyz”><thead><tr><th>id</th></tr></thead>

You can add style tag inline and give css to it.

Thereafter Include require Js file

var doc = new jsPDF(‘l’, ‘pt’, ‘a4’);
var specialElementHandlers = {
‘#xyz’: function(element, renderer){
return true;
var table = document.getElementById(“xyz”);
var res = doc.autoTableHtmlToJson(table, true);
doc.autoTable(res.columns,, {
startY: 60,
tableWidth: ‘auto’,
columnWidth: ‘auto’,
styles: {
overflow: ‘linebreak’
beforePageContent: function(data) {
doc.text(“New Pdf”, 40, 30);}
var pdf =doc.output(); //returns raw body of resulting PDF returned as a string as per the plugin documentation.
var data = new FormData();
data.append(“data” , pdf);
var xhr = new XMLHttpRequest(); ‘post’, ‘test.php’, true ); //Post to php Script to save to server

After That Create new php file to post data on it and using phpmailer you can attached and mail. eg:test.php

require_once “../../PHPMailer/class.phpmailer.php”; // Download phpmailer
$data = $_POST[‘data’];
$fname = ‘newpdf-‘.date(‘d-m-Y’).’.pdf’; // name the file
$file = fopen(“../pdf/” .$fname, ‘w’); // open the file path
fwrite($file, $data); //save data
$bodytext = ‘Please Find PDF’;
$email = new PHPMailer();
$email->From = ‘’;
$email->FromName = ‘My first pdf’;
$email->Subject = $file;
$email->Body = $bodytext;
$email->AddAddress( ‘’ );
//$email->AddAddress(‘’, ‘Person Two’);
//$file_to_attach = $_SERVER[‘DOCUMENT_ROOT’].’/front/pdf’;
$email->AddAttachment(‘../pdf/’.$fname );

return $email->Send();
} else {
echo “No Data Sent”;

How to add js and css in wordpress?

 * Proper way to enqueue scripts and styles
function theme_name_scripts() {
	wp_enqueue_style( 'style-name', get_stylesheet_uri() );
	wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );